Skip to content

Commit f62b11a

Browse files
committed
fix binary search unit quote
1 parent 327f5ce commit f62b11a

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

src/swapService/config/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const baseRoutingConfig: ChainRoutingConfig = [
2727
includeSources: [
2828
"kyberswap",
2929
// "paraswap",
30-
//"odos",
30+
"odos",
3131
"1inch",
3232
"li-fi",
3333
"open-ocean",

src/swapService/config/sonic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const sonicConfig: ChainRoutingConfig = [
2323
config: {
2424
sourcesFilter: {
2525
includeSources: [
26-
// "kyberswap",
26+
"kyberswap",
2727
"paraswap",
2828
"odos",
2929
"1inch",

src/swapService/strategies/strategyBalmySDK.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -296,24 +296,6 @@ export class StrategyBalmySDK {
296296
sourcesFilter,
297297
)
298298

299-
// Pick a random quote to avoid lock in if one source returns a false quote
300-
// FIXME: use unit quotes as initial quotes
301-
const unitAmountTo =
302-
unitQuotes[Math.floor(Math.random() * unitQuotes.length)].minBuyAmount
303-
.amount
304-
305-
// const unitAmountTo = unitQuotes[0].minBuyAmount.amount
306-
307-
const estimatedAmountIn = calculateEstimatedAmountFrom(
308-
unitAmountTo,
309-
swapParamsExactIn.amount,
310-
swapParamsExactIn.tokenIn.decimals,
311-
swapParamsExactIn.tokenOut.decimals,
312-
)
313-
314-
if (estimatedAmountIn === 0n) throw new Error("quote not found")
315-
console.log("estimatedAmountIn: ", estimatedAmountIn)
316-
317299
const overSwapTarget = adjustForInterest(swapParams.amount)
318300

319301
const shouldContinue = (currentAmountTo: bigint): boolean =>
@@ -322,10 +304,27 @@ export class StrategyBalmySDK {
322304
(currentAmountTo * 1000n) / overSwapTarget > 1005n
323305

324306
// single run to preselect sources
325-
const initialQuotes = await this.#getAllQuotes({
326-
...swapParams,
327-
amount: estimatedAmountIn,
328-
})
307+
const initialQuotes = (
308+
await Promise.all(
309+
unitQuotes.map((unitQuote) => {
310+
const estimatedAmountIn = calculateEstimatedAmountFrom(
311+
unitQuote.minBuyAmount.amount,
312+
swapParamsExactIn.amount,
313+
swapParamsExactIn.tokenIn.decimals,
314+
swapParamsExactIn.tokenOut.decimals,
315+
)
316+
return this.#getAllQuotes(
317+
{
318+
...swapParams,
319+
amount: estimatedAmountIn,
320+
},
321+
{
322+
includeSources: [unitQuote.source.id],
323+
},
324+
)
325+
}),
326+
)
327+
).flat()
329328

330329
const allSettled = await Promise.allSettled(
331330
initialQuotes.map(async (initialQuote) =>
@@ -342,7 +341,7 @@ export class StrategyBalmySDK {
342341
}
343342
},
344343
overSwapTarget,
345-
estimatedAmountIn,
344+
initialQuote.sellAmount.amount,
346345
shouldContinue,
347346
{
348347
quote: initialQuote,
@@ -363,7 +362,9 @@ export class StrategyBalmySDK {
363362
const bestQuotes = allSettled
364363
.filter((q) => q.status === "fulfilled")
365364
.map((q) => q.value)
365+
366366
if (bestQuotes.length === 0) throw new Error("Quotes not found")
367+
367368
return bestQuotes
368369
}
369370

0 commit comments

Comments
 (0)