|
| 1 | +import { type Address, isAddressEqual, maxUint256 } from "viem" |
| 2 | +import { getRoutingConfig } from "../config" |
| 3 | +import { SwapperMode } from "../interface" |
| 4 | +import { runPipeline } from "../runner" |
| 5 | +import type { StrategyResult, SwapParams } from "../types" |
| 6 | +import { |
| 7 | + adjustForInterest, |
| 8 | + buildApiResponseSwap, |
| 9 | + buildApiResponseVerifyDebtMax, |
| 10 | + encodeDepositMulticallItem, |
| 11 | + encodeRepayAndDepositMulticallItem, |
| 12 | + encodeRepayMulticallItem, |
| 13 | + encodeSwapMulticallItem, |
| 14 | + matchParams, |
| 15 | +} from "../utils" |
| 16 | + |
| 17 | +const defaultConfig: { |
| 18 | + supportedVaults: Array<{ |
| 19 | + chainId: number |
| 20 | + vault: Address |
| 21 | + asset: Address |
| 22 | + assetDustEVault: Address |
| 23 | + }> |
| 24 | +} = { |
| 25 | + supportedVaults: [ |
| 26 | + { |
| 27 | + chainId: 1, |
| 28 | + vault: "0xd001f0a15D272542687b2677BA627f48A4333b5d", |
| 29 | + asset: "0x73A15FeD60Bf67631dC6cd7Bc5B6e8da8190aCF5", |
| 30 | + assetDustEVault: "0xB0465546E8D70E667d4a187F66eF959B1522cc77", |
| 31 | + }, |
| 32 | + ], |
| 33 | +} |
| 34 | + |
| 35 | +// Wrapper which redirects deposit of over-swapped repay to vault other than the debt vault |
| 36 | +export class StrategyRedirectDepositWrapper { |
| 37 | + static name() { |
| 38 | + return "redirect_deposit_wrapper" |
| 39 | + } |
| 40 | + readonly match |
| 41 | + readonly config |
| 42 | + |
| 43 | + constructor(match = {}, config = defaultConfig) { |
| 44 | + this.match = match |
| 45 | + this.config = config |
| 46 | + } |
| 47 | + |
| 48 | + async supports(swapParams: SwapParams) { |
| 49 | + return ( |
| 50 | + swapParams.swapperMode === SwapperMode.TARGET_DEBT && |
| 51 | + this.config.supportedVaults.some( |
| 52 | + (v) => |
| 53 | + v.chainId === swapParams.chainId && |
| 54 | + isAddressEqual(v.vault, swapParams.receiver), |
| 55 | + ) |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + async findSwap(swapParams: SwapParams): Promise<StrategyResult> { |
| 60 | + const result: StrategyResult = { |
| 61 | + strategy: StrategyRedirectDepositWrapper.name(), |
| 62 | + supports: await this.supports(swapParams), |
| 63 | + match: matchParams(swapParams, this.match), |
| 64 | + } |
| 65 | + |
| 66 | + if (!result.supports || !result.match) return result |
| 67 | + |
| 68 | + try { |
| 69 | + const vaultData = this.getSupportedVault(swapParams.receiver) |
| 70 | + // remove itself from the routing and run the pipeline, directing output to Swapper |
| 71 | + const routing = getRoutingConfig(swapParams.chainId).filter( |
| 72 | + (r) => r.strategy !== StrategyRedirectDepositWrapper.name(), |
| 73 | + ) |
| 74 | + |
| 75 | + const innerSwapParams = { |
| 76 | + ...swapParams, |
| 77 | + receiver: swapParams.from, |
| 78 | + routingOverride: routing, |
| 79 | + } |
| 80 | + |
| 81 | + const innerSwap = await runPipeline(innerSwapParams) |
| 82 | + |
| 83 | + // split target debt repay into swap to Swapper, repay and deposit into escrow vault |
| 84 | + |
| 85 | + const newMulticallItems = innerSwap.swap.multicallItems.flatMap( |
| 86 | + (item) => { |
| 87 | + if ( |
| 88 | + item.functionName === "swap" && |
| 89 | + item.args[0].mode === String(SwapperMode.TARGET_DEBT) |
| 90 | + ) { |
| 91 | + const exactInSwapItemArgs = { |
| 92 | + ...item.args[0], |
| 93 | + mode: SwapperMode.EXACT_IN, |
| 94 | + } |
| 95 | + |
| 96 | + console.log("exactInSwapItemArgs: ", exactInSwapItemArgs) |
| 97 | + const swapItem = encodeSwapMulticallItem(exactInSwapItemArgs) |
| 98 | + // if target debt is 0, encode repay(max) to repay all debt, otherwise use all of the available Swapper balance |
| 99 | + const repayAmount = |
| 100 | + swapParams.targetDebt === 0n ? maxUint256 : maxUint256 - 1n |
| 101 | + console.log("repayAmount: ", repayAmount === maxUint256) |
| 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 | + console.log("swapParams.deadline: ", swapParams.deadline) |
| 137 | + |
| 138 | + result.response = { |
| 139 | + ...innerSwap, |
| 140 | + swap, |
| 141 | + verify, |
| 142 | + } |
| 143 | + } catch (error) { |
| 144 | + result.error = error |
| 145 | + } |
| 146 | + |
| 147 | + return result |
| 148 | + } |
| 149 | + |
| 150 | + getSupportedVault(vault: Address) { |
| 151 | + const supportedVault = this.config.supportedVaults.find((v) => |
| 152 | + isAddressEqual(v.vault, vault), |
| 153 | + ) |
| 154 | + if (!supportedVault) throw new Error("Vault not supported") |
| 155 | + |
| 156 | + return supportedVault |
| 157 | + } |
| 158 | +} |
0 commit comments