-
Notifications
You must be signed in to change notification settings - Fork 93
/
AaveLeverageModule.sol
1099 lines (937 loc) · 51.7 KB
/
AaveLeverageModule.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2021 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import { AaveV2 } from "../integration/lib/AaveV2.sol";
import { IAToken } from "../../interfaces/external/aave-v2/IAToken.sol";
import { IController } from "../../interfaces/IController.sol";
import { IDebtIssuanceModule } from "../../interfaces/IDebtIssuanceModule.sol";
import { IExchangeAdapter } from "../../interfaces/IExchangeAdapter.sol";
import { ILendingPool } from "../../interfaces/external/aave-v2/ILendingPool.sol";
import { ILendingPoolAddressesProvider } from "../../interfaces/external/aave-v2/ILendingPoolAddressesProvider.sol";
import { IProtocolDataProvider } from "../../interfaces/external/aave-v2/IProtocolDataProvider.sol";
import { ISetToken } from "../../interfaces/ISetToken.sol";
import { IVariableDebtToken } from "../../interfaces/external/aave-v2/IVariableDebtToken.sol";
import { ModuleBase } from "../lib/ModuleBase.sol";
/**
* @title AaveLeverageModule
* @author Set Protocol
* @notice Smart contract that enables leverage trading using Aave as the lending protocol.
* @dev Do not use this module in conjunction with other debt modules that allow Aave debt positions as it could lead to double counting of
* debt when borrowed assets are the same.
*/
contract AaveLeverageModule is ModuleBase, ReentrancyGuard, Ownable {
using AaveV2 for ISetToken;
/* ============ Structs ============ */
struct EnabledAssets {
address[] collateralAssets; // Array of enabled underlying collateral assets for a SetToken
address[] borrowAssets; // Array of enabled underlying borrow assets for a SetToken
}
struct ActionInfo {
ISetToken setToken; // SetToken instance
IExchangeAdapter exchangeAdapter; // Exchange adapter instance
uint256 setTotalSupply; // Total supply of SetToken
uint256 notionalSendQuantity; // Total notional quantity sent to exchange
uint256 minNotionalReceiveQuantity; // Min total notional received from exchange
IERC20 collateralAsset; // Address of collateral asset
IERC20 borrowAsset; // Address of borrow asset
uint256 preTradeReceiveTokenBalance; // Balance of pre-trade receive token balance
}
struct ReserveTokens {
IAToken aToken; // Reserve's aToken instance
IVariableDebtToken variableDebtToken; // Reserve's variable debt token instance
}
/* ============ Events ============ */
/**
* @dev Emitted on lever()
* @param _setToken Instance of the SetToken being levered
* @param _borrowAsset Asset being borrowed for leverage
* @param _collateralAsset Collateral asset being levered
* @param _exchangeAdapter Exchange adapter used for trading
* @param _totalBorrowAmount Total amount of `_borrowAsset` borrowed
* @param _totalReceiveAmount Total amount of `_collateralAsset` received by selling `_borrowAsset`
* @param _protocolFee Protocol fee charged
*/
event LeverageIncreased(
ISetToken indexed _setToken,
IERC20 indexed _borrowAsset,
IERC20 indexed _collateralAsset,
IExchangeAdapter _exchangeAdapter,
uint256 _totalBorrowAmount,
uint256 _totalReceiveAmount,
uint256 _protocolFee
);
/**
* @dev Emitted on delever() and deleverToZeroBorrowBalance()
* @param _setToken Instance of the SetToken being delevered
* @param _collateralAsset Asset sold to decrease leverage
* @param _repayAsset Asset being bought to repay to Aave
* @param _exchangeAdapter Exchange adapter used for trading
* @param _totalRedeemAmount Total amount of `_collateralAsset` being sold
* @param _totalRepayAmount Total amount of `_repayAsset` being repaid
* @param _protocolFee Protocol fee charged
*/
event LeverageDecreased(
ISetToken indexed _setToken,
IERC20 indexed _collateralAsset,
IERC20 indexed _repayAsset,
IExchangeAdapter _exchangeAdapter,
uint256 _totalRedeemAmount,
uint256 _totalRepayAmount,
uint256 _protocolFee
);
/**
* @dev Emitted on addCollateralAssets() and removeCollateralAssets()
* @param _setToken Instance of SetToken whose collateral assets is updated
* @param _added true if assets are added false if removed
* @param _assets Array of collateral assets being added/removed
*/
event CollateralAssetsUpdated(
ISetToken indexed _setToken,
bool indexed _added,
IERC20[] _assets
);
/**
* @dev Emitted on addBorrowAssets() and removeBorrowAssets()
* @param _setToken Instance of SetToken whose borrow assets is updated
* @param _added true if assets are added false if removed
* @param _assets Array of borrow assets being added/removed
*/
event BorrowAssetsUpdated(
ISetToken indexed _setToken,
bool indexed _added,
IERC20[] _assets
);
/**
* @dev Emitted when `underlyingToReserveTokensMappings` is updated
* @param _underlying Address of the underlying asset
* @param _aToken Updated aave reserve aToken
* @param _variableDebtToken Updated aave reserve variable debt token
*/
event ReserveTokensUpdated(
IERC20 indexed _underlying,
IAToken indexed _aToken,
IVariableDebtToken indexed _variableDebtToken
);
/**
* @dev Emitted on updateAllowedSetToken()
* @param _setToken SetToken being whose allowance to initialize this module is being updated
* @param _added true if added false if removed
*/
event SetTokenStatusUpdated(
ISetToken indexed _setToken,
bool indexed _added
);
/**
* @dev Emitted on updateAnySetAllowed()
* @param _anySetAllowed true if any set is allowed to initialize this module, false otherwise
*/
event AnySetAllowedUpdated(
bool indexed _anySetAllowed
);
/* ============ Constants ============ */
// This module only supports borrowing in variable rate mode from Aave which is represented by 2
uint256 constant internal BORROW_RATE_MODE = 2;
// String identifying the DebtIssuanceModule in the IntegrationRegistry. Note: Governance must add DefaultIssuanceModule as
// the string as the integration name
string constant internal DEFAULT_ISSUANCE_MODULE_NAME = "DefaultIssuanceModule";
// 0 index stores protocol fee % on the controller, charged in the _executeTrade function
uint256 constant internal PROTOCOL_TRADE_FEE_INDEX = 0;
/* ============ State Variables ============ */
// Mapping to efficiently fetch reserve token addresses. Tracking Aave reserve token addresses and updating them
// upon requirement is more efficient than fetching them each time from Aave.
// Note: For an underlying asset to be enabled as collateral/borrow asset on SetToken, it must be added to this mapping first.
mapping(IERC20 => ReserveTokens) public underlyingToReserveTokens;
// AaveV2 LendingPool contract exposes all user-oriented actions such as deposit, borrow, withdraw and repay
// We use this variable along with AaveV2 library contract to invoke those actions on SetToken
ILendingPool public lendingPool;
// Used to fetch reserves and user data from AaveV2
IProtocolDataProvider public protocolDataProvider;
// Used to fetch lendingPool address. This contract is immutable and its address will never change.
ILendingPoolAddressesProvider public lendingPoolAddressesProvider;
// Mapping to efficiently check if collateral asset is enabled in SetToken
mapping(ISetToken => mapping(IERC20 => bool)) public collateralAssetEnabled;
// Mapping to efficiently check if a borrow asset is enabled in SetToken
mapping(ISetToken => mapping(IERC20 => bool)) public borrowAssetEnabled;
// Internal mapping of enabled collateral and borrow tokens for syncing positions
mapping(ISetToken => EnabledAssets) internal enabledAssets;
// Mapping of SetToken to boolean indicating if SetToken is on allow list. Updateable by governance
mapping(ISetToken => bool) public allowedSetTokens;
// Boolean that returns if any SetToken can initialize this module. If false, then subject to allow list. Updateable by governance.
bool public anySetAllowed;
/* ============ Constructor ============ */
/**
* @dev Instantiate addresses. Underlying to reserve tokens mapping is created.
* @param _controller Address of controller contract
* @param _lendingPoolAddressesProvider Address of Aave LendingPoolAddressProvider
* @param _protocolDataProvider Address of Aave ProtocolDataProvider
*/
constructor(
IController _controller,
ILendingPoolAddressesProvider _lendingPoolAddressesProvider,
IProtocolDataProvider _protocolDataProvider
)
public
ModuleBase(_controller)
{
lendingPoolAddressesProvider = _lendingPoolAddressesProvider;
protocolDataProvider = _protocolDataProvider;
lendingPool = ILendingPool(_lendingPoolAddressesProvider.getLendingPool());
IProtocolDataProvider.TokenData[] memory reserveTokens = protocolDataProvider.getAllReservesTokens();
for(uint256 i = 0; i < reserveTokens.length; i++) {
_addUnderlyingToReserveTokensMapping(IERC20(reserveTokens[i].tokenAddress));
}
}
/* ============ External Functions ============ */
/**
* @dev MANAGER ONLY: Increases leverage for a given collateral position using an enabled borrow asset.
* Borrows _borrowAsset from Aave. Performs a DEX trade, exchanging the _borrowAsset for _collateralAsset.
* Deposits _collateralAsset to Aave and mints corresponding aToken.
* Note: Both collateral and borrow assets need to be enabled, and they must not be the same asset.
* @param _setToken Instance of the SetToken
* @param _borrowAsset Address of underlying asset being borrowed for leverage
* @param _collateralAsset Address of underlying collateral asset
* @param _borrowQuantityUnits Borrow quantity of asset in position units
* @param _minReceiveQuantityUnits Min receive quantity of collateral asset to receive post-trade in position units
* @param _tradeAdapterName Name of trade adapter
* @param _tradeData Arbitrary data for trade
*/
function lever(
ISetToken _setToken,
IERC20 _borrowAsset,
IERC20 _collateralAsset,
uint256 _borrowQuantityUnits,
uint256 _minReceiveQuantityUnits,
string memory _tradeAdapterName,
bytes memory _tradeData
)
external
nonReentrant
onlyManagerAndValidSet(_setToken)
{
// For levering up, send quantity is derived from borrow asset and receive quantity is derived from
// collateral asset
ActionInfo memory leverInfo = _createAndValidateActionInfo(
_setToken,
_borrowAsset,
_collateralAsset,
_borrowQuantityUnits,
_minReceiveQuantityUnits,
_tradeAdapterName,
true
);
_borrow(leverInfo.setToken, leverInfo.borrowAsset, leverInfo.notionalSendQuantity);
uint256 postTradeReceiveQuantity = _executeTrade(leverInfo, _borrowAsset, _collateralAsset, _tradeData);
uint256 protocolFee = _accrueProtocolFee(_setToken, _collateralAsset, postTradeReceiveQuantity);
uint256 postTradeCollateralQuantity = postTradeReceiveQuantity.sub(protocolFee);
_deposit(leverInfo.setToken, _collateralAsset, postTradeCollateralQuantity);
_updateLeverPositions(leverInfo, _borrowAsset);
emit LeverageIncreased(
_setToken,
_borrowAsset,
_collateralAsset,
leverInfo.exchangeAdapter,
leverInfo.notionalSendQuantity,
postTradeCollateralQuantity,
protocolFee
);
}
/**
* @dev MANAGER ONLY: Decrease leverage for a given collateral position using an enabled borrow asset.
* Withdraws _collateralAsset from Aave. Performs a DEX trade, exchanging the _collateralAsset for _repayAsset.
* Repays _repayAsset to Aave and burns corresponding debt tokens.
* Note: Both collateral and borrow assets need to be enabled, and they must not be the same asset.
* @param _setToken Instance of the SetToken
* @param _collateralAsset Address of underlying collateral asset being withdrawn
* @param _repayAsset Address of underlying borrowed asset being repaid
* @param _redeemQuantityUnits Quantity of collateral asset to delever in position units
* @param _minRepayQuantityUnits Minimum amount of repay asset to receive post trade in position units
* @param _tradeAdapterName Name of trade adapter
* @param _tradeData Arbitrary data for trade
*/
function delever(
ISetToken _setToken,
IERC20 _collateralAsset,
IERC20 _repayAsset,
uint256 _redeemQuantityUnits,
uint256 _minRepayQuantityUnits,
string memory _tradeAdapterName,
bytes memory _tradeData
)
external
nonReentrant
onlyManagerAndValidSet(_setToken)
{
// Note: for delevering, send quantity is derived from collateral asset and receive quantity is derived from
// repay asset
ActionInfo memory deleverInfo = _createAndValidateActionInfo(
_setToken,
_collateralAsset,
_repayAsset,
_redeemQuantityUnits,
_minRepayQuantityUnits,
_tradeAdapterName,
false
);
_withdraw(deleverInfo.setToken, _collateralAsset, deleverInfo.notionalSendQuantity);
uint256 postTradeReceiveQuantity = _executeTrade(deleverInfo, _collateralAsset, _repayAsset, _tradeData);
uint256 protocolFee = _accrueProtocolFee(_setToken, _repayAsset, postTradeReceiveQuantity);
uint256 repayQuantity = postTradeReceiveQuantity.sub(protocolFee);
_repayBorrow(deleverInfo.setToken, _repayAsset, repayQuantity);
_updateLeverPositions(deleverInfo, _repayAsset);
emit LeverageDecreased(
_setToken,
_collateralAsset,
_repayAsset,
deleverInfo.exchangeAdapter,
deleverInfo.notionalSendQuantity,
repayQuantity,
protocolFee
);
}
/** @dev MANAGER ONLY: Pays down the borrow asset to 0 selling off a given amount of collateral asset.
* Withdraws _collateralAsset from Aave. Performs a DEX trade, exchanging the _collateralAsset for _repayAsset.
* Minimum receive amount for the DEX trade is set to the current variable debt balance of the borrow asset.
* Repays received _repayAsset to Aave which burns corresponding debt tokens. Any extra received borrow asset is .
* updated as equity. No protocol fee is charged.
* Note: Both collateral and borrow assets need to be enabled, and they must not be the same asset.
* The function reverts if not enough collateral asset is redeemed to buy required minimum amount of _repayAsset.
* @param _setToken Instance of the SetToken
* @param _collateralAsset Address of underlying collateral asset being redeemed
* @param _repayAsset Address of underlying asset being repaid
* @param _redeemQuantityUnits Quantity of collateral asset to delever in position units
* @param _tradeAdapterName Name of trade adapter
* @param _tradeData Arbitrary data for trade
* @return uint256 Notional repay quantity
*/
function deleverToZeroBorrowBalance(
ISetToken _setToken,
IERC20 _collateralAsset,
IERC20 _repayAsset,
uint256 _redeemQuantityUnits,
string memory _tradeAdapterName,
bytes memory _tradeData
)
external
nonReentrant
onlyManagerAndValidSet(_setToken)
returns (uint256)
{
uint256 setTotalSupply = _setToken.totalSupply();
uint256 notionalRedeemQuantity = _redeemQuantityUnits.preciseMul(setTotalSupply);
require(borrowAssetEnabled[_setToken][_repayAsset], "Borrow not enabled");
uint256 notionalRepayQuantity = underlyingToReserveTokens[_repayAsset].variableDebtToken.balanceOf(address(_setToken));
require(notionalRepayQuantity > 0, "Borrow balance is zero");
ActionInfo memory deleverInfo = _createAndValidateActionInfoNotional(
_setToken,
_collateralAsset,
_repayAsset,
notionalRedeemQuantity,
notionalRepayQuantity,
_tradeAdapterName,
false,
setTotalSupply
);
_withdraw(deleverInfo.setToken, _collateralAsset, deleverInfo.notionalSendQuantity);
_executeTrade(deleverInfo, _collateralAsset, _repayAsset, _tradeData);
_repayBorrow(deleverInfo.setToken, _repayAsset, notionalRepayQuantity);
// Update default position first to save gas on editing borrow position
_setToken.calculateAndEditDefaultPosition(
address(_repayAsset),
deleverInfo.setTotalSupply,
deleverInfo.preTradeReceiveTokenBalance
);
_updateLeverPositions(deleverInfo, _repayAsset);
emit LeverageDecreased(
_setToken,
_collateralAsset,
_repayAsset,
deleverInfo.exchangeAdapter,
deleverInfo.notionalSendQuantity,
notionalRepayQuantity,
0 // No protocol fee
);
return notionalRepayQuantity;
}
/**
* @dev CALLABLE BY ANYBODY: Sync Set positions with ALL enabled Aave collateral and borrow positions.
* For collateral assets, update aToken default position. For borrow assets, update external borrow position.
* - Collateral assets may come out of sync when interest is accrued or a position is liquidated
* - Borrow assets may come out of sync when interest is accrued or position is liquidated and borrow is repaid
* Note: In Aave, both collateral and borrow interest is accrued in each block by increasing the balance of
* aTokens and debtTokens for each user, and 1 aToken = 1 variableDebtToken = 1 underlying.
* @param _setToken Instance of the SetToken
*/
function sync(ISetToken _setToken) public nonReentrant onlyValidAndInitializedSet(_setToken) {
uint256 setTotalSupply = _setToken.totalSupply();
// Only sync positions when Set supply is not 0. Without this check, if sync is called by someone before the
// first issuance, then editDefaultPosition would remove the default positions from the SetToken
if (setTotalSupply > 0) {
address[] memory collateralAssets = enabledAssets[_setToken].collateralAssets;
for(uint256 i = 0; i < collateralAssets.length; i++) {
IAToken aToken = underlyingToReserveTokens[IERC20(collateralAssets[i])].aToken;
uint256 previousPositionUnit = _setToken.getDefaultPositionRealUnit(address(aToken)).toUint256();
uint256 newPositionUnit = _getCollateralPosition(_setToken, aToken, setTotalSupply);
// Note: Accounts for if position does not exist on SetToken but is tracked in enabledAssets
if (previousPositionUnit != newPositionUnit) {
_updateCollateralPosition(_setToken, aToken, newPositionUnit);
}
}
address[] memory borrowAssets = enabledAssets[_setToken].borrowAssets;
for(uint256 i = 0; i < borrowAssets.length; i++) {
IERC20 borrowAsset = IERC20(borrowAssets[i]);
int256 previousPositionUnit = _setToken.getExternalPositionRealUnit(address(borrowAsset), address(this));
int256 newPositionUnit = _getBorrowPosition(_setToken, borrowAsset, setTotalSupply);
// Note: Accounts for if position does not exist on SetToken but is tracked in enabledAssets
if (newPositionUnit != previousPositionUnit) {
_updateBorrowPosition(_setToken, borrowAsset, newPositionUnit);
}
}
}
}
/**
* @dev MANAGER ONLY: Initializes this module to the SetToken. Either the SetToken needs to be on the allowed list
* or anySetAllowed needs to be true. Only callable by the SetToken's manager.
* Note: Managers can enable collateral and borrow assets that don't exist as positions on the SetToken
* @param _setToken Instance of the SetToken to initialize
* @param _collateralAssets Underlying tokens to be enabled as collateral in the SetToken
* @param _borrowAssets Underlying tokens to be enabled as borrow in the SetToken
*/
function initialize(
ISetToken _setToken,
IERC20[] memory _collateralAssets,
IERC20[] memory _borrowAssets
)
external
onlySetManager(_setToken, msg.sender)
onlyValidAndPendingSet(_setToken)
{
if (!anySetAllowed) {
require(allowedSetTokens[_setToken], "Not allowed SetToken");
}
// Initialize module before trying register
_setToken.initializeModule();
// Get debt issuance module registered to this module and require that it is initialized
require(_setToken.isInitializedModule(getAndValidateAdapter(DEFAULT_ISSUANCE_MODULE_NAME)), "Issuance not initialized");
// Try if register exists on any of the modules including the debt issuance module
address[] memory modules = _setToken.getModules();
for(uint256 i = 0; i < modules.length; i++) {
try IDebtIssuanceModule(modules[i]).registerToIssuanceModule(_setToken) {} catch {}
}
// _collateralAssets and _borrowAssets arrays are validated in their respective internal functions
_addCollateralAssets(_setToken, _collateralAssets);
_addBorrowAssets(_setToken, _borrowAssets);
}
/**
* @dev MANAGER ONLY: Removes this module from the SetToken, via call by the SetToken. Any deposited collateral assets
* are disabled to be used as collateral on Aave. Aave Settings and manager enabled assets state is deleted.
* Note: Function will revert is there is any debt remaining on Aave
*/
function removeModule() external override onlyValidAndInitializedSet(ISetToken(msg.sender)) {
ISetToken setToken = ISetToken(msg.sender);
// Sync Aave and SetToken positions prior to any removal action
sync(setToken);
address[] memory borrowAssets = enabledAssets[setToken].borrowAssets;
for(uint256 i = 0; i < borrowAssets.length; i++) {
IERC20 borrowAsset = IERC20(borrowAssets[i]);
require(underlyingToReserveTokens[borrowAsset].variableDebtToken.balanceOf(address(setToken)) == 0, "Variable debt remaining");
delete borrowAssetEnabled[setToken][borrowAsset];
}
address[] memory collateralAssets = enabledAssets[setToken].collateralAssets;
for(uint256 i = 0; i < collateralAssets.length; i++) {
IERC20 collateralAsset = IERC20(collateralAssets[i]);
_updateUseReserveAsCollateral(setToken, collateralAsset, false);
delete collateralAssetEnabled[setToken][collateralAsset];
}
delete enabledAssets[setToken];
// Try if unregister exists on any of the modules
address[] memory modules = setToken.getModules();
for(uint256 i = 0; i < modules.length; i++) {
try IDebtIssuanceModule(modules[i]).unregisterFromIssuanceModule(setToken) {} catch {}
}
}
/**
* @dev MANAGER ONLY: Add registration of this module on debt issuance module for the SetToken.
* Note: if the debt issuance module is not added to SetToken before this module is initialized, then this function
* needs to be called if the debt issuance module is later added and initialized to prevent state inconsistencies
* @param _setToken Instance of the SetToken
* @param _debtIssuanceModule Debt issuance module address to register
*/
function registerToModule(ISetToken _setToken, IDebtIssuanceModule _debtIssuanceModule) external onlyManagerAndValidSet(_setToken) {
require(_setToken.isInitializedModule(address(_debtIssuanceModule)), "Issuance not initialized");
_debtIssuanceModule.registerToIssuanceModule(_setToken);
}
/**
* @dev CALLABLE BY ANYBODY: Updates `underlyingToReserveTokens` mappings. Reverts if mapping already exists
* or the passed _underlying asset does not have a valid reserve on Aave.
* Note: Call this function when Aave adds a new reserve.
* @param _underlying Address of underlying asset
*/
function addUnderlyingToReserveTokensMapping(IERC20 _underlying) external {
require(address(underlyingToReserveTokens[_underlying].aToken) == address(0), "Mapping already exists");
// An active reserve is an alias for a valid reserve on Aave.
(,,,,,,,, bool isActive,) = protocolDataProvider.getReserveConfigurationData(address(_underlying));
require(isActive, "Invalid aave reserve");
_addUnderlyingToReserveTokensMapping(_underlying);
}
/**
* @dev MANAGER ONLY: Add collateral assets. aTokens corresponding to collateral assets are tracked for syncing positions.
* Note: Reverts with "Borrow already enabled" if there are duplicate assets in the passed _newBorrowAssets array.
*
* NOTE: ALL ADDED COLLATERAL ASSETS CAN BE ADDED AS A POSITION ON THE SET TOKEN WITHOUT MANAGER'S EXPLICIT PERMISSION.
* UNWANTED EXTRA POSITIONS CAN BREAK EXTERNAL LOGIC, INCREASE COST OF MINT/REDEEM OF SET TOKEN, AMONG OTHER POTENTIAL UNINTENDED CONSEQUENCES.
* SO, PLEASE ADD ONLY THOSE COLLATERAL ASSETS WHOSE CORRESPONDING aTOKENS ARE NEEDED AS DEFAULT POSITIONS ON THE SET TOKEN.
*
* @param _setToken Instance of the SetToken
* @param _newCollateralAssets Addresses of new collateral underlying assets
*/
function addCollateralAssets(ISetToken _setToken, IERC20[] memory _newCollateralAssets) external onlyManagerAndValidSet(_setToken) {
_addCollateralAssets(_setToken, _newCollateralAssets);
}
/**
* @dev MANAGER ONLY: Remove collateral asset. Disable deposited assets to be used as collateral on Aave market.
* @param _setToken Instance of the SetToken
* @param _collateralAssets Addresses of collateral underlying assets to remove
*/
function removeCollateralAssets(ISetToken _setToken, IERC20[] memory _collateralAssets) external onlyManagerAndValidSet(_setToken) {
for(uint256 i = 0; i < _collateralAssets.length; i++) {
IERC20 collateralAsset = _collateralAssets[i];
require(collateralAssetEnabled[_setToken][collateralAsset], "Collateral not enabled");
_updateUseReserveAsCollateral(_setToken, collateralAsset, false);
delete collateralAssetEnabled[_setToken][collateralAsset];
enabledAssets[_setToken].collateralAssets.removeStorage(address(collateralAsset));
}
emit CollateralAssetsUpdated(_setToken, false, _collateralAssets);
}
/**
* @dev MANAGER ONLY: Add borrow assets. Debt tokens corresponding to borrow assets are tracked for syncing positions.
* Note: Reverts with "Borrow already enabled" if there are duplicate assets in the passed _newBorrowAssets array.
* @param _setToken Instance of the SetToken
* @param _newBorrowAssets Addresses of borrow underlying assets to add
*/
function addBorrowAssets(ISetToken _setToken, IERC20[] memory _newBorrowAssets) external onlyManagerAndValidSet(_setToken) {
_addBorrowAssets(_setToken, _newBorrowAssets);
}
/**
* @dev MANAGER ONLY: Remove borrow asset.
* Note: If there is a borrow balance, borrow asset cannot be removed
* @param _setToken Instance of the SetToken
* @param _borrowAssets Addresses of borrow underlying assets to remove
*/
function removeBorrowAssets(ISetToken _setToken, IERC20[] memory _borrowAssets) external onlyManagerAndValidSet(_setToken) {
for(uint256 i = 0; i < _borrowAssets.length; i++) {
IERC20 borrowAsset = _borrowAssets[i];
require(borrowAssetEnabled[_setToken][borrowAsset], "Borrow not enabled");
require(underlyingToReserveTokens[borrowAsset].variableDebtToken.balanceOf(address(_setToken)) == 0, "Variable debt remaining");
delete borrowAssetEnabled[_setToken][borrowAsset];
enabledAssets[_setToken].borrowAssets.removeStorage(address(borrowAsset));
}
emit BorrowAssetsUpdated(_setToken, false, _borrowAssets);
}
/**
* @dev GOVERNANCE ONLY: Enable/disable ability of a SetToken to initialize this module. Only callable by governance.
* @param _setToken Instance of the SetToken
* @param _status Bool indicating if _setToken is allowed to initialize this module
*/
function updateAllowedSetToken(ISetToken _setToken, bool _status) external onlyOwner {
require(controller.isSet(address(_setToken)) || allowedSetTokens[_setToken], "Invalid SetToken");
allowedSetTokens[_setToken] = _status;
emit SetTokenStatusUpdated(_setToken, _status);
}
/**
* @dev GOVERNANCE ONLY: Toggle whether ANY SetToken is allowed to initialize this module. Only callable by governance.
* @param _anySetAllowed Bool indicating if ANY SetToken is allowed to initialize this module
*/
function updateAnySetAllowed(bool _anySetAllowed) external onlyOwner {
anySetAllowed = _anySetAllowed;
emit AnySetAllowedUpdated(_anySetAllowed);
}
/**
* @dev MODULE ONLY: Hook called prior to issuance to sync positions on SetToken. Only callable by valid module.
* @param _setToken Instance of the SetToken
*/
function moduleIssueHook(ISetToken _setToken, uint256 /* _setTokenQuantity */) external onlyModule(_setToken) {
sync(_setToken);
}
/**
* @dev MODULE ONLY: Hook called prior to redemption to sync positions on SetToken. For redemption, always use current borrowed
* balance after interest accrual. Only callable by valid module.
* @param _setToken Instance of the SetToken
*/
function moduleRedeemHook(ISetToken _setToken, uint256 /* _setTokenQuantity */) external onlyModule(_setToken) {
sync(_setToken);
}
/**
* @dev MODULE ONLY: Hook called prior to looping through each component on issuance. Invokes borrow in order for
* module to return debt to issuer. Only callable by valid module.
* @param _setToken Instance of the SetToken
* @param _setTokenQuantity Quantity of SetToken
* @param _component Address of component
*/
function componentIssueHook(ISetToken _setToken, uint256 _setTokenQuantity, IERC20 _component, bool /* _isEquity */) external onlyModule(_setToken) {
int256 componentDebt = _setToken.getExternalPositionRealUnit(address(_component), address(this));
require(componentDebt < 0, "Component must be negative");
uint256 notionalDebt = componentDebt.mul(-1).toUint256().preciseMul(_setTokenQuantity);
_borrow(_setToken, _component, notionalDebt);
}
/**
* @dev MODULE ONLY: Hook called prior to looping through each component on redemption. Invokes repay after
* issuance module transfers debt from issuer. Only callable by valid module.
* @param _setToken Instance of the SetToken
* @param _setTokenQuantity Quantity of SetToken
* @param _component Address of component
*/
function componentRedeemHook(ISetToken _setToken, uint256 _setTokenQuantity, IERC20 _component, bool /* _isEquity */) external onlyModule(_setToken) {
int256 componentDebt = _setToken.getExternalPositionRealUnit(address(_component), address(this));
require(componentDebt < 0, "Component must be negative");
uint256 notionalDebt = componentDebt.mul(-1).toUint256().preciseMulCeil(_setTokenQuantity);
_repayBorrow(_setToken, _component, notionalDebt);
}
/* ============ External Getter Functions ============ */
/**
* @dev Get enabled assets for SetToken. Returns an array of collateral and borrow assets.
* @return Underlying collateral assets that are enabled
* @return Underlying borrowed assets that are enabled
*/
function getEnabledAssets(ISetToken _setToken) external view returns(address[] memory, address[] memory) {
return (
enabledAssets[_setToken].collateralAssets,
enabledAssets[_setToken].borrowAssets
);
}
/* ============ Internal Functions ============ */
/**
* @dev Invoke deposit from SetToken using AaveV2 library. Mints aTokens for SetToken.
*/
function _deposit(ISetToken _setToken, IERC20 _asset, uint256 _notionalQuantity) internal {
_setToken.invokeApprove(address(_asset), address(lendingPool), _notionalQuantity);
_setToken.invokeDeposit(lendingPool, address(_asset), _notionalQuantity);
}
/**
* @dev Invoke withdraw from SetToken using AaveV2 library. Burns aTokens and returns underlying to SetToken.
*/
function _withdraw(ISetToken _setToken, IERC20 _asset, uint256 _notionalQuantity) internal {
_setToken.invokeWithdraw(lendingPool, address(_asset), _notionalQuantity);
}
/**
* @dev Invoke repay from SetToken using AaveV2 library. Burns DebtTokens for SetToken.
*/
function _repayBorrow(ISetToken _setToken, IERC20 _asset, uint256 _notionalQuantity) internal {
_setToken.invokeApprove(address(_asset), address(lendingPool), _notionalQuantity);
_setToken.invokeRepay(lendingPool, address(_asset), _notionalQuantity, BORROW_RATE_MODE);
}
/**
* @dev Invoke borrow from the SetToken using AaveV2 library. Mints DebtTokens for SetToken.
*/
function _borrow(ISetToken _setToken, IERC20 _asset, uint256 _notionalQuantity) internal {
_setToken.invokeBorrow(lendingPool, address(_asset), _notionalQuantity, BORROW_RATE_MODE);
}
/**
* @dev Invokes approvals, gets trade call data from exchange adapter and invokes trade from SetToken
* @return uint256 The quantity of tokens received post-trade
*/
function _executeTrade(
ActionInfo memory _actionInfo,
IERC20 _sendToken,
IERC20 _receiveToken,
bytes memory _data
)
internal
returns (uint256)
{
ISetToken setToken = _actionInfo.setToken;
uint256 notionalSendQuantity = _actionInfo.notionalSendQuantity;
setToken.invokeApprove(
address(_sendToken),
_actionInfo.exchangeAdapter.getSpender(),
notionalSendQuantity
);
(
address targetExchange,
uint256 callValue,
bytes memory methodData
) = _actionInfo.exchangeAdapter.getTradeCalldata(
address(_sendToken),
address(_receiveToken),
address(setToken),
notionalSendQuantity,
_actionInfo.minNotionalReceiveQuantity,
_data
);
setToken.invoke(targetExchange, callValue, methodData);
uint256 receiveTokenQuantity = _receiveToken.balanceOf(address(setToken)).sub(_actionInfo.preTradeReceiveTokenBalance);
require(
receiveTokenQuantity >= _actionInfo.minNotionalReceiveQuantity,
"Slippage too high"
);
return receiveTokenQuantity;
}
/**
* @dev Calculates protocol fee on module and pays protocol fee from SetToken
* @return uint256 Total protocol fee paid
*/
function _accrueProtocolFee(ISetToken _setToken, IERC20 _receiveToken, uint256 _exchangedQuantity) internal returns(uint256) {
uint256 protocolFeeTotal = getModuleFee(PROTOCOL_TRADE_FEE_INDEX, _exchangedQuantity);
payProtocolFeeFromSetToken(_setToken, address(_receiveToken), protocolFeeTotal);
return protocolFeeTotal;
}
/**
* @dev Updates the collateral (aToken held) and borrow position (variableDebtToken held) of the SetToken
*/
function _updateLeverPositions(ActionInfo memory actionInfo, IERC20 _borrowAsset) internal {
IAToken aToken = underlyingToReserveTokens[actionInfo.collateralAsset].aToken;
_updateCollateralPosition(
actionInfo.setToken,
aToken,
_getCollateralPosition(
actionInfo.setToken,
aToken,
actionInfo.setTotalSupply
)
);
_updateBorrowPosition(
actionInfo.setToken,
_borrowAsset,
_getBorrowPosition(
actionInfo.setToken,
_borrowAsset,
actionInfo.setTotalSupply
)
);
}
/**
* @dev Updates default position unit for given aToken on SetToken
*/
function _updateCollateralPosition(ISetToken _setToken, IAToken _aToken, uint256 _newPositionUnit) internal {
_setToken.editDefaultPosition(address(_aToken), _newPositionUnit);
}
/**
* @dev Updates external position unit for given borrow asset on SetToken
*/
function _updateBorrowPosition(ISetToken _setToken, IERC20 _underlyingAsset, int256 _newPositionUnit) internal {
_setToken.editExternalPosition(address(_underlyingAsset), address(this), _newPositionUnit, "");
}
/**
* @dev Construct the ActionInfo struct for lever and delever
* @return ActionInfo Instance of constructed ActionInfo struct
*/
function _createAndValidateActionInfo(
ISetToken _setToken,
IERC20 _sendToken,
IERC20 _receiveToken,
uint256 _sendQuantityUnits,
uint256 _minReceiveQuantityUnits,
string memory _tradeAdapterName,
bool _isLever
)
internal
view
returns(ActionInfo memory)
{
uint256 totalSupply = _setToken.totalSupply();
return _createAndValidateActionInfoNotional(
_setToken,
_sendToken,
_receiveToken,
_sendQuantityUnits.preciseMul(totalSupply),
_minReceiveQuantityUnits.preciseMul(totalSupply),
_tradeAdapterName,
_isLever,
totalSupply
);
}
/**
* @dev Construct the ActionInfo struct for lever and delever accepting notional units
* @return ActionInfo Instance of constructed ActionInfo struct
*/
function _createAndValidateActionInfoNotional(
ISetToken _setToken,
IERC20 _sendToken,
IERC20 _receiveToken,
uint256 _notionalSendQuantity,
uint256 _minNotionalReceiveQuantity,
string memory _tradeAdapterName,
bool _isLever,
uint256 _setTotalSupply
)
internal
view
returns(ActionInfo memory)
{
ActionInfo memory actionInfo = ActionInfo ({
exchangeAdapter: IExchangeAdapter(getAndValidateAdapter(_tradeAdapterName)),
setToken: _setToken,
collateralAsset: _isLever ? _receiveToken : _sendToken,
borrowAsset: _isLever ? _sendToken : _receiveToken,
setTotalSupply: _setTotalSupply,
notionalSendQuantity: _notionalSendQuantity,
minNotionalReceiveQuantity: _minNotionalReceiveQuantity,
preTradeReceiveTokenBalance: IERC20(_receiveToken).balanceOf(address(_setToken))
});
_validateCommon(actionInfo);
return actionInfo;
}
/**
* @dev Updates `underlyingToReserveTokens` mappings for given `_underlying` asset. Emits ReserveTokensUpdated event.
*/
function _addUnderlyingToReserveTokensMapping(IERC20 _underlying) internal {
(address aToken, , address variableDebtToken) = protocolDataProvider.getReserveTokensAddresses(address(_underlying));
underlyingToReserveTokens[_underlying].aToken = IAToken(aToken);
underlyingToReserveTokens[_underlying].variableDebtToken = IVariableDebtToken(variableDebtToken);
emit ReserveTokensUpdated(_underlying, IAToken(aToken), IVariableDebtToken(variableDebtToken));
}
/**
* @dev Add collateral assets to SetToken. Updates the collateralAssetsEnabled and enabledAssets mappings.
* Emits CollateralAssetsUpdated event.
*/
function _addCollateralAssets(ISetToken _setToken, IERC20[] memory _newCollateralAssets) internal {
for(uint256 i = 0; i < _newCollateralAssets.length; i++) {
IERC20 collateralAsset = _newCollateralAssets[i];
_validateNewCollateralAsset(_setToken, collateralAsset);
_updateUseReserveAsCollateral(_setToken, collateralAsset, true);
collateralAssetEnabled[_setToken][collateralAsset] = true;
enabledAssets[_setToken].collateralAssets.push(address(collateralAsset));
}
emit CollateralAssetsUpdated(_setToken, true, _newCollateralAssets);
}
/**
* @dev Add borrow assets to SetToken. Updates the borrowAssetsEnabled and enabledAssets mappings.
* Emits BorrowAssetsUpdated event.
*/
function _addBorrowAssets(ISetToken _setToken, IERC20[] memory _newBorrowAssets) internal {
for(uint256 i = 0; i < _newBorrowAssets.length; i++) {
IERC20 borrowAsset = _newBorrowAssets[i];
_validateNewBorrowAsset(_setToken, borrowAsset);
borrowAssetEnabled[_setToken][borrowAsset] = true;
enabledAssets[_setToken].borrowAssets.push(address(borrowAsset));
}
emit BorrowAssetsUpdated(_setToken, true, _newBorrowAssets);
}
/**
* @dev Updates SetToken's ability to use an asset as collateral on Aave
*/
function _updateUseReserveAsCollateral(ISetToken _setToken, IERC20 _asset, bool _useAsCollateral) internal {
/*
Note: Aave ENABLES an asset to be used as collateral by `to` address in an `aToken.transfer(to, amount)` call provided
1. msg.sender (from address) isn't the same as `to` address
2. `to` address had zero aToken balance before the transfer
3. transfer `amount` is greater than 0
Note: Aave DISABLES an asset to be used as collateral by `msg.sender`in an `aToken.transfer(to, amount)` call provided
1. msg.sender (from address) isn't the same as `to` address
2. msg.sender has zero balance after the transfer
Different states of the SetToken and what this function does in those states:
Case 1: Manager adds collateral asset to SetToken before first issuance
- Since aToken.balanceOf(setToken) == 0, we do not call `setToken.invokeUserUseReserveAsCollateral` because Aave
requires aToken balance to be greater than 0 before enabling/disabling the underlying asset to be used as collateral
on Aave markets.
Case 2: First issuance of the SetToken
- SetToken was initialized with aToken as default position
- DebtIssuanceModule reads the default position and transfers corresponding aToken from the issuer to the SetToken
- Aave enables aToken to be used as collateral by the SetToken
- Manager calls lever() and the aToken is used as collateral to borrow other assets
Case 3: Manager removes collateral asset from the SetToken
- Disable asset to be used as collateral on SetToken by calling `setToken.invokeSetUserUseReserveAsCollateral` with
useAsCollateral equals false
- Note: If health factor goes below 1 by removing the collateral asset, then Aave reverts on the above call, thus whole
transaction reverts, and manager can't remove corresponding collateral asset