Skip to content

Commit 317dfb6

Browse files
authored
feat: aave v3 leverage strategy extension
* Add contract interfaces * Inherit from AaveLeverageStrategyExtension * Implement setEModeCategory endpoint * Add tests for strategy extension * Fix first tests * Fix some engage tests * Add rebalance test * Switch to using tradeAdapterMock * iterateRebalance method * ripcord tests * more tests * more tests * all legacy tests * Move aaveV3 test to integration test folder * Don't run aaveV3StrategyExtension test in normal test mode * Fix integration tests * Fix lint failure * Add test for setEModeCategory endpoint * PR Feedback
1 parent add917b commit 317dfb6

28 files changed

+7621
-50
lines changed

.solhint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extends": "solhint:recommended",
33
"rules": {
44
"reason-string": ["warn", { "maxLength": 50 }],
5-
"compiler-version": ["error", "0.6.10"]
5+
"compiler-version": ["error", ">=0.6.10"]
66
}
77
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2023 Index Coop
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
pragma experimental ABIEncoderV2;
21+
22+
import {AaveLeverageStrategyExtension} from "./AaveLeverageStrategyExtension.sol";
23+
24+
import {IBaseManager} from "../interfaces/IBaseManager.sol";
25+
26+
/**
27+
* @title AaveV3LeverageStrategyExtension
28+
* @author Index Coop
29+
*
30+
* Extension of AaveLeverageStrategyExtension to add endpoint for setting the eMode categoryId
31+
*
32+
*/
33+
contract AaveV3LeverageStrategyExtension is AaveLeverageStrategyExtension {
34+
constructor(
35+
IBaseManager _manager,
36+
ContractSettings memory _strategy,
37+
MethodologySettings memory _methodology,
38+
ExecutionSettings memory _execution,
39+
IncentiveSettings memory _incentive,
40+
string[] memory _exchangeNames,
41+
ExchangeSettings[] memory _exchangeSettings
42+
)
43+
public
44+
AaveLeverageStrategyExtension(
45+
_manager,
46+
_strategy,
47+
_methodology,
48+
_execution,
49+
_incentive,
50+
_exchangeNames,
51+
_exchangeSettings
52+
)
53+
{}
54+
55+
/**
56+
* OPERATOR ONLY: Set eMode categoryId to new value
57+
*
58+
* @param _categoryId eMode categoryId as defined on aaveV3
59+
*/
60+
function setEModeCategory(uint8 _categoryId) external onlyOperator {
61+
_setEModeCategory(_categoryId);
62+
}
63+
64+
function _setEModeCategory(uint8 _categoryId) internal {
65+
bytes memory setEmodeCallData =
66+
abi.encodeWithSignature("setEModeCategory(address,uint8)", address(strategy.setToken), _categoryId);
67+
invokeManager(address(strategy.leverageModule), setEmodeCallData);
68+
}
69+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: BUSL-1.1
2+
pragma solidity 0.6.10;
3+
4+
library ConfiguratorInputTypes {
5+
struct InitReserveInput {
6+
address aTokenImpl;
7+
address stableDebtTokenImpl;
8+
address variableDebtTokenImpl;
9+
uint8 underlyingAssetDecimals;
10+
address interestRateStrategyAddress;
11+
address underlyingAsset;
12+
address treasury;
13+
address incentivesController;
14+
string aTokenName;
15+
string aTokenSymbol;
16+
string variableDebtTokenName;
17+
string variableDebtTokenSymbol;
18+
string stableDebtTokenName;
19+
string stableDebtTokenSymbol;
20+
bytes params;
21+
}
22+
23+
struct UpdateATokenInput {
24+
address asset;
25+
address treasury;
26+
address incentivesController;
27+
string name;
28+
string symbol;
29+
address implementation;
30+
bytes params;
31+
}
32+
33+
struct UpdateDebtTokenInput {
34+
address asset;
35+
address incentivesController;
36+
string name;
37+
string symbol;
38+
address implementation;
39+
bytes params;
40+
}
41+
}

contracts/interfaces/Datatypes.sol

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
// SPDX-License-Identifier: BUSL-1.1
2+
pragma solidity 0.6.10;
3+
4+
library DataTypes {
5+
struct ReserveData {
6+
//stores the reserve configuration
7+
ReserveConfigurationMap configuration;
8+
//the liquidity index. Expressed in ray
9+
uint128 liquidityIndex;
10+
//the current supply rate. Expressed in ray
11+
uint128 currentLiquidityRate;
12+
//variable borrow index. Expressed in ray
13+
uint128 variableBorrowIndex;
14+
//the current variable borrow rate. Expressed in ray
15+
uint128 currentVariableBorrowRate;
16+
//the current stable borrow rate. Expressed in ray
17+
uint128 currentStableBorrowRate;
18+
//timestamp of last update
19+
uint40 lastUpdateTimestamp;
20+
//the id of the reserve. Represents the position in the list of the active reserves
21+
uint16 id;
22+
//aToken address
23+
address aTokenAddress;
24+
//stableDebtToken address
25+
address stableDebtTokenAddress;
26+
//variableDebtToken address
27+
address variableDebtTokenAddress;
28+
//address of the interest rate strategy
29+
address interestRateStrategyAddress;
30+
//the current treasury balance, scaled
31+
uint128 accruedToTreasury;
32+
//the outstanding unbacked aTokens minted through the bridging feature
33+
uint128 unbacked;
34+
//the outstanding debt borrowed against this asset in isolation mode
35+
uint128 isolationModeTotalDebt;
36+
}
37+
38+
struct ReserveConfigurationMap {
39+
//bit 0-15: LTV
40+
//bit 16-31: Liq. threshold
41+
//bit 32-47: Liq. bonus
42+
//bit 48-55: Decimals
43+
//bit 56: reserve is active
44+
//bit 57: reserve is frozen
45+
//bit 58: borrowing is enabled
46+
//bit 59: stable rate borrowing enabled
47+
//bit 60: asset is paused
48+
//bit 61: borrowing in isolation mode is enabled
49+
//bit 62-63: reserved
50+
//bit 64-79: reserve factor
51+
//bit 80-115 borrow cap in whole tokens, borrowCap == 0 => no cap
52+
//bit 116-151 supply cap in whole tokens, supplyCap == 0 => no cap
53+
//bit 152-167 liquidation protocol fee
54+
//bit 168-175 eMode category
55+
//bit 176-211 unbacked mint cap in whole tokens, unbackedMintCap == 0 => minting disabled
56+
//bit 212-251 debt ceiling for isolation mode with (ReserveConfiguration::DEBT_CEILING_DECIMALS) decimals
57+
//bit 252-255 unused
58+
59+
uint256 data;
60+
}
61+
62+
struct UserConfigurationMap {
63+
/**
64+
* @dev Bitmap of the users collaterals and borrows. It is divided in pairs of bits, one pair per asset.
65+
* The first bit indicates if an asset is used as collateral by the user, the second whether an
66+
* asset is borrowed by the user.
67+
*/
68+
uint256 data;
69+
}
70+
71+
struct EModeCategory {
72+
// each eMode category has a custom ltv and liquidation threshold
73+
uint16 ltv;
74+
uint16 liquidationThreshold;
75+
uint16 liquidationBonus;
76+
// each eMode category may or may not have a custom oracle to override the individual assets price oracles
77+
address priceSource;
78+
string label;
79+
}
80+
81+
enum InterestRateMode {
82+
NONE,
83+
STABLE,
84+
VARIABLE
85+
}
86+
87+
struct ReserveCache {
88+
uint256 currScaledVariableDebt;
89+
uint256 nextScaledVariableDebt;
90+
uint256 currPrincipalStableDebt;
91+
uint256 currAvgStableBorrowRate;
92+
uint256 currTotalStableDebt;
93+
uint256 nextAvgStableBorrowRate;
94+
uint256 nextTotalStableDebt;
95+
uint256 currLiquidityIndex;
96+
uint256 nextLiquidityIndex;
97+
uint256 currVariableBorrowIndex;
98+
uint256 nextVariableBorrowIndex;
99+
uint256 currLiquidityRate;
100+
uint256 currVariableBorrowRate;
101+
uint256 reserveFactor;
102+
ReserveConfigurationMap reserveConfiguration;
103+
address aTokenAddress;
104+
address stableDebtTokenAddress;
105+
address variableDebtTokenAddress;
106+
uint40 reserveLastUpdateTimestamp;
107+
uint40 stableDebtLastUpdateTimestamp;
108+
}
109+
110+
struct ExecuteLiquidationCallParams {
111+
uint256 reservesCount;
112+
uint256 debtToCover;
113+
address collateralAsset;
114+
address debtAsset;
115+
address user;
116+
bool receiveAToken;
117+
address priceOracle;
118+
uint8 userEModeCategory;
119+
address priceOracleSentinel;
120+
}
121+
122+
struct ExecuteSupplyParams {
123+
address asset;
124+
uint256 amount;
125+
address onBehalfOf;
126+
uint16 referralCode;
127+
}
128+
129+
struct ExecuteBorrowParams {
130+
address asset;
131+
address user;
132+
address onBehalfOf;
133+
uint256 amount;
134+
InterestRateMode interestRateMode;
135+
uint16 referralCode;
136+
bool releaseUnderlying;
137+
uint256 maxStableRateBorrowSizePercent;
138+
uint256 reservesCount;
139+
address oracle;
140+
uint8 userEModeCategory;
141+
address priceOracleSentinel;
142+
}
143+
144+
struct ExecuteRepayParams {
145+
address asset;
146+
uint256 amount;
147+
InterestRateMode interestRateMode;
148+
address onBehalfOf;
149+
bool useATokens;
150+
}
151+
152+
struct ExecuteWithdrawParams {
153+
address asset;
154+
uint256 amount;
155+
address to;
156+
uint256 reservesCount;
157+
address oracle;
158+
uint8 userEModeCategory;
159+
}
160+
161+
struct ExecuteSetUserEModeParams {
162+
uint256 reservesCount;
163+
address oracle;
164+
uint8 categoryId;
165+
}
166+
167+
struct FinalizeTransferParams {
168+
address asset;
169+
address from;
170+
address to;
171+
uint256 amount;
172+
uint256 balanceFromBefore;
173+
uint256 balanceToBefore;
174+
uint256 reservesCount;
175+
address oracle;
176+
uint8 fromEModeCategory;
177+
}
178+
179+
struct FlashloanParams {
180+
address receiverAddress;
181+
address[] assets;
182+
uint256[] amounts;
183+
uint256[] interestRateModes;
184+
address onBehalfOf;
185+
bytes params;
186+
uint16 referralCode;
187+
uint256 flashLoanPremiumToProtocol;
188+
uint256 flashLoanPremiumTotal;
189+
uint256 maxStableRateBorrowSizePercent;
190+
uint256 reservesCount;
191+
address addressesProvider;
192+
uint8 userEModeCategory;
193+
bool isAuthorizedFlashBorrower;
194+
}
195+
196+
struct FlashloanSimpleParams {
197+
address receiverAddress;
198+
address asset;
199+
uint256 amount;
200+
bytes params;
201+
uint16 referralCode;
202+
uint256 flashLoanPremiumToProtocol;
203+
uint256 flashLoanPremiumTotal;
204+
}
205+
206+
struct FlashLoanRepaymentParams {
207+
uint256 amount;
208+
uint256 totalPremium;
209+
uint256 flashLoanPremiumToProtocol;
210+
address asset;
211+
address receiverAddress;
212+
uint16 referralCode;
213+
}
214+
215+
struct CalculateUserAccountDataParams {
216+
UserConfigurationMap userConfig;
217+
uint256 reservesCount;
218+
address user;
219+
address oracle;
220+
uint8 userEModeCategory;
221+
}
222+
223+
struct ValidateBorrowParams {
224+
ReserveCache reserveCache;
225+
UserConfigurationMap userConfig;
226+
address asset;
227+
address userAddress;
228+
uint256 amount;
229+
InterestRateMode interestRateMode;
230+
uint256 maxStableLoanPercent;
231+
uint256 reservesCount;
232+
address oracle;
233+
uint8 userEModeCategory;
234+
address priceOracleSentinel;
235+
bool isolationModeActive;
236+
address isolationModeCollateralAddress;
237+
uint256 isolationModeDebtCeiling;
238+
}
239+
240+
struct ValidateLiquidationCallParams {
241+
ReserveCache debtReserveCache;
242+
uint256 totalDebt;
243+
uint256 healthFactor;
244+
address priceOracleSentinel;
245+
}
246+
247+
struct CalculateInterestRatesParams {
248+
uint256 unbacked;
249+
uint256 liquidityAdded;
250+
uint256 liquidityTaken;
251+
uint256 totalStableDebt;
252+
uint256 totalVariableDebt;
253+
uint256 averageStableBorrowRate;
254+
uint256 reserveFactor;
255+
address reserve;
256+
address aToken;
257+
}
258+
259+
struct InitReserveParams {
260+
address asset;
261+
address aTokenAddress;
262+
address stableDebtAddress;
263+
address variableDebtAddress;
264+
address interestRateStrategyAddress;
265+
uint16 reservesCount;
266+
uint16 maxNumberReserves;
267+
}
268+
}

0 commit comments

Comments
 (0)