-
Notifications
You must be signed in to change notification settings - Fork 1
/
Kelp.sol
181 lines (144 loc) · 7.43 KB
/
Kelp.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.24;
import {Constants} from "@contracts/global/Constants.sol";
import {Deployments} from "@deployments/Deployments.sol";
import {IERC20} from "@interfaces/IERC20.sol";
import {TypeConvert} from "@contracts/global/TypeConvert.sol";
import { WithdrawRequest } from "@contracts/vaults/common/WithdrawRequestBase.sol";
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
import {ClonedCoolDownHolder} from "./ClonedCoolDownHolder.sol";
interface IWithdrawalManager {
function initiateWithdrawal(address asset, uint256 withdrawAmount) external;
function completeWithdrawal(address asset) external payable;
function nextLockedNonce(address asset) external view returns (uint256 requestNonce);
function withdrawalDelayBlocks() external view returns (uint256);
function getUserWithdrawalRequest(address asset, address user, uint256 userIndex)
external
view
returns (uint256 rsETHAmount, uint256 expectedAssetAmount, uint256 withdrawalStartBlock, uint256 userNonce);
function unlockQueue(
address asset,
uint256 firstExcludedIndex,
uint256 minimumAssetPrice,
uint256 minimumRsEthPrice
) external returns (uint256 rsETHBurned, uint256 assetAmountUnlocked);
}
interface ILidoWithdraw {
struct WithdrawalRequestStatus {
uint256 amountOfStETH;
uint256 amountOfShares;
address owner;
uint256 timestamp;
bool isFinalized;
bool isClaimed;
}
function requestWithdrawals(uint256[] memory _amounts, address _owner) external returns (uint256[] memory requestIds);
function getWithdrawalRequests(address _owner) external view returns (uint256[] memory requestsIds);
function getWithdrawalStatus(uint256[] memory _requestIds) external view returns (WithdrawalRequestStatus[] memory statuses);
function claimWithdrawal(uint256 _requestId) external;
function finalize(uint256 _lastRequestIdToBeFinalized, uint256 _maxShareRate) external payable;
function getLastRequestId() external view returns (uint256);
}
IERC20 constant rsETH = IERC20(0xA1290d69c65A6Fe4DF752f95823fae25cB99e5A7);
IWithdrawalManager constant WithdrawManager = IWithdrawalManager(0x62De59c08eB5dAE4b7E6F7a8cAd3006d6965ec16);
address constant stETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
ILidoWithdraw constant LidoWithdraw = ILidoWithdraw(0x889edC2eDab5f40e902b864aD4d7AdE8E412F9B1);
contract KelpCooldownHolder is ClonedCoolDownHolder {
bool public triggered = false;
constructor(address _vault) ClonedCoolDownHolder(_vault) { }
receive() external payable {}
/// @notice There is no way to stop a cool down
function _stopCooldown() internal pure override { revert(); }
function _startCooldown() internal override {
uint256 balance = rsETH.balanceOf(address(this));
rsETH.approve(address(WithdrawManager), balance);
// initiate withdraw from Kelp
WithdrawManager.initiateWithdrawal(stETH, balance);
}
/// @notice this method need to be called once withdraw on Kelp is finalized
/// to start withdraw process from Lido so we can unwrap stETH to ETH
/// since we are not able to withdraw ETH directly from Kelp
function triggerExtraStep() external {
require(!triggered);
(/* */, /* */, /* */, uint256 userWithdrawalRequestNonce) = WithdrawManager.getUserWithdrawalRequest(stETH, address(this), 0);
require(userWithdrawalRequestNonce < WithdrawManager.nextLockedNonce(stETH));
WithdrawManager.completeWithdrawal(stETH);
uint256 tokensClaimed = IERC20(stETH).balanceOf(address(this));
uint256[] memory amounts = new uint256[](1);
amounts[0] = tokensClaimed;
IERC20(stETH).approve(address(LidoWithdraw), amounts[0]);
LidoWithdraw.requestWithdrawals(amounts, address(this));
triggered = true;
}
function _finalizeCooldown() internal override returns (uint256 tokensClaimed, bool finalized) {
if (!triggered) {
return (0, false);
}
uint256[] memory requestIds = LidoWithdraw.getWithdrawalRequests(address(this));
ILidoWithdraw.WithdrawalRequestStatus[] memory withdrawsStatus = LidoWithdraw.getWithdrawalStatus(requestIds);
if (!withdrawsStatus[0].isFinalized) {
return (0, false);
}
LidoWithdraw.claimWithdrawal(requestIds[0]);
tokensClaimed = address(this).balance;
(bool sent,) = vault.call{value: tokensClaimed}("");
require(sent);
finalized = true;
}
}
library KelpLib {
using TypeConvert for int256;
uint256 internal constant stETH_PRECISION = 1e18;
function _getValueOfWithdrawRequest(
WithdrawRequest memory w,
address borrowToken,
uint256 borrowPrecision
) internal view returns (uint256) {
address holder = address(uint160(w.requestId));
uint256 expectedStETHAmount;
if (KelpCooldownHolder(payable(holder)).triggered()) {
uint256[] memory requestIds = LidoWithdraw.getWithdrawalRequests(holder);
ILidoWithdraw.WithdrawalRequestStatus[] memory withdrawsStatus = LidoWithdraw.getWithdrawalStatus(requestIds);
expectedStETHAmount = withdrawsStatus[0].amountOfStETH;
} else {
(/* */, expectedStETHAmount, /* */, /* */) = WithdrawManager.getUserWithdrawalRequest(stETH, holder, 0);
}
(int256 stETHToBorrowRate, /* */) = Deployments.TRADING_MODULE.getOraclePrice(
address(stETH), borrowToken
);
return (expectedStETHAmount * stETHToBorrowRate.toUint() * borrowPrecision) /
(Constants.EXCHANGE_RATE_PRECISION * stETH_PRECISION);
}
function _initiateWithdrawImpl(
uint256 balanceToTransfer,
address holderImplementation
) internal returns (uint256 requestId) {
KelpCooldownHolder holder = KelpCooldownHolder(payable(Clones.clone(holderImplementation)));
rsETH.transfer(address(holder), balanceToTransfer);
holder.startCooldown();
return uint256(uint160(address(holder)));
}
function _finalizeWithdrawImpl(
uint256 requestId
) internal returns (uint256 tokensClaimed, bool finalized) {
KelpCooldownHolder holder = KelpCooldownHolder(payable(address(uint160(requestId))));
(tokensClaimed, finalized) = holder.finalizeCooldown();
}
function _canFinalizeWithdrawRequest(uint256 requestId) internal view returns (bool) {
address holder = address(uint160(requestId));
if (!KelpCooldownHolder(payable(holder)).triggered()) return false;
uint256[] memory requestIds = LidoWithdraw.getWithdrawalRequests(holder);
ILidoWithdraw.WithdrawalRequestStatus[] memory withdrawsStatus = LidoWithdraw.getWithdrawalStatus(requestIds);
return withdrawsStatus[0].isFinalized;
}
function _canTriggerExtraStep(uint256 requestId) internal view returns (bool) {
address holder = address(uint160(requestId));
if (KelpCooldownHolder(payable(holder)).triggered()) return false;
(/* */, /* */, /* */, uint256 userWithdrawalRequestNonce) = WithdrawManager.getUserWithdrawalRequest(stETH, holder, 0);
return userWithdrawalRequestNonce < WithdrawManager.nextLockedNonce(stETH);
}
function _triggerExtraStep(uint256 requestId) internal {
KelpCooldownHolder holder = KelpCooldownHolder(payable(address(uint160(requestId))));
holder.triggerExtraStep();
}
}