@@ -11,7 +11,6 @@ import {
11
11
} from "viem"
12
12
import { SwapperMode } from "../interface"
13
13
import type { SwapApiResponse } from "../interface"
14
- import { fetchLiFiExactOutQuote } from "../quoters"
15
14
import { runPipeline } from "../runner"
16
15
import type { StrategyResult , SwapParams } from "../types"
17
16
import {
@@ -26,7 +25,6 @@ import {
26
25
findToken ,
27
26
isExactInRepay ,
28
27
matchParams ,
29
- quoteToRoute ,
30
28
} from "../utils"
31
29
32
30
export const MTBILL_MAINNET = "0xdd629e5241cbc5919847783e6c96b2de4754e438"
@@ -87,9 +85,9 @@ export class StrategyMTBILL {
87
85
}
88
86
} else {
89
87
if ( isAddressEqual ( swapParams . tokenIn . addressInfo , USDC_MAINNET ) ) {
90
- result . response = await this . exactInToMTokenFromUSDC ( swapParams )
88
+ result . response = await this . exactInFromUSDCToMToken ( swapParams )
91
89
} else {
92
- result . response = await this . exactInToMTokenFromAny ( swapParams )
90
+ result . response = await this . exactInFromAnyToMToken ( swapParams )
93
91
}
94
92
}
95
93
break
@@ -105,9 +103,9 @@ export class StrategyMTBILL {
105
103
} else {
106
104
if ( isAddressEqual ( swapParams . tokenIn . addressInfo , USDC_MAINNET ) ) {
107
105
result . response =
108
- await this . targetDebtToMTokenFromUSDC ( swapParams )
106
+ await this . targetDebtFromUSDCToMToken ( swapParams )
109
107
} else {
110
- result . response = await this . targetDebtToMTokenFromAny ( swapParams )
108
+ result . response = await this . targetDebtFromAnyToMToken ( swapParams )
111
109
}
112
110
}
113
111
break
@@ -224,7 +222,7 @@ export class StrategyMTBILL {
224
222
}
225
223
}
226
224
227
- async exactInToMTokenFromUSDC (
225
+ async exactInFromUSDCToMToken (
228
226
swapParams : SwapParams ,
229
227
) : Promise < SwapApiResponse > {
230
228
const {
@@ -273,7 +271,7 @@ export class StrategyMTBILL {
273
271
}
274
272
}
275
273
276
- async exactInToMTokenFromAny (
274
+ async exactInFromAnyToMToken (
277
275
swapParams : SwapParams ,
278
276
) : Promise < SwapApiResponse > {
279
277
const innerSwapParams = {
@@ -392,25 +390,13 @@ export class StrategyMTBILL {
392
390
async targetDebtFromMTokenToAny (
393
391
swapParams : SwapParams ,
394
392
) : Promise < SwapApiResponse > {
395
- const lifiSwapParams = {
393
+ const innerSwapParams = {
396
394
...swapParams ,
397
395
tokenIn : findToken ( swapParams . chainId , USDC_MAINNET ) ,
398
- receiver : swapParams . from ,
396
+ vaultIn : USDC_ESCROW_VAULT_MAINNET ,
397
+ onlyFixedInputExactOut : true , // eliminate dust in the intermediate asset (vault underlying)
399
398
}
400
- const lifiQuote = await fetchLiFiExactOutQuote ( lifiSwapParams )
401
-
402
- const lifiSwapMulticallItem = encodeSwapMulticallItem ( {
403
- handler : SWAPPER_HANDLER_GENERIC ,
404
- mode : BigInt ( SwapperMode . TARGET_DEBT ) , // will deposit overswap from lifi, and also USDC although it shouldn't be necessary
405
- account : swapParams . accountOut ,
406
- tokenIn : swapParams . tokenIn . addressInfo ,
407
- tokenOut : swapParams . tokenOut . addressInfo ,
408
- vaultIn : swapParams . vaultIn ,
409
- accountIn : swapParams . accountIn ,
410
- receiver : swapParams . receiver ,
411
- amountOut : swapParams . targetDebt ,
412
- data : lifiQuote . data ,
413
- } )
399
+ const innerQuote = await runPipeline ( innerSwapParams )
414
400
415
401
const redeemSwapParams = {
416
402
...swapParams ,
@@ -421,12 +407,15 @@ export class StrategyMTBILL {
421
407
amountIn : redeemInstantAmountIn ,
422
408
} = await encodeMTBILLRedeemInstant (
423
409
redeemSwapParams ,
424
- lifiQuote . amountIn ,
410
+ BigInt ( innerQuote . amountIn ) ,
425
411
false ,
426
412
USDC_MAINNET ,
427
413
)
428
414
429
- const multicallItems = [ redeemInstantMulticallItem , lifiSwapMulticallItem ]
415
+ const multicallItems = [
416
+ redeemInstantMulticallItem ,
417
+ ...innerQuote . swap . multicallItems ,
418
+ ]
430
419
431
420
const swap = buildApiResponseSwap ( swapParams . from , multicallItems )
432
421
@@ -441,22 +430,22 @@ export class StrategyMTBILL {
441
430
return {
442
431
amountIn : String ( redeemInstantAmountIn ) ,
443
432
amountInMax : String ( redeemInstantAmountIn ) ,
444
- amountOut : String ( lifiQuote . amountOut ) ,
445
- amountOutMin : String ( lifiQuote . amountOutMin ) ,
433
+ amountOut : String ( innerQuote . amountOut ) ,
434
+ amountOutMin : String ( innerQuote . amountOutMin ) ,
446
435
vaultIn : swapParams . vaultIn ,
447
436
receiver : swapParams . receiver ,
448
437
accountIn : swapParams . accountIn ,
449
438
accountOut : swapParams . accountOut ,
450
439
tokenIn : swapParams . tokenIn ,
451
440
tokenOut : swapParams . tokenOut ,
452
441
slippage : swapParams . slippage ,
453
- route : [ MTBILL_ROUTE , ...quoteToRoute ( lifiQuote ) ] ,
442
+ route : [ MTBILL_ROUTE , ...innerQuote . route ] ,
454
443
swap,
455
444
verify,
456
445
}
457
446
}
458
447
459
- async targetDebtToMTokenFromUSDC (
448
+ async targetDebtFromUSDCToMToken (
460
449
swapParams : SwapParams ,
461
450
) : Promise < SwapApiResponse > {
462
451
const depositInstantAmount = adjustForInterest ( swapParams . amount ) // TODO move to config, helper
@@ -503,12 +492,12 @@ export class StrategyMTBILL {
503
492
}
504
493
}
505
494
506
- async targetDebtToMTokenFromAny (
495
+ async targetDebtFromAnyToMToken (
507
496
swapParams : SwapParams ,
508
497
) : Promise < SwapApiResponse > {
509
498
const targetDeposit = adjustForInterest ( swapParams . amount ) // TODO move to config, helper
510
499
511
- const swapParamsDeposit = {
500
+ const depositSwapParams = {
512
501
...swapParams ,
513
502
tokenIn : findToken ( swapParams . chainId , USDC_MAINNET ) ,
514
503
vaultIn : USDC_ESCROW_VAULT_MAINNET ,
@@ -518,34 +507,36 @@ export class StrategyMTBILL {
518
507
amountIn : depositInstantAmountIn ,
519
508
amountOut,
520
509
} = await encodeMTBILLDepositInstant (
521
- swapParamsDeposit ,
510
+ depositSwapParams ,
522
511
targetDeposit ,
523
512
true ,
524
513
USDC_MAINNET ,
525
514
)
526
515
527
- const lifiSwapParams = {
516
+ const innerSwapParams = {
528
517
...swapParams ,
529
518
amount : depositInstantAmountIn ,
530
519
tokenOut : findToken ( swapParams . chainId , USDC_MAINNET ) ,
531
- swapperMode : SwapperMode . EXACT_IN ,
532
520
receiver : swapParams . from ,
521
+ onlyFixedInputExactOut : true , // this option will overswap, which should cover growing exchange rate
533
522
}
534
- const lifiQuote = await fetchLiFiExactOutQuote ( lifiSwapParams )
535
- const lifiSwapMulticallItem = encodeSwapMulticallItem ( {
536
- handler : SWAPPER_HANDLER_GENERIC ,
537
- mode : BigInt ( SwapperMode . EXACT_IN ) ,
538
- account : swapParams . accountOut ,
539
- tokenIn : swapParams . tokenIn . addressInfo ,
540
- tokenOut : swapParams . tokenOut . addressInfo ,
541
- vaultIn : swapParams . vaultIn ,
542
- accountIn : swapParams . accountIn ,
543
- receiver : lifiSwapParams . receiver ,
544
- amountOut : 0n , // ignored
545
- data : lifiQuote . data ,
523
+
524
+ const innerQuote = await runPipeline ( innerSwapParams )
525
+
526
+ // re-encode inner swap from target debt to exact out so that repay is not executed before mint TODO fix with exact out support in all strategies
527
+ const innerSwapItems = innerQuote . swap . multicallItems . map ( ( item ) => {
528
+ if ( item . functionName !== "swap" ) return item
529
+
530
+ const newItem = encodeSwapMulticallItem ( {
531
+ ...item . args [ 0 ] ,
532
+ mode : BigInt ( SwapperMode . EXACT_OUT ) ,
533
+ } )
534
+
535
+ return newItem
546
536
} )
547
- // deposit instant is encoded in target debt mode, so repay will happen automatically
548
- const multicallItems = [ lifiSwapMulticallItem , depositInstantMulticallItem ]
537
+
538
+ // repay is done through deposit item, which will return unused input, which is the intermediate asset
539
+ const multicallItems = [ ...innerSwapItems , depositInstantMulticallItem ]
549
540
550
541
const swap = buildApiResponseSwap ( swapParams . from , multicallItems )
551
542
@@ -558,8 +549,8 @@ export class StrategyMTBILL {
558
549
)
559
550
560
551
return {
561
- amountIn : String ( lifiQuote . amountIn ) ,
562
- amountInMax : String ( lifiQuote . amountIn ) ,
552
+ amountIn : String ( innerQuote . amountIn ) ,
553
+ amountInMax : String ( innerQuote . amountInMax ) ,
563
554
amountOut : String ( amountOut ) ,
564
555
amountOutMin : String ( amountOut ) ,
565
556
vaultIn : swapParams . vaultIn ,
@@ -569,7 +560,7 @@ export class StrategyMTBILL {
569
560
tokenIn : swapParams . tokenIn ,
570
561
tokenOut : swapParams . tokenOut ,
571
562
slippage : swapParams . slippage ,
572
- route : [ ...quoteToRoute ( lifiQuote ) , MTBILL_ROUTE ] ,
563
+ route : [ ...innerQuote . route , MTBILL_ROUTE ] ,
573
564
swap,
574
565
verify,
575
566
}
0 commit comments