@@ -122,7 +122,6 @@ describe("AaveLeverageModule", () => {
122
122
aaveLeverageModule = await deployer . modules . deployAaveLeverageModule (
123
123
setup . controller . address ,
124
124
aaveSetup . lendingPoolAddressesProvider . address ,
125
- aaveSetup . protocolDataProvider . address ,
126
125
"contracts/protocol/integration/lib/AaveV2.sol:AaveV2" ,
127
126
aaveV2Library . address ,
128
127
) ;
@@ -221,19 +220,16 @@ describe("AaveLeverageModule", () => {
221
220
describe ( "#constructor" , async ( ) => {
222
221
let subjectController : Address ;
223
222
let subjectLendingPoolAddressesProvider : Address ;
224
- let subjectProtocolDataProvider : Address ;
225
223
226
224
beforeEach ( async ( ) => {
227
225
subjectController = setup . controller . address ;
228
226
subjectLendingPoolAddressesProvider = aaveSetup . lendingPoolAddressesProvider . address ;
229
- subjectProtocolDataProvider = aaveSetup . protocolDataProvider . address ;
230
227
} ) ;
231
228
232
229
async function subject ( ) : Promise < AaveLeverageModule > {
233
230
return deployer . modules . deployAaveLeverageModule (
234
231
subjectController ,
235
232
subjectLendingPoolAddressesProvider ,
236
- subjectProtocolDataProvider ,
237
233
"contracts/protocol/integration/lib/AaveV2.sol:AaveV2" ,
238
234
aaveV2Library . address
239
235
) ;
@@ -249,11 +245,9 @@ describe("AaveLeverageModule", () => {
249
245
it ( "should set the correct Aave contracts" , async ( ) => {
250
246
const aaveLeverageModule = await subject ( ) ;
251
247
252
- const lendingPool = await aaveLeverageModule . lendingPool ( ) ;
253
248
const lendingPoolAddressesProvider = await aaveLeverageModule . lendingPoolAddressesProvider ( ) ;
254
249
const protocolDataProvider = await aaveLeverageModule . protocolDataProvider ( ) ;
255
250
256
- expect ( lendingPool ) . to . eq ( aaveSetup . lendingPool . address ) ;
257
251
expect ( lendingPoolAddressesProvider ) . to . eq ( aaveSetup . lendingPoolAddressesProvider . address ) ;
258
252
expect ( protocolDataProvider ) . to . eq ( aaveSetup . protocolDataProvider . address ) ;
259
253
} ) ;
@@ -1319,6 +1313,89 @@ describe("AaveLeverageModule", () => {
1319
1313
} ) ;
1320
1314
} ) ;
1321
1315
1316
+ describe ( "when used to delever to zero" , async ( ) => {
1317
+
1318
+ beforeEach ( async ( ) => {
1319
+ subjectMinRepayQuantity = ether ( 1001 ) ;
1320
+ await oneInchExchangeMockFromWeth . updateReceiveAmount ( subjectMinRepayQuantity ) ;
1321
+
1322
+ subjectTradeData = oneInchExchangeMockFromWeth . interface . encodeFunctionData ( "swap" , [
1323
+ setup . weth . address , // Send token
1324
+ setup . dai . address , // Receive token
1325
+ subjectRedeemQuantity , // Send quantity
1326
+ subjectMinRepayQuantity , // Min receive quantity
1327
+ ZERO ,
1328
+ ADDRESS_ZERO ,
1329
+ [ ADDRESS_ZERO ] ,
1330
+ EMPTY_BYTES ,
1331
+ [ ZERO ] ,
1332
+ [ ZERO ] ,
1333
+ ] ) ;
1334
+ } ) ;
1335
+
1336
+ it ( "should transfer the correct components to the exchange" , async ( ) => {
1337
+ const oldSourceTokenBalance = await setup . weth . balanceOf ( oneInchExchangeMockFromWeth . address ) ;
1338
+
1339
+ await subject ( ) ;
1340
+ const totalSourceQuantity = subjectRedeemQuantity ;
1341
+ const expectedSourceTokenBalance = oldSourceTokenBalance . add ( totalSourceQuantity ) ;
1342
+ const newSourceTokenBalance = await setup . weth . balanceOf ( oneInchExchangeMockFromWeth . address ) ;
1343
+ expect ( newSourceTokenBalance ) . to . eq ( expectedSourceTokenBalance ) ;
1344
+ } ) ;
1345
+
1346
+ it ( "should update the collateral position on the SetToken correctly" , async ( ) => {
1347
+ const initialPositions = await setToken . getPositions ( ) ;
1348
+
1349
+ await subject ( ) ;
1350
+
1351
+ const currentPositions = await setToken . getPositions ( ) ;
1352
+ const newFirstPosition = ( await setToken . getPositions ( ) ) [ 0 ] ;
1353
+
1354
+ const newUnits = subjectRedeemQuantity ;
1355
+ const expectedFirstPositionUnit = initialPositions [ 0 ] . unit . sub ( newUnits ) ;
1356
+
1357
+ expect ( initialPositions . length ) . to . eq ( 2 ) ;
1358
+ expect ( currentPositions . length ) . to . eq ( 2 ) ;
1359
+ expect ( newFirstPosition . component ) . to . eq ( aWETH . address ) ;
1360
+ expect ( newFirstPosition . positionState ) . to . eq ( 0 ) ; // Default
1361
+ expect ( newFirstPosition . unit ) . to . eq ( expectedFirstPositionUnit ) ;
1362
+ expect ( newFirstPosition . module ) . to . eq ( ADDRESS_ZERO ) ;
1363
+ } ) ;
1364
+
1365
+ it ( "should update the borrow position on the SetToken correctly" , async ( ) => {
1366
+ const initialPositions = await setToken . getPositions ( ) ;
1367
+
1368
+ await subject ( ) ;
1369
+
1370
+ const currentPositions = await setToken . getPositions ( ) ;
1371
+ const newSecondPosition = ( await setToken . getPositions ( ) ) [ 1 ] ;
1372
+
1373
+ const expectedSecondPositionUnit = await setup . dai . balanceOf ( setToken . address ) ;
1374
+
1375
+ expect ( initialPositions . length ) . to . eq ( 2 ) ;
1376
+ expect ( currentPositions . length ) . to . eq ( 2 ) ;
1377
+ expect ( newSecondPosition . component ) . to . eq ( setup . dai . address ) ;
1378
+ expect ( newSecondPosition . positionState ) . to . eq ( 0 ) ; // Default since we traded for more Dai than outstannding debt
1379
+ expect ( newSecondPosition . unit ) . to . eq ( expectedSecondPositionUnit ) ;
1380
+ expect ( newSecondPosition . module ) . to . eq ( ADDRESS_ZERO ) ;
1381
+ } ) ;
1382
+
1383
+ it ( "should emit the correct LeverageDecreased event" , async ( ) => {
1384
+ const totalCollateralQuantity = subjectRedeemQuantity ;
1385
+ const totalRepayQuantity = subjectMinRepayQuantity ;
1386
+
1387
+ await expect ( subject ( ) ) . to . emit ( aaveLeverageModule , "LeverageDecreased" ) . withArgs (
1388
+ setToken . address ,
1389
+ subjectCollateralAsset ,
1390
+ subjectRepayAsset ,
1391
+ oneInchExchangeAdapterFromWeth . address ,
1392
+ totalCollateralQuantity ,
1393
+ totalRepayQuantity ,
1394
+ ZERO
1395
+ ) ;
1396
+ } ) ;
1397
+ } ) ;
1398
+
1322
1399
describe ( "when the exchange is not valid" , async ( ) => {
1323
1400
beforeEach ( async ( ) => {
1324
1401
subjectTradeAdapterName = "UNISWAP" ;
@@ -3052,7 +3129,7 @@ describe("AaveLeverageModule", () => {
3052
3129
subjectSetToken = setToken . address ;
3053
3130
subjectSetQuantity = issueQuantity ;
3054
3131
subjectComponent = setup . dai . address ;
3055
- subjectIsEquity = true ; // Unused by module
3132
+ subjectIsEquity = false ;
3056
3133
subjectCaller = mockModule ;
3057
3134
} ) ;
3058
3135
@@ -3076,7 +3153,7 @@ describe("AaveLeverageModule", () => {
3076
3153
expect ( currentDaiBalance ) . to . eq ( preciseMul ( borrowQuantity , subjectSetQuantity ) ) ;
3077
3154
} ) ;
3078
3155
3079
- describe ( "when component has positive unit" , async ( ) => {
3156
+ describe ( "when isEquity is false and component has positive unit (should not happen) " , async ( ) => {
3080
3157
beforeEach ( async ( ) => {
3081
3158
subjectComponent = aWETH . address ;
3082
3159
} ) ;
@@ -3086,6 +3163,23 @@ describe("AaveLeverageModule", () => {
3086
3163
} ) ;
3087
3164
} ) ;
3088
3165
3166
+ describe ( "when isEquity is true" , async ( ) => {
3167
+ beforeEach ( async ( ) => {
3168
+ subjectIsEquity = true ;
3169
+ } ) ;
3170
+
3171
+ it ( "should NOT increase borrowed quantity on the SetToken" , async ( ) => {
3172
+ const previousDaiBalance = await setup . dai . balanceOf ( setToken . address ) ;
3173
+
3174
+ await subject ( ) ;
3175
+
3176
+ const currentDaiBalance = await setup . dai . balanceOf ( setToken . address ) ;
3177
+
3178
+ expect ( previousDaiBalance ) . to . eq ( ZERO ) ;
3179
+ expect ( currentDaiBalance ) . to . eq ( ZERO ) ;
3180
+ } ) ;
3181
+ } ) ;
3182
+
3089
3183
describe ( "when caller is not module" , async ( ) => {
3090
3184
beforeEach ( async ( ) => {
3091
3185
subjectCaller = owner ;
@@ -3203,7 +3297,7 @@ describe("AaveLeverageModule", () => {
3203
3297
subjectSetToken = setToken . address ;
3204
3298
subjectSetQuantity = issueQuantity ;
3205
3299
subjectComponent = setup . dai . address ;
3206
- subjectIsEquity = true ; // Unused by module
3300
+ subjectIsEquity = false ;
3207
3301
subjectCaller = mockModule ;
3208
3302
} ) ;
3209
3303
@@ -3227,7 +3321,7 @@ describe("AaveLeverageModule", () => {
3227
3321
expect ( currentDaiBalance ) . to . eq ( ZERO ) ;
3228
3322
} ) ;
3229
3323
3230
- describe ( "when component has positive unit" , async ( ) => {
3324
+ describe ( "when _isEquity is false and component has positive unit" , async ( ) => {
3231
3325
beforeEach ( async ( ) => {
3232
3326
subjectComponent = aWETH . address ;
3233
3327
} ) ;
@@ -3237,6 +3331,23 @@ describe("AaveLeverageModule", () => {
3237
3331
} ) ;
3238
3332
} ) ;
3239
3333
3334
+ describe ( "when isEquity is true" , async ( ) => {
3335
+ beforeEach ( async ( ) => {
3336
+ subjectIsEquity = true ;
3337
+ } ) ;
3338
+
3339
+ it ( "should NOT decrease borrowed quantity on the SetToken" , async ( ) => {
3340
+ const previousDaiBalance = await setup . dai . balanceOf ( setToken . address ) ;
3341
+
3342
+ await subject ( ) ;
3343
+
3344
+ const currentDaiBalance = await setup . dai . balanceOf ( setToken . address ) ;
3345
+
3346
+ expect ( previousDaiBalance ) . to . eq ( repayQuantity ) ;
3347
+ expect ( currentDaiBalance ) . to . eq ( repayQuantity ) ;
3348
+ } ) ;
3349
+ } ) ;
3350
+
3240
3351
describe ( "when caller is not module" , async ( ) => {
3241
3352
beforeEach ( async ( ) => {
3242
3353
subjectCaller = owner ;
0 commit comments