This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIchiVaultSpell.sol
443 lines (390 loc) · 14.5 KB
/
IchiVaultSpell.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
import "./BasicSpell.sol";
import "../utils/BlueBerryConst.sol";
import "../libraries/UniV3/UniV3WrappedLibMockup.sol";
import "../interfaces/IOracle.sol";
import "../interfaces/IWIchiFarm.sol";
import "../interfaces/ichi/IICHIVault.sol";
contract IchiVaultSpell is BasicSpell, IUniswapV3SwapCallback {
using SafeERC20Upgradeable for IERC20Upgradeable;
struct Strategy {
address vault;
uint256 maxPositionSize;
}
/// @dev temperory state used to store uni v3 pool when swapping on uni v3
IUniswapV3Pool private swapPool;
/// @dev strategyId => ichi vault
Strategy[] public strategies;
/// @dev strategyId => collateral token => maxLTV
mapping(uint256 => mapping(address => uint256)) public maxLTV; // base 1e4
/// @dev address of ICHI farm wrapper
IWIchiFarm public wIchiFarm;
/// @dev address of ICHI token
address public ICHI;
event StrategyAdded(uint256 strategyId, address vault, uint256 maxPosSize);
event CollateralsSupportAdded(
uint256 strategyId,
address[] collaterals,
uint256[] maxLTVs
);
modifier existingStrategy(uint256 strategyId) {
if (strategyId >= strategies.length)
revert STRATEGY_NOT_EXIST(address(this), strategyId);
_;
}
modifier existingCollateral(uint256 strategyId, address col) {
if (maxLTV[strategyId][col] == 0)
revert COLLATERAL_NOT_EXIST(strategyId, col);
_;
}
function initialize(
IBank _bank,
address _werc20,
address _weth,
address _wichiFarm
) external initializer {
__BasicSpell_init(_bank, _werc20, _weth);
wIchiFarm = IWIchiFarm(_wichiFarm);
ICHI = address(wIchiFarm.ICHI());
IWIchiFarm(_wichiFarm).setApprovalForAll(address(_bank), true);
}
/**
* @notice Owner privileged function to add vault
* @param vault Address of ICHI angel vault
* @param maxPosSize, USD price based maximum size of a position for given vault, based 1e18
*/
function addStrategy(address vault, uint256 maxPosSize) external onlyOwner {
if (vault == address(0)) revert ZERO_ADDRESS();
if (maxPosSize == 0) revert ZERO_AMOUNT();
strategies.push(Strategy({vault: vault, maxPositionSize: maxPosSize}));
emit StrategyAdded(strategies.length - 1, vault, maxPosSize);
}
function addCollateralsSupport(
uint256 strategyId,
address[] memory collaterals,
uint256[] memory maxLTVs
) external existingStrategy(strategyId) onlyOwner {
if (collaterals.length != maxLTVs.length || collaterals.length == 0)
revert INPUT_ARRAY_MISMATCH();
for (uint256 i = 0; i < collaterals.length; i++) {
if (collaterals[i] == address(0)) revert ZERO_ADDRESS();
if (maxLTVs[i] == 0) revert ZERO_AMOUNT();
maxLTV[strategyId][collaterals[i]] = maxLTVs[i];
}
emit CollateralsSupportAdded(strategyId, collaterals, maxLTVs);
}
function _validateMaxLTV(uint256 strategyId) internal view {
uint256 debtValue = bank.getDebtValue(bank.POSITION_ID());
(, address collToken, uint256 collAmount, , , , , ) = bank
.getCurrentPositionInfo();
uint256 collPrice = bank.oracle().getPrice(collToken);
uint256 collValue = (collPrice * collAmount) /
10**IERC20Metadata(collToken).decimals();
if (
debtValue >
(collValue * maxLTV[strategyId][collToken]) / DENOMINATOR
) revert EXCEED_MAX_LTV();
}
/**
* @notice Internal function to deposit assets on ICHI Vault
* @param collToken Isolated collateral token address
* @param collAmount Amount of isolated collateral
* @param borrowToken Token address to borrow
* @param borrowAmount amount to borrow from Bank
*/
function depositInternal(
uint256 strategyId,
address collToken,
address borrowToken,
uint256 collAmount,
uint256 borrowAmount
) internal {
Strategy memory strategy = strategies[strategyId];
// 1. Lend isolated collaterals on compound
doLend(collToken, collAmount);
// 2. Borrow specific amounts
doBorrow(borrowToken, borrowAmount);
// 3. Add liquidity - Deposit on ICHI Vault
IICHIVault vault = IICHIVault(strategy.vault);
bool isTokenA = vault.token0() == borrowToken;
uint256 balance = IERC20(borrowToken).balanceOf(address(this));
ensureApprove(borrowToken, address(vault));
if (isTokenA) {
vault.deposit(balance, 0, address(this));
} else {
vault.deposit(0, balance, address(this));
}
// 4. Validate MAX LTV
_validateMaxLTV(strategyId);
// 5. Validate Max Pos Size
uint256 lpPrice = bank.oracle().getPrice(strategy.vault);
uint256 curPosSize = (lpPrice * vault.balanceOf(address(this))) /
10**IICHIVault(strategy.vault).decimals();
if (curPosSize > strategy.maxPositionSize)
revert EXCEED_MAX_POS_SIZE(strategyId);
}
/**
* @notice External function to deposit assets on IchiVault
* @param collToken Collateral Token address to deposit (e.g USDC)
* @param collAmount Amount of user's collateral (e.g USDC)
* @param borrowToken Address of token to borrow
* @param borrowAmount Amount to borrow from Bank
*/
function openPosition(
uint256 strategyId,
address collToken,
address borrowToken,
uint256 collAmount,
uint256 borrowAmount
)
external
existingStrategy(strategyId)
existingCollateral(strategyId, collToken)
{
// 1-3 Deposit on ichi vault
depositInternal(
strategyId,
collToken,
borrowToken,
collAmount,
borrowAmount
);
// 4. Put collateral - ICHI Vault Lp Token
address vault = strategies[strategyId].vault;
doPutCollateral(vault, IERC20(vault).balanceOf(address(this)));
}
/**
* @notice External function to deposit assets on IchiVault and farm in Ichi Farm
* @param collToken Collateral Token address to deposit (e.g USDC)
* @param collAmount Amount of user's collateral (e.g USDC)
* @param borrowToken Address of token to borrow
* @param borrowAmount Amount to borrow from Bank
* @param farmingPid Pool Id of vault lp on ICHI Farm
*/
function openPositionFarm(
uint256 strategyId,
address collToken,
address borrowToken,
uint256 collAmount,
uint256 borrowAmount,
uint256 farmingPid
)
external
existingStrategy(strategyId)
existingCollateral(strategyId, collToken)
{
Strategy memory strategy = strategies[strategyId];
address lpToken = wIchiFarm.ichiFarm().lpToken(farmingPid);
if (strategy.vault != lpToken) revert INCORRECT_LP(lpToken);
// 1-3 Deposit on ichi vault
depositInternal(
strategyId,
collToken,
borrowToken,
collAmount,
borrowAmount
);
// 4. Take out collateral
(
,
,
,
,
address posCollToken,
uint256 collId,
uint256 collSize,
) = bank.getCurrentPositionInfo();
if (collSize > 0) {
(uint256 decodedPid, ) = wIchiFarm.decodeId(collId);
if (farmingPid != decodedPid) revert INCORRECT_PID(farmingPid);
if (posCollToken != address(wIchiFarm))
revert INCORRECT_COLTOKEN(posCollToken);
bank.takeCollateral(collSize);
wIchiFarm.burn(collId, collSize);
}
// 5. Deposit on farming pool, put collateral
ensureApprove(strategy.vault, address(wIchiFarm));
uint256 lpAmount = IERC20(strategy.vault).balanceOf(address(this));
uint256 id = wIchiFarm.mint(farmingPid, lpAmount);
bank.putCollateral(address(wIchiFarm), id, lpAmount);
}
/**
* @dev Increase isolated collateral of position
* @param token Isolated collateral token address
* @param amount Amount of token to increase position
*/
function increasePosition(address token, uint256 amount) external {
// 1. Get user input amounts
doLend(token, amount);
}
/**
* @dev Reduce isolated collateral of position
* @param collToken Isolated collateral token address
* @param collAmount Amount of Isolated collateral
*/
function reducePosition(
uint256 strategyId,
address collToken,
uint256 collAmount
) external {
doWithdraw(collToken, collAmount);
doRefund(collToken);
_validateMaxLTV(strategyId);
}
function withdrawInternal(
uint256 strategyId,
address collToken,
address borrowToken,
uint256 amountRepay,
uint256 amountLpWithdraw,
uint256 amountShareWithdraw
) internal {
Strategy memory strategy = strategies[strategyId];
IICHIVault vault = IICHIVault(strategy.vault);
uint256 positionId = bank.POSITION_ID();
// 1. Compute repay amount if MAX_INT is supplied (max debt)
if (amountRepay == type(uint256).max) {
amountRepay = bank.borrowBalanceCurrent(positionId, borrowToken);
}
// 2. Calculate actual amount to remove
uint256 amtLPToRemove = vault.balanceOf(address(this)) -
amountLpWithdraw;
// 3. Withdraw liquidity from ICHI vault
vault.withdraw(amtLPToRemove, address(this));
// 4. Swap withdrawn tokens to initial deposit token
bool isTokenA = vault.token0() == borrowToken;
uint256 amountToSwap = IERC20(
isTokenA ? vault.token1() : vault.token0()
).balanceOf(address(this));
if (amountToSwap > 0) {
swapPool = IUniswapV3Pool(vault.pool());
swapPool.swap(
address(this),
// if withdraw token is Token0, then swap token1 -> token0 (false)
!isTokenA,
int256(amountToSwap),
isTokenA
? UniV3WrappedLibMockup.MAX_SQRT_RATIO - 1 // Token0 -> Token1
: UniV3WrappedLibMockup.MIN_SQRT_RATIO + 1, // Token1 -> Token0
abi.encode(address(this))
);
}
// 5. Withdraw isolated collateral from Bank
doWithdraw(collToken, amountShareWithdraw);
// 6. Repay
doRepay(borrowToken, amountRepay);
_validateMaxLTV(strategyId);
// 7. Refund
doRefund(borrowToken);
doRefund(collToken);
}
/**
* @notice External function to withdraw assets from ICHI Vault
* @param collToken Token address to withdraw (e.g USDC)
* @param borrowToken Token address to withdraw (e.g USDC)
* @param lpTakeAmt Amount of ICHI Vault LP token to take out from Bank
* @param amountRepay Amount to repay the loan
* @param amountLpWithdraw Amount of ICHI Vault LP to withdraw from ICHI Vault
* @param amountShareWithdraw Amount of Isolated collateral to withdraw from Compound
*/
function closePosition(
uint256 strategyId,
address collToken,
address borrowToken,
uint256 lpTakeAmt,
uint256 amountRepay,
uint256 amountLpWithdraw,
uint256 amountShareWithdraw
)
external
existingStrategy(strategyId)
existingCollateral(strategyId, collToken)
{
// 1. Take out collateral
doTakeCollateral(strategies[strategyId].vault, lpTakeAmt);
withdrawInternal(
strategyId,
collToken,
borrowToken,
amountRepay,
amountLpWithdraw,
amountShareWithdraw
);
}
function closePositionFarm(
uint256 strategyId,
address collToken,
address borrowToken,
uint256 lpTakeAmt,
uint256 amountRepay,
uint256 amountLpWithdraw,
uint256 amountShareWithdraw
)
external
existingStrategy(strategyId)
existingCollateral(strategyId, collToken)
{
address vault = strategies[strategyId].vault;
(, , , , address posCollToken, uint256 collId, , ) = bank
.getCurrentPositionInfo();
if (IWIchiFarm(posCollToken).getUnderlyingToken(collId) != vault)
revert INCORRECT_UNDERLYING(vault);
if (posCollToken != address(wIchiFarm))
revert INCORRECT_COLTOKEN(posCollToken);
// 1. Take out collateral
bank.takeCollateral(lpTakeAmt);
wIchiFarm.burn(collId, lpTakeAmt);
doCutRewardsFee(ICHI);
// 2-8. Remove liquidity
withdrawInternal(
strategyId,
collToken,
borrowToken,
amountRepay,
amountLpWithdraw,
amountShareWithdraw
);
// 9. Refund ichi token
doRefund(ICHI);
}
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external override {
if (msg.sender != address(swapPool)) revert NOT_FROM_UNIV3(msg.sender);
address payer = abi.decode(data, (address));
if (amount0Delta > 0) {
if (payer == address(this)) {
IERC20Upgradeable(swapPool.token0()).safeTransfer(
msg.sender,
uint256(amount0Delta)
);
} else {
IERC20Upgradeable(swapPool.token0()).safeTransferFrom(
payer,
msg.sender,
uint256(amount0Delta)
);
}
} else if (amount1Delta > 0) {
if (payer == address(this)) {
IERC20Upgradeable(swapPool.token1()).safeTransfer(
msg.sender,
uint256(amount1Delta)
);
} else {
IERC20Upgradeable(swapPool.token1()).safeTransferFrom(
payer,
msg.sender,
uint256(amount1Delta)
);
}
}
}
}