@@ -296,24 +296,6 @@ export class StrategyBalmySDK {
296
296
sourcesFilter ,
297
297
)
298
298
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
-
317
299
const overSwapTarget = adjustForInterest ( swapParams . amount )
318
300
319
301
const shouldContinue = ( currentAmountTo : bigint ) : boolean =>
@@ -322,10 +304,27 @@ export class StrategyBalmySDK {
322
304
( currentAmountTo * 1000n ) / overSwapTarget > 1005n
323
305
324
306
// 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 ( )
329
328
330
329
const allSettled = await Promise . allSettled (
331
330
initialQuotes . map ( async ( initialQuote ) =>
@@ -342,7 +341,7 @@ export class StrategyBalmySDK {
342
341
}
343
342
} ,
344
343
overSwapTarget ,
345
- estimatedAmountIn ,
344
+ initialQuote . sellAmount . amount ,
346
345
shouldContinue ,
347
346
{
348
347
quote : initialQuote ,
@@ -363,7 +362,9 @@ export class StrategyBalmySDK {
363
362
const bestQuotes = allSettled
364
363
. filter ( ( q ) => q . status === "fulfilled" )
365
364
. map ( ( q ) => q . value )
365
+
366
366
if ( bestQuotes . length === 0 ) throw new Error ( "Quotes not found" )
367
+
367
368
return bestQuotes
368
369
}
369
370
0 commit comments