Skip to content

Commit e88819c

Browse files
committed
fix redirect deposit wrapper
1 parent f466bc4 commit e88819c

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

src/swapService/strategies/strategyRedirectDepositWrapper.ts

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -80,65 +80,72 @@ export class StrategyRedirectDepositWrapper {
8080
routingOverride: routing,
8181
}
8282

83-
const innerSwap = await runPipeline(innerSwapParams)
83+
const innerSwaps = await runPipeline(innerSwapParams)
8484

8585
// split target debt repay into swap to Swapper, repay and deposit into escrow vault
86-
87-
const newMulticallItems = innerSwap.swap.multicallItems.flatMap(
88-
(item) => {
89-
if (
90-
item.functionName === "swap" &&
91-
item.args[0].mode === String(SwapperMode.TARGET_DEBT)
92-
) {
93-
const exactInSwapItemArgs = {
94-
...item.args[0],
95-
mode: SwapperMode.EXACT_IN,
86+
result.quotes = innerSwaps.map((innerSwap) => {
87+
const newMulticallItems = innerSwap.swap.multicallItems.flatMap(
88+
(item) => {
89+
if (
90+
item.functionName === "swap" &&
91+
item.args[0].mode === String(SwapperMode.TARGET_DEBT)
92+
) {
93+
const exactInSwapItemArgs = {
94+
...item.args[0],
95+
mode: SwapperMode.EXACT_IN,
96+
}
97+
98+
console.log()
99+
100+
const swapItem = encodeSwapMulticallItem(exactInSwapItemArgs)
101+
// if target debt is 0, encode repay(max) to repay all debt, otherwise use all of the available Swapper balance
102+
const repayAmount =
103+
swapParams.targetDebt === 0n ? maxUint256 : maxUint256 - 1n
104+
console.log(
105+
"swapParams.targetDebt === 0n: ",
106+
swapParams.targetDebt === 0n,
107+
)
108+
const repayItem = encodeRepayMulticallItem(
109+
vaultData.asset,
110+
swapParams.receiver,
111+
repayAmount,
112+
swapParams.accountOut,
113+
)
114+
const depositItem = encodeDepositMulticallItem(
115+
vaultData.asset,
116+
vaultData.assetDustEVault,
117+
5n,
118+
swapParams.accountOut,
119+
)
120+
121+
return [swapItem, repayItem, depositItem]
96122
}
97-
98-
const swapItem = encodeSwapMulticallItem(exactInSwapItemArgs)
99-
// if target debt is 0, encode repay(max) to repay all debt, otherwise use all of the available Swapper balance
100-
const repayAmount =
101-
swapParams.targetDebt === 0n ? maxUint256 : maxUint256 - 1n
102-
const repayItem = encodeRepayMulticallItem(
103-
vaultData.asset,
104-
swapParams.receiver,
105-
repayAmount,
106-
swapParams.accountOut,
107-
)
108-
const depositItem = encodeDepositMulticallItem(
109-
vaultData.asset,
110-
vaultData.assetDustEVault,
111-
5n,
112-
swapParams.accountOut,
113-
)
114-
115-
return [swapItem, repayItem, depositItem]
116-
}
117-
return item
118-
},
119-
)
120-
121-
// reencode everything
122-
123-
const swap = buildApiResponseSwap(swapParams.from, newMulticallItems)
124-
125-
let debtMax = swapParams.currentDebt - BigInt(innerSwap.amountOutMin)
126-
if (debtMax < 0n) debtMax = 0n
127-
debtMax = adjustForInterest(debtMax)
128-
129-
const verify = buildApiResponseVerifyDebtMax(
130-
swapParams.chainId,
131-
swapParams.receiver,
132-
swapParams.accountOut,
133-
debtMax,
134-
swapParams.deadline,
135-
)
136-
137-
result.response = {
138-
...innerSwap,
139-
swap,
140-
verify,
141-
}
123+
return item
124+
},
125+
)
126+
127+
// reencode everything
128+
129+
const swap = buildApiResponseSwap(swapParams.from, newMulticallItems)
130+
131+
let debtMax = swapParams.currentDebt - BigInt(innerSwap.amountOutMin)
132+
if (debtMax < 0n) debtMax = 0n
133+
debtMax = adjustForInterest(debtMax)
134+
135+
const verify = buildApiResponseVerifyDebtMax(
136+
swapParams.chainId,
137+
swapParams.receiver,
138+
swapParams.accountOut,
139+
debtMax,
140+
swapParams.deadline,
141+
)
142+
143+
return {
144+
...innerSwap,
145+
swap,
146+
verify,
147+
}
148+
})
142149
} catch (error) {
143150
result.error = error
144151
}

0 commit comments

Comments
 (0)