@@ -129,11 +129,6 @@ if (process.env.INTEGRATIONTEST) {
129
129
await weth . transfer ( owner . address , ether ( 100 ) ) ;
130
130
await aweth . transfer ( owner . address , ether ( 100 ) ) ;
131
131
132
- // This is done to avoid flaky "Invalid transfer in, results in undercollateralization" error
133
- // See: https://github.com/IndexCoop/index-protocol/blob/1a587d93d273d9004d03f1235c395f6f7cd147dc/test/protocol/modules/v1/debtIssuanceModuleV2.spec.ts#L730
134
- // TODO: Review if we have to do this in production.
135
- await aweth . transfer ( setToken . address , ether ( 0.000001 ) ) ;
136
-
137
132
await aweth
138
133
. connect ( owner . wallet )
139
134
. approve ( addresses . setFork . debtIssuanceModuleV2 , ether ( 10 ) ) ;
@@ -191,10 +186,10 @@ if (process.env.INTEGRATIONTEST) {
191
186
) . to . equal ( MAX_UINT_256 ) ;
192
187
} ) ;
193
188
194
- [ "collateralToken" , " USDC", "ETH" ] . forEach ( inputTokenName => {
189
+ [ "USDC" , "ETH" ] . forEach ( inputTokenName => {
195
190
describe ( `When input/output token is ${ inputTokenName } ` , ( ) => {
196
191
let amountIn : BigNumber ;
197
- beforeEach ( async ( ) => {
192
+ before ( async ( ) => {
198
193
amountIn = ether ( 0.4 ) ;
199
194
if ( inputTokenName === "USDC" ) {
200
195
amountIn = utils . parseUnits ( "2500" , 6 ) ;
@@ -220,8 +215,11 @@ if (process.env.INTEGRATIONTEST) {
220
215
let subjectSetToken : Address ;
221
216
let subjectMaxAmountIn : BigNumber ;
222
217
let subjectInputToken : Address ;
218
+ let setBalancebefore : BigNumber ;
219
+ let inputBalanceBefore : BigNumber ;
220
+ let quotedInputAmount : BigNumber ;
223
221
224
- beforeEach ( async ( ) => {
222
+ before ( async ( ) => {
225
223
subjectSetAmount = ether ( 1 ) ;
226
224
swapDataDebtToCollateral = {
227
225
path : [ addresses . tokens . USDC , addresses . tokens . weth ] ,
@@ -255,6 +253,13 @@ if (process.env.INTEGRATIONTEST) {
255
253
subjectInputToken = inputToken . address ;
256
254
}
257
255
subjectSetToken = setToken . address ;
256
+ setBalancebefore = await setToken . balanceOf ( owner . address ) ;
257
+ inputBalanceBefore =
258
+ inputTokenName === "ETH"
259
+ ? await owner . wallet . getBalance ( )
260
+ : await inputToken . balanceOf ( owner . address ) ;
261
+ quotedInputAmount = await subjectQuote ( ) ;
262
+ await subject ( ) ;
258
263
} ) ;
259
264
260
265
async function subject ( ) {
@@ -287,35 +292,22 @@ if (process.env.INTEGRATIONTEST) {
287
292
}
288
293
289
294
it ( "should issue the correct amount of tokens" , async ( ) => {
290
- const setBalancebefore = await setToken . balanceOf ( owner . address ) ;
291
- await subject ( ) ;
292
295
const setBalanceAfter = await setToken . balanceOf ( owner . address ) ;
293
296
const setObtained = setBalanceAfter . sub ( setBalancebefore ) ;
294
297
expect ( setObtained ) . to . eq ( subjectSetAmount ) ;
295
298
} ) ;
296
299
297
300
it ( "should spend less than specified max amount" , async ( ) => {
298
- const inputBalanceBefore =
299
- inputTokenName === "ETH"
300
- ? await owner . wallet . getBalance ( )
301
- : await inputToken . balanceOf ( owner . address ) ;
302
- await subject ( ) ;
303
301
const inputBalanceAfter =
304
302
inputTokenName === "ETH"
305
303
? await owner . wallet . getBalance ( )
306
304
: await inputToken . balanceOf ( owner . address ) ;
307
305
const inputSpent = inputBalanceBefore . sub ( inputBalanceAfter ) ;
308
- expect ( inputSpent . gt ( 0 ) ) . to . be . true ;
309
- expect ( inputSpent . lte ( subjectMaxAmountIn ) ) . to . be . true ;
306
+ expect ( inputSpent ) . to . be . gt ( 0 ) ;
307
+ expect ( inputSpent ) . to . be . lte ( subjectMaxAmountIn ) ;
310
308
} ) ;
311
309
312
310
it ( "should quote the correct input amount" , async ( ) => {
313
- const quotedInputAmount = await subjectQuote ( ) ;
314
- const inputBalanceBefore =
315
- inputTokenName === "ETH"
316
- ? await owner . wallet . getBalance ( )
317
- : await inputToken . balanceOf ( owner . address ) ;
318
- await subject ( ) ;
319
311
const inputBalanceAfter =
320
312
inputTokenName === "ETH"
321
313
? await owner . wallet . getBalance ( )
@@ -344,7 +336,12 @@ if (process.env.INTEGRATIONTEST) {
344
336
let subjectPriceEstimateInflater : BigNumber ;
345
337
let subjectMaxDust : BigNumber ;
346
338
347
- beforeEach ( async ( ) => {
339
+ let setBalancebefore : BigNumber ;
340
+ let ethBalanceBefore : BigNumber ;
341
+ let gasCosts : BigNumber ;
342
+ let inputBalanceBefore : BigNumber ;
343
+
344
+ before ( async ( ) => {
348
345
swapDataDebtToCollateral = {
349
346
path : [ addresses . tokens . USDC , addresses . tokens . weth ] ,
350
347
fees : [ 500 ] ,
@@ -379,6 +376,15 @@ if (process.env.INTEGRATIONTEST) {
379
376
subjectMinSetAmount = ether ( 1 ) ;
380
377
subjectSetToken = setToken . address ;
381
378
swapDataInputTokenToETH = swapDataInputTokenToCollateral ; // Assumes Collateral Token is WETH
379
+ setBalancebefore = await setToken . balanceOf ( owner . address ) ;
380
+ ethBalanceBefore = await owner . wallet . getBalance ( ) ;
381
+ inputBalanceBefore =
382
+ inputTokenName === "ETH"
383
+ ? await owner . wallet . getBalance ( )
384
+ : await inputToken . balanceOf ( owner . address ) ;
385
+ const tx = await subject ( ) ;
386
+ const receipt = await tx . wait ( ) ;
387
+ gasCosts = receipt . gasUsed . mul ( tx . gasPrice ) ;
382
388
} ) ;
383
389
384
390
async function subject ( ) {
@@ -407,34 +413,19 @@ if (process.env.INTEGRATIONTEST) {
407
413
}
408
414
409
415
it ( "should issue at least minSetAmount of set tokens" , async ( ) => {
410
- const setBalancebefore = await setToken . balanceOf ( owner . address ) ;
411
- await subject ( ) ;
412
416
const setBalanceAfter = await setToken . balanceOf ( owner . address ) ;
413
417
const setObtained = setBalanceAfter . sub ( setBalancebefore ) ;
414
418
expect ( setObtained ) . to . gte ( subjectMinSetAmount ) ;
415
419
} ) ;
416
420
417
421
if ( inputTokenName !== "ETH" ) {
418
422
it ( "should give gas rebaste" , async ( ) => {
419
- const ethBalanceBefore = await owner . wallet . getBalance ( ) ;
420
- const tx = await subject ( ) ;
421
- const receipt = await tx . wait ( ) ;
422
- const gasCosts = receipt . gasUsed . mul ( tx . gasPrice ) ;
423
423
const ethBalanceAfter = await owner . wallet . getBalance ( ) ;
424
424
expect ( ethBalanceBefore . sub ( ethBalanceAfter ) ) . to . lt ( gasCosts ) ;
425
425
} ) ;
426
426
}
427
427
428
428
it ( "should spend exactly inputAmount" , async ( ) => {
429
- const inputBalanceBefore =
430
- inputTokenName === "ETH"
431
- ? await owner . wallet . getBalance ( )
432
- : await inputToken . balanceOf ( owner . address ) ;
433
-
434
- const tx = await subject ( ) ;
435
- const receipt = await tx . wait ( ) ;
436
- const gasCosts = receipt . gasUsed . mul ( tx . gasPrice ) ;
437
-
438
429
const inputBalanceAfter =
439
430
inputTokenName === "ETH"
440
431
? await owner . wallet . getBalance ( )
@@ -468,6 +459,10 @@ if (process.env.INTEGRATIONTEST) {
468
459
let subjectOutputToken : Address ;
469
460
let subjectPriceEstimateInflater : BigNumber ;
470
461
let subjectMaxDust : BigNumber ;
462
+ let setBalanceBefore : BigNumber ;
463
+ let ethBalanceBefore : BigNumber ;
464
+ let outputBalanceBefore : BigNumber ;
465
+ let gasCosts : BigNumber ;
471
466
472
467
async function subject ( ) {
473
468
if ( inputTokenName === "ETH" ) {
@@ -498,7 +493,7 @@ if (process.env.INTEGRATIONTEST) {
498
493
) ;
499
494
}
500
495
501
- beforeEach ( async ( ) => {
496
+ before ( async ( ) => {
502
497
subjectPriceEstimateInflater = ether ( 0.9 ) ;
503
498
subjectMaxSetAmount = ether ( 1 ) ;
504
499
subjectAmountOut =
@@ -547,35 +542,32 @@ if (process.env.INTEGRATIONTEST) {
547
542
if ( inputTokenName !== "ETH" ) {
548
543
subjectOutputToken = outputToken . address ;
549
544
}
545
+ setBalanceBefore = await setToken . balanceOf ( owner . address ) ;
546
+ ethBalanceBefore = await owner . wallet . getBalance ( ) ;
547
+ outputBalanceBefore =
548
+ inputTokenName === "ETH"
549
+ ? await owner . wallet . getBalance ( )
550
+ : await outputToken . balanceOf ( owner . address ) ;
551
+
552
+ const tx = await subject ( ) ;
553
+ const receipt = await tx . wait ( ) ;
554
+ gasCosts = receipt . gasUsed . mul ( tx . gasPrice ) ;
550
555
} ) ;
551
556
552
557
it ( "should redeem at most subjectMaxSetAmount" , async ( ) => {
553
- const setBalanceBefore = await setToken . balanceOf ( owner . address ) ;
554
- await subject ( ) ;
555
558
const setBalanceAfter = await setToken . balanceOf ( owner . address ) ;
556
559
const setRedeemed = setBalanceBefore . sub ( setBalanceAfter ) ;
557
560
expect ( setRedeemed ) . to . lte ( subjectMaxSetAmount ) ;
558
561
} ) ;
559
562
560
563
if ( inputTokenName !== "ETH" ) {
561
564
it ( "should give gas rebaste" , async ( ) => {
562
- const ethBalanceBefore = await owner . wallet . getBalance ( ) ;
563
- const tx = await subject ( ) ;
564
- const receipt = await tx . wait ( ) ;
565
- const gasCosts = receipt . gasUsed . mul ( tx . gasPrice ) ;
566
565
const ethBalanceAfter = await owner . wallet . getBalance ( ) ;
567
566
expect ( ethBalanceBefore . sub ( ethBalanceAfter ) ) . to . lt ( gasCosts ) ;
568
567
} ) ;
569
568
}
570
569
571
570
it ( "should return exactly specified of output tokens" , async ( ) => {
572
- const outputBalanceBefore =
573
- inputTokenName === "ETH"
574
- ? await owner . wallet . getBalance ( )
575
- : await outputToken . balanceOf ( owner . address ) ;
576
- const tx = await subject ( ) ;
577
- const receipt = await tx . wait ( ) ;
578
- const gasCosts = receipt . gasUsed . mul ( tx . gasPrice ) ;
579
571
const outputBalanceAfter =
580
572
inputTokenName === "ETH"
581
573
? await owner . wallet . getBalance ( )
@@ -603,6 +595,9 @@ if (process.env.INTEGRATIONTEST) {
603
595
let subjectSetAmount : BigNumber ;
604
596
let subjectMinAmountOut : BigNumber ;
605
597
let subjectOutputToken : Address ;
598
+ let setBalanceBefore : BigNumber ;
599
+ let outputBalanceBefore : BigNumber ;
600
+ let outputAmountQuote : BigNumber ;
606
601
607
602
async function subject ( ) {
608
603
if ( inputTokenName === "ETH" ) {
@@ -633,7 +628,7 @@ if (process.env.INTEGRATIONTEST) {
633
628
) ;
634
629
}
635
630
636
- beforeEach ( async ( ) => {
631
+ before ( async ( ) => {
637
632
subjectSetAmount = ether ( 1 ) ;
638
633
swapDataCollateralToDebt = {
639
634
path : [ collateralTokenAddress , addresses . tokens . USDC ] ,
@@ -664,22 +659,22 @@ if (process.env.INTEGRATIONTEST) {
664
659
if ( inputTokenName !== "ETH" ) {
665
660
subjectOutputToken = outputToken . address ;
666
661
}
662
+ setBalanceBefore = await setToken . balanceOf ( owner . address ) ;
663
+ outputBalanceBefore =
664
+ inputTokenName === "ETH"
665
+ ? await owner . wallet . getBalance ( )
666
+ : await outputToken . balanceOf ( owner . address ) ;
667
+ outputAmountQuote = await subjectQuote ( ) ;
668
+ await subject ( ) ;
667
669
} ) ;
668
670
669
671
it ( "should redeem the correct amount of tokens" , async ( ) => {
670
- const setBalanceBefore = await setToken . balanceOf ( owner . address ) ;
671
- await subject ( ) ;
672
672
const setBalanceAfter = await setToken . balanceOf ( owner . address ) ;
673
673
const setRedeemed = setBalanceBefore . sub ( setBalanceAfter ) ;
674
674
expect ( setRedeemed ) . to . eq ( subjectSetAmount ) ;
675
675
} ) ;
676
676
677
677
it ( "should return at least the specified minimum of output tokens" , async ( ) => {
678
- const outputBalanceBefore =
679
- inputTokenName === "ETH"
680
- ? await owner . wallet . getBalance ( )
681
- : await outputToken . balanceOf ( owner . address ) ;
682
- await subject ( ) ;
683
678
const outputBalanceAfter =
684
679
inputTokenName === "ETH"
685
680
? await owner . wallet . getBalance ( )
@@ -689,18 +684,12 @@ if (process.env.INTEGRATIONTEST) {
689
684
} ) ;
690
685
691
686
it ( "should quote the correct output amount" , async ( ) => {
692
- const outputBalanceBefore =
693
- inputTokenName === "ETH"
694
- ? await owner . wallet . getBalance ( )
695
- : await outputToken . balanceOf ( owner . address ) ;
696
- await subject ( ) ;
697
687
const outputBalanceAfter =
698
688
inputTokenName === "ETH"
699
689
? await owner . wallet . getBalance ( )
700
690
: await outputToken . balanceOf ( owner . address ) ;
701
691
const outputObtained = outputBalanceAfter . sub ( outputBalanceBefore ) ;
702
692
703
- const outputAmountQuote = await subjectQuote ( ) ;
704
693
expect ( outputAmountQuote ) . to . gt ( preciseMul ( outputObtained , ether ( 0.99 ) ) ) ;
705
694
expect ( outputAmountQuote ) . to . lt ( preciseMul ( outputObtained , ether ( 1.01 ) ) ) ;
706
695
} ) ;
0 commit comments