Skip to content

Commit 0ee7d70

Browse files
committed
fix binary search stuck
1 parent b176fe8 commit 0ee7d70

File tree

6 files changed

+84
-69
lines changed

6 files changed

+84
-69
lines changed

src/swapService/runner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export async function runPipeline(
6767
amountOutMin: finalResult.quotes[0].amountOutMin,
6868
route: finalResult.quotes[0].route,
6969
})
70+
// console.log(
71+
// finalResult.quotes
72+
// .map((q) => q.route.map((r) => r.providerName).join(" "))
73+
// .join(", "),
74+
// )
7075

7176
// console.log('finalResult.quotes: ', JSON.stringify(finalResult.quotes, null, 2));
7277
return finalResult.quotes

src/swapService/strategies/balmySDK/sources/magpieQuoteSource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
QuoteParams,
1212
QuoteSourceMetadata,
1313
SourceQuoteResponse,
14+
SourceQuoteTransaction,
1415
} from "@balmy/sdk/dist/services/quotes/quote-sources/types"
1516
import {
1617
addQuoteSlippage,

src/swapService/strategies/strategyBalmySDK.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,15 @@ export class StrategyBalmySDK {
317317
receiver: swapParams.from,
318318
isRepay: false,
319319
}
320+
320321
const unitQuotes = await this.#getAllQuotes(
321322
{
322323
...swapParamsExactIn,
323324
amount: parseUnits("1", swapParams.tokenIn.decimals),
324325
},
325326
sourcesFilter,
326327
)
328+
327329
const unitAmountTo = unitQuotes[0].minBuyAmount.amount
328330

329331
const estimatedAmountIn = calculateEstimatedAmountFrom(
@@ -385,7 +387,6 @@ export class StrategyBalmySDK {
385387
.filter((q) => q.status === "fulfilled")
386388
.map((q) => q.value)
387389
if (bestQuotes.length === 0) throw new Error("Quotes not found")
388-
389390
return bestQuotes
390391
}
391392

src/swapService/strategies/strategyCombinedUniswap.ts

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -114,73 +114,76 @@ export class StrategyCombinedUniswap {
114114
shouldContinue,
115115
)) as SwapApiResponse[]
116116

117-
const uniswapSwapParams = {
118-
...swapParams,
119-
amount: swapParams.amount - BigInt(exactInputQuotes[0].amountOut),
120-
receiver: swapParams.from,
121-
}
122-
const {
123-
protocol,
124-
data: path,
125-
amountIn: uniswapAmountIn, // assuming exact out trade
126-
} = await fetchUniswapQuote(uniswapSwapParams)
127-
128-
result.quotes = exactInputQuotes.map((exactInputQuote) => {
129-
const uniswapSwapMulticallItem = encodeSwapMulticallItem({
130-
handler:
131-
protocol === "V2"
132-
? SWAPPER_HANDLER_UNISWAP_V2
133-
: SWAPPER_HANDLER_UNISWAP_V3,
134-
mode: BigInt(SwapperMode.TARGET_DEBT),
135-
account: swapParams.accountOut,
136-
tokenIn: swapParams.tokenIn.addressInfo,
137-
tokenOut: swapParams.tokenOut.addressInfo,
138-
vaultIn: swapParams.vaultIn,
139-
accountIn: swapParams.accountIn,
140-
receiver: swapParams.receiver,
141-
amountOut: swapParams.targetDebt,
142-
data: path as Hex,
143-
})
144-
145-
const combinedMulticallItems = [
146-
...exactInputQuote.swap.multicallItems,
147-
uniswapSwapMulticallItem,
148-
]
149-
150-
const swap = buildApiResponseSwap(
151-
swapParams.from,
152-
combinedMulticallItems,
153-
)
154-
155-
const verify = buildApiResponseVerifyDebtMax(
156-
swapParams.chainId,
157-
swapParams.receiver,
158-
swapParams.accountOut,
159-
swapParams.targetDebt,
160-
swapParams.deadline,
161-
)
162-
163-
const amountIn =
164-
BigInt(exactInputQuote.amountIn) + BigInt(uniswapAmountIn)
165-
const amountInMax = applySlippage(amountIn, swapParams.slippage, true)
166-
167-
return {
168-
amountIn: String(amountIn),
169-
amountInMax: String(amountInMax),
170-
amountOut: String(swapParams.amount),
171-
amountOutMin: String(swapParams.amount),
172-
vaultIn: swapParams.vaultIn,
173-
receiver: swapParams.receiver,
174-
accountIn: swapParams.accountIn,
175-
accountOut: swapParams.accountOut,
176-
tokenIn: swapParams.tokenIn,
177-
tokenOut: swapParams.tokenOut,
178-
slippage: swapParams.slippage,
179-
route: [...exactInputQuote.route, { providerName: "Uniswap" }],
180-
swap,
181-
verify,
182-
}
183-
})
117+
result.quotes = await Promise.all(
118+
exactInputQuotes.map(async (exactInputQuote) => {
119+
const uniswapSwapParams = {
120+
...swapParams,
121+
amount: swapParams.amount - BigInt(exactInputQuote.amountOutMin),
122+
receiver: swapParams.from,
123+
}
124+
125+
const {
126+
protocol,
127+
data: path,
128+
amountIn: uniswapAmountIn, // assuming exact out trade
129+
} = await fetchUniswapQuote(uniswapSwapParams)
130+
131+
const uniswapSwapMulticallItem = encodeSwapMulticallItem({
132+
handler:
133+
protocol === "V2"
134+
? SWAPPER_HANDLER_UNISWAP_V2
135+
: SWAPPER_HANDLER_UNISWAP_V3,
136+
mode: BigInt(SwapperMode.TARGET_DEBT),
137+
account: swapParams.accountOut,
138+
tokenIn: swapParams.tokenIn.addressInfo,
139+
tokenOut: swapParams.tokenOut.addressInfo,
140+
vaultIn: swapParams.vaultIn,
141+
accountIn: swapParams.accountIn,
142+
receiver: swapParams.receiver,
143+
amountOut: swapParams.targetDebt,
144+
data: path as Hex,
145+
})
146+
147+
const combinedMulticallItems = [
148+
...exactInputQuote.swap.multicallItems,
149+
uniswapSwapMulticallItem,
150+
]
151+
152+
const swap = buildApiResponseSwap(
153+
swapParams.from,
154+
combinedMulticallItems,
155+
)
156+
157+
const verify = buildApiResponseVerifyDebtMax(
158+
swapParams.chainId,
159+
swapParams.receiver,
160+
swapParams.accountOut,
161+
swapParams.targetDebt,
162+
swapParams.deadline,
163+
)
164+
165+
const amountIn =
166+
BigInt(exactInputQuote.amountIn) + BigInt(uniswapAmountIn)
167+
const amountInMax = applySlippage(amountIn, swapParams.slippage, true)
168+
169+
return {
170+
amountIn: String(amountIn),
171+
amountInMax: String(amountInMax),
172+
amountOut: String(swapParams.amount),
173+
amountOutMin: String(swapParams.amount),
174+
vaultIn: swapParams.vaultIn,
175+
receiver: swapParams.receiver,
176+
accountIn: swapParams.accountIn,
177+
accountOut: swapParams.accountOut,
178+
tokenIn: swapParams.tokenIn,
179+
tokenOut: swapParams.tokenOut,
180+
slippage: swapParams.slippage,
181+
route: [...exactInputQuote.route, { providerName: "Uniswap" }],
182+
swap,
183+
verify,
184+
}
185+
}),
186+
)
184187
} catch (error) {
185188
result.error = error
186189
}

src/swapService/strategies/strategyERC4626Wrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export class StrategyERC4626Wrapper {
253253

254254
const vaultData = this.getSupportedVault(swapParams.tokenIn.addressInfo)
255255
const tokenIn = findToken(swapParams.chainId, vaultData.asset)
256-
console.log("vaultData: ", vaultData)
256+
257257
if (!tokenIn) throw new Error("Inner token not found")
258258
const innerSwapParams = {
259259
...swapParams,

src/swapService/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ export async function binarySearchQuote(
494494
let cnt = 0
495495
let quote
496496
let amountTo
497+
let prevAmountTo
497498

498499
do {
499500
amountFrom = (amountFrom * percentageChange) / 10000n
@@ -504,6 +505,10 @@ export async function binarySearchQuote(
504505
amount: amountFrom,
505506
})))
506507

508+
if (prevAmountTo && prevAmountTo === amountTo)
509+
throw new Error("Binary search quote not improving")
510+
prevAmountTo = amountTo
511+
507512
if (amountTo === 0n || targetAmountTo === 0n)
508513
throw new Error("Quote not found")
509514

0 commit comments

Comments
 (0)