-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYearnCurveVaultOperator.sol
286 lines (250 loc) · 10.4 KB
/
YearnCurveVaultOperator.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.14;
import "./../../Withdrawer.sol";
import "./YearnVaultStorage.sol";
import "./../../libraries/OperatorHelpers.sol";
import "./../../libraries/ExchangeHelpers.sol";
import "./../../interfaces/external/IWETH.sol";
import "../../libraries/StakingLPVaultHelpers.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./../../libraries/CurveHelpers/CurveHelpers.sol";
import "./../../interfaces/external/ICurvePool/ICurvePoolETH.sol";
import "./../../interfaces/external/IStakingVault/IYearnVault.sol";
import "./../../interfaces/external/ICurvePool/ICurvePoolNonETH.sol";
/// @title Yearn Curve Vault Operator
/// @notice Deposit/Withdraw in a Yearn Curve vault.
contract YearnCurveVaultOperator {
YearnVaultStorage public immutable operatorStorage;
/// @dev ETH address
address public immutable eth;
/// @dev WETH contract
IWETH private immutable weth;
/// @dev Withdrawer
Withdrawer private immutable withdrawer;
constructor(
address[] memory vaults,
CurvePool[] memory pools,
Withdrawer _withdrawer,
address _eth,
address _weth
) {
uint256 vaultsLength = vaults.length;
require(vaultsLength == pools.length, "YCVO: INVALID_VAULTS_LENGTH");
operatorStorage = new YearnVaultStorage();
for (uint256 i; i < vaultsLength; i++) {
operatorStorage.addVault(vaults[i], pools[i]);
}
operatorStorage.transferOwnership(msg.sender);
eth = _eth;
weth = IWETH(_weth);
withdrawer = _withdrawer;
}
/// @notice Add liquidity in a Curve pool that includes ETH,
/// deposit the LP token in a Yearn vault and receive
/// the Yearn vault shares
/// @param vault The Yearn vault address to deposit into
/// @param amount The amount of token to add liquidity
/// @param minVaultAmount The minimum of Yearn vault shares expected
/// @return amounts Array of amounts :
/// - [0] : The vault token received amount
/// - [1] : The token deposited amount
/// @return tokens Array of token addresses
/// - [0] : The vault token received address
/// - [1] : The token deposited address
function depositETH(
address vault,
uint256 amount,
uint256 minVaultAmount
) external payable returns (uint256[] memory amounts, address[] memory tokens) {
require(amount != 0, "YCVO: INVALID_AMOUNT");
(address pool, uint96 poolCoinAmount, address lpToken) = operatorStorage.vaults(vault);
require(pool != address(0), "YCVO: INVALID_VAULT");
uint256 vaultBalanceBefore = IERC20(vault).balanceOf(address(this));
uint256 ethBalanceBefore = weth.balanceOf(address(this));
ExchangeHelpers.setMaxAllowance(IERC20(address(weth)), address(withdrawer));
// withdraw ETH from WETH
withdrawer.withdraw(amount);
StakingLPVaultHelpers._addLiquidityAndDepositETH(
vault,
ICurvePoolETH(pool),
IERC20(lpToken),
poolCoinAmount,
eth,
amount
);
(amounts, tokens) = OperatorHelpers.getOutputAmounts(
IERC20(address(weth)),
ethBalanceBefore,
amount,
IERC20(vault),
vaultBalanceBefore,
minVaultAmount
);
}
/// @notice Add liquidity in a Curve pool, deposit
/// the LP token in a Yearn vault and receive
/// the Yearn vault shares
/// @param vault The Yearn vault address to deposit into
/// @param token The token to add liquidity
/// @param amount The amount of token to add liquidity
/// @param minVaultAmount The minimum of Yearn vault shares expected
/// @return amounts Array of amounts :
/// - [0] : The vault token received amount
/// - [1] : The token deposited amount
/// @return tokens Array of token addresses
/// - [0] : The vault token received address
/// - [1] : The token deposited address
function deposit(
address vault,
address token,
uint256 amount,
uint256 minVaultAmount
) external payable returns (uint256[] memory amounts, address[] memory tokens) {
require(amount != 0, "YCVO: INVALID_AMOUNT");
(address pool, uint96 poolCoinAmount, address lpToken) = operatorStorage.vaults(vault);
require(pool != address(0), "YCVO: INVALID_VAULT");
uint256 vaultBalanceBefore = IERC20(vault).balanceOf(address(this));
uint256 tokenBalanceBefore = IERC20(token).balanceOf(address(this));
StakingLPVaultHelpers._addLiquidityAndDeposit(
vault,
ICurvePoolNonETH(pool),
IERC20(lpToken),
poolCoinAmount,
token,
amount
);
(amounts, tokens) = OperatorHelpers.getOutputAmounts(
IERC20(token),
tokenBalanceBefore,
amount,
IERC20(vault),
vaultBalanceBefore,
minVaultAmount
);
}
/// @notice Withdraw the LP token from the Yearn vault,
/// remove ETH liquidity from the Curve pool
/// and receive one of the curve pool token
/// @param vault The Yearn vault address to withdraw from
/// @param amount The amount to withdraw
/// @param minAmountOut The minimum of output token expected
/// @return amounts Array of amounts :
/// - [0] : The token received amount
/// - [1] : The vault token deposited amount
/// @return tokens Array of token addresses
/// - [0] : The token received address
/// - [1] : The vault token deposited address
function withdrawETH(
address vault,
uint256 amount,
uint256 minAmountOut
) external payable returns (uint256[] memory amounts, address[] memory tokens) {
require(amount != 0, "YCVO: INVALID_AMOUNT");
(address pool, uint96 poolCoinAmount, address lpToken) = operatorStorage.vaults(vault);
require(pool != address(0), "YCVO: INVALID_VAULT");
uint256 vaultBalanceBefore = IERC20(vault).balanceOf(address(this));
uint256 tokenBalanceBefore = weth.balanceOf(address(this));
StakingLPVaultHelpers._withdrawAndRemoveLiquidity128(
vault,
amount,
ICurvePool(pool),
IERC20(lpToken),
poolCoinAmount,
eth
);
(amounts, tokens) = OperatorHelpers.getOutputAmounts(
IERC20(vault),
vaultBalanceBefore,
amount,
IERC20(address(weth)),
tokenBalanceBefore,
minAmountOut
);
}
/// @notice Withdraw the LP token from the Yearn vault,
/// remove the liquidity from the Curve pool
/// (using int128 for the curvePool.remove_liquidity_one_coin
/// coin index parameter) and receive one of the
/// curve pool token
/// @param vault The Yearn vault address to withdraw from
/// @param amount The amount to withdraw
/// @param outputToken Output token to receive
/// @param minAmountOut The minimum of output token expected
/// @return amounts Array of amounts :
/// - [0] : The token received amount
/// - [1] : The vault token deposited amount
/// @return tokens Array of token addresses
/// - [0] : The token received address
/// - [1] : The vault token deposited address
function withdraw128(
address vault,
uint256 amount,
IERC20 outputToken,
uint256 minAmountOut
) external payable returns (uint256[] memory amounts, address[] memory tokens) {
require(amount != 0, "YCVO: INVALID_AMOUNT");
(address pool, uint96 poolCoinAmount, address lpToken) = operatorStorage.vaults(vault);
require(pool != address(0), "YCVO: INVALID_VAULT");
uint256 vaultBalanceBefore = IERC20(vault).balanceOf(address(this));
uint256 tokenBalanceBefore = outputToken.balanceOf(address(this));
StakingLPVaultHelpers._withdrawAndRemoveLiquidity128(
vault,
amount,
ICurvePool(pool),
IERC20(lpToken),
poolCoinAmount,
address(outputToken)
);
(amounts, tokens) = OperatorHelpers.getOutputAmounts(
IERC20(vault),
vaultBalanceBefore,
amount,
outputToken,
tokenBalanceBefore,
minAmountOut
);
}
/// @notice Withdraw the LP token from the Yearn vault,
/// remove the liquidity from the Curve pool
/// (using uint256 for the curvePool.remove_liquidity_one_coin
/// coin index parameter) and receive one of the
/// curve pool token
/// @param vault The Yearn vault address to withdraw from
/// @param amount The amount to withdraw
/// @param outputToken Output token to receive
/// @param minAmountOut The minimum of output token expected
/// @return amounts Array of amounts :
/// - [0] : The token received amount
/// - [1] : The vault token deposited amount
/// @return tokens Array of token addresses
/// - [0] : The token received address
/// - [1] : The vault token deposited address
function withdraw256(
address vault,
uint256 amount,
IERC20 outputToken,
uint256 minAmountOut
) external payable returns (uint256[] memory amounts, address[] memory tokens) {
require(amount != 0, "YCVO: INVALID_AMOUNT");
(address pool, uint96 poolCoinAmount, address lpToken) = operatorStorage.vaults(vault);
require(pool != address(0), "YCVO: INVALID_VAULT");
uint256 vaultBalanceBefore = IERC20(vault).balanceOf(address(this));
uint256 tokenBalanceBefore = outputToken.balanceOf(address(this));
StakingLPVaultHelpers._withdrawAndRemoveLiquidity256(
vault,
amount,
ICurvePoolNonETH(pool),
IERC20(lpToken),
poolCoinAmount,
address(outputToken)
);
(amounts, tokens) = OperatorHelpers.getOutputAmounts(
IERC20(vault),
vaultBalanceBefore,
amount,
outputToken,
tokenBalanceBefore,
minAmountOut
);
}
}