-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCNumaToken.sol
378 lines (339 loc) · 12 KB
/
CNumaToken.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
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.20;
import "./CErc20Immutable.sol";
import "@openzeppelin/contracts_5.0.2/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts_5.0.2/utils/structs/EnumerableSet.sol";
import "../interfaces/INumaVault.sol";
import "./INumaLeverageStrategy.sol";
/**
* @title CNumaToken
* @notice CTokens used with numa vault
* @author
*/
contract CNumaToken is CErc20Immutable {
INumaVault public vault;
uint constant max_strategy = 10;
using EnumerableSet for EnumerableSet.AddressSet;
EnumerableSet.AddressSet leverageStrategies;
/// @notice set vault event
event SetVault(address vaultAddress);
/// @notice open leverage event
event LeverageOpen(
CNumaToken indexed _collateral,
uint _suppliedAmount,
uint _borrowAmountVault,
uint _borrowAmount
);
/// @notice close leverage event
event LeverageClose(CNumaToken indexed _collateral, uint _borrowtorepay);
event RemovedStrategy(address);
event AddedStrategy(address);
modifier onlyAdmin() {
require(msg.sender == admin, "only admin");
_;
}
constructor(
address underlying_,
ComptrollerInterface comptroller_,
InterestRateModel interestRateModel_,
uint initialExchangeRateMantissa_,
string memory name_,
string memory symbol_,
uint8 decimals_,
uint fullUtilizationRate_,
address payable admin_,
address _vault
)
CErc20Immutable(
underlying_,
comptroller_,
interestRateModel_,
initialExchangeRateMantissa_,
name_,
symbol_,
decimals_,
fullUtilizationRate_,
admin_
)
{
vault = INumaVault(_vault);
}
function setVault(address _vault) external onlyAdmin {
vault = INumaVault(_vault);
emit SetVault(_vault);
}
/**
* @dev returns vaults list
*/
function getLeverageStrategies() external view returns (address[] memory) {
return leverageStrategies.values();
}
/**
* @dev adds a leverage strategy
*/
function addStrategy(address _strategy) external onlyAdmin {
require(
leverageStrategies.length() < max_strategy,
"too many strategies"
);
require(leverageStrategies.add(_strategy), "already in list");
emit AddedStrategy(_strategy);
}
/**
* @dev removes a leverage strategy
*/
function removeStrategy(address _strategy) external onlyAdmin {
require(leverageStrategies.contains(_strategy), "not in list");
leverageStrategies.remove(_strategy);
emit RemovedStrategy(_strategy);
}
function isStrategy(address _addy) public view returns (bool) {
return (leverageStrategies.contains(_addy));
}
// function borrowOnBehalf(uint borrowAmount,address payable borrower,address payable receiver) internal nonReentrant {
// accrueInterest();
// // borrowFresh emits borrow-specific logs on errors, so we don't need to
// borrowFreshOnBehalf(borrower,receiver, borrowAmount);
// }
function borrowInternalNoTransfer(
uint borrowAmount,
address borrower
) internal nonReentrant {
accrueInterest();
// borrowFresh emits borrow-specific logs on errors, so we don't need to
borrowFreshNoTransfer(payable(borrower), borrowAmount);
}
function getAmountIn(
uint256 _amount,
bool _closePosition,
uint _strategyIndex
) external view returns (uint256) {
INumaLeverageStrategy strat = INumaLeverageStrategy(
leverageStrategies.at(_strategyIndex)
);
return strat.getAmountIn(_amount, _closePosition);
}
/**
* @notice leverage by depositing and borrowing
* LTV will be _borrowAmount/(_borrowAmount+_suppliedAmount)
* 1) flash borrow _collateral.underlying from vault (will be repaid at the end of the function)
* 2) deposit as collateral (mint input CNumaToken), send minted Ctoken to sender
* 3) borrow other token using collateral
* 4) convert to other token using vault
* 5) flash repay vault
*
*/
function leverageStrategy(
uint _suppliedAmount,
uint _borrowAmount,
CNumaToken _collateral,
uint _strategyIndex
) external {
// AUDITV2FIX if we don't do that, borrow balance might change when calling borrowinternal
accrueInterest();
_collateral.accrueInterest();
INumaLeverageStrategy strat = INumaLeverageStrategy(
leverageStrategies.at(_strategyIndex)
);
address underlyingCollateral = _collateral.underlying();
// borrow from vault
vault.borrowLeverage(_borrowAmount, false);
// get user tokens
SafeERC20.safeTransferFrom(
IERC20(underlyingCollateral),
msg.sender,
address(this),
_suppliedAmount
);
uint totalAmount = _suppliedAmount + _borrowAmount;
// supply (mint collateral)
uint balCtokenBefore = EIP20Interface(address(_collateral)).balanceOf(
address(this)
);
EIP20Interface(underlyingCollateral).approve(
address(_collateral),
totalAmount
);
_collateral.mint(totalAmount);
uint balCtokenAfter = EIP20Interface(address(_collateral)).balanceOf(
address(this)
);
// send collateral to sender
uint receivedtokens = balCtokenAfter - balCtokenBefore;
require(receivedtokens > 0, "no collateral");
// transfer collateral to sender
SafeERC20.safeTransfer(
IERC20(address(_collateral)),
msg.sender,
receivedtokens
);
// how much to we need to borrow to repay vault
uint borrowAmount = strat.getAmountIn(_borrowAmount, false);
//
uint accountBorrowBefore = accountBorrows[msg.sender].principal;
// borrow but do not transfer borrowed tokens
borrowInternalNoTransfer(borrowAmount, msg.sender);
//uint accountBorrowAfter = accountBorrows[msg.sender].principal;
require(
(accountBorrows[msg.sender].principal - accountBorrowBefore) ==
borrowAmount,
"borrow ko"
);
// swap
EIP20Interface(underlying).approve(address(strat), borrowAmount);
(uint collateralReceived, uint unUsedInput) = strat.swap(
borrowAmount,
_borrowAmount,
false
);
// repay flashloan
EIP20Interface(underlyingCollateral).approve(
address(vault),
_borrowAmount
);
vault.repayLeverage(false);
//refund if more collateral is received than needed
if (collateralReceived > _borrowAmount) {
// send back the surplus
SafeERC20.safeTransfer(
IERC20(underlyingCollateral),
msg.sender,
collateralReceived - _borrowAmount
);
}
if (unUsedInput > 0) {
// we did not use all that was borrowed
// so we can repay that borrow
repayBorrowFresh(address(this), msg.sender, unUsedInput);
}
emit LeverageOpen(
_collateral,
_suppliedAmount,
_borrowAmount,
borrowAmount
);
}
function closeLeverageAmount(
CNumaToken _collateral,
uint _borrowtorepay,
uint _strategyIndex
) public view returns (uint, uint) {
INumaLeverageStrategy strat = INumaLeverageStrategy(
leverageStrategies.at(_strategyIndex)
);
// amount of underlying needed
uint swapAmountIn = strat.getAmountIn(_borrowtorepay, true);
// amount of ctokens to redeem this amount
Exp memory exchangeRate = Exp({
mantissa: _collateral.exchangeRateStored()
});
uint cTokenAmount = div_(swapAmountIn, exchangeRate);
return (cTokenAmount, swapAmountIn);
}
function closeLeverageStrategy(
CNumaToken _collateral,
uint _borrowtorepay,
uint _strategyIndex
) external {
// AUDITV2FIX
accrueInterest();
_collateral.accrueInterest();
INumaLeverageStrategy strat = INumaLeverageStrategy(
leverageStrategies.at(_strategyIndex)
);
address underlyingCollateral = _collateral.underlying();
// get borrowed amount
uint borrowAmountFull = borrowBalanceStored(msg.sender);
require(borrowAmountFull >= _borrowtorepay, "no borrow");
// clip to borrowed amount
if (_borrowtorepay > borrowAmountFull)
_borrowtorepay = borrowAmountFull;
// flashloan
vault.borrowLeverage(_borrowtorepay, true);
// repay borrow
repayBorrowFresh(address(this), msg.sender, _borrowtorepay);
// amount of underlying needed
(uint cTokenAmount, uint swapAmountIn) = closeLeverageAmount(
_collateral,
_borrowtorepay,
_strategyIndex
);
SafeERC20.safeTransferFrom(
IERC20(address(_collateral)),
msg.sender,
address(this),
cTokenAmount
);
// redeem to underlying
uint balBefore = IERC20(underlyingCollateral).balanceOf(address(this));
_collateral.redeemUnderlying(swapAmountIn);
uint balAfter = IERC20(underlyingCollateral).balanceOf(address(this));
uint received = balAfter - balBefore;
require(received >= swapAmountIn, "not enough redeem");
// swap to get enough token to repay flashlon
EIP20Interface(underlyingCollateral).approve(
address(strat),
swapAmountIn
);
(uint bought, uint unusedAmount) = strat.swap(
swapAmountIn,
_borrowtorepay,
true
);
// repay FLASHLOAN
EIP20Interface(underlying).approve(address(vault), _borrowtorepay);
vault.repayLeverage(true);
// send what has not been swapped to msg.sender (surplus)
if (bought > _borrowtorepay) {
// send back the surplus
SafeERC20.safeTransfer(
IERC20(underlying),
msg.sender,
bought - _borrowtorepay
);
}
// send also collateral that was not needed
if (unusedAmount > 0) {
SafeERC20.safeTransfer(
IERC20(underlyingCollateral),
msg.sender,
unusedAmount
);
}
emit LeverageClose(_collateral, _borrowtorepay);
}
/**
* @notice The sender liquidates the borrowers collateral.
* The collateral seized is transferred to the liquidator.
* @param borrower The borrower of this cToken to be liquidated
* @param repayAmount The amount of the underlying borrowed asset to repay
* @param cTokenCollateral The market in which to seize collateral from the borrower
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
*/
function liquidateBorrow(
address borrower,
uint repayAmount,
CTokenInterface cTokenCollateral
) external override returns (uint) {
// only vault can liquidate
require(msg.sender == address(vault), "vault only");
liquidateBorrowInternal(borrower, repayAmount, cTokenCollateral);
return NO_ERROR;
}
function liquidateBadDebt(
address borrower,
uint repayAmount,
uint percentageToTake,
CTokenInterface cTokenCollateral
) external override returns (uint) {
// only vault can liquidate
require(msg.sender == address(vault), "vault only");
liquidateBadDebtInternal(
borrower,
repayAmount,
percentageToTake,
cTokenCollateral
);
return NO_ERROR;
}
}