From 80dddc05b4e884e2a9a6bd4a5351ba891097ea7f Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Tue, 23 Jun 2026 16:31:46 +0000 Subject: [PATCH] init --- src/cli/index.ts | 2 +- src/router/sushi/error.ts | 1 + src/router/sushi/index.ts | 11 +++++++++++ src/rpc/helpers.ts | 3 +-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/cli/index.ts b/src/cli/index.ts index a3bc4684..934b1ee6 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -277,7 +277,7 @@ export class RainSolverCli { /** Resets the DataFetcher (sushi router cached pool data) by the given interval */ async maybeResetDataFetcher() { const now = Date.now(); - if (this.nextDatafetcherReset <= now) { + if (this.nextDatafetcherReset <= now || this.state.router.sushi?.signalReset) { this.nextDatafetcherReset = now + this.appOptions.poolUpdateInterval * 60 * 1000; // reset only if the data fetcher is initialized successfully const res = await this.state.router.sushi?.reset(); diff --git a/src/router/sushi/error.ts b/src/router/sushi/error.ts index 67e4aa88..87582982 100644 --- a/src/router/sushi/error.ts +++ b/src/router/sushi/error.ts @@ -7,6 +7,7 @@ export enum SushiRouterErrorType { FetchFailed, WasmEncodedError, UndefinedTradeDestinationAddress, + NegativeOutput, } /** diff --git a/src/router/sushi/index.ts b/src/router/sushi/index.ts index ec57611a..79563367 100644 --- a/src/router/sushi/index.ts +++ b/src/router/sushi/index.ts @@ -101,6 +101,7 @@ export class SushiRouter extends RainSolverRouterBase { /** The sushi router data fetcher instance for interacting */ dataFetcher: RainDataFetcher; + signalReset = false; constructor( chainId: number, @@ -235,6 +236,15 @@ export class SushiRouter extends RainSolverRouterBase { ), ); } else { + if (route.amountOutBI < 0n) { + this.signalReset = true; + return Result.err( + new SushiRouterError( + `Sushi router returned negative output for route: amountOut ${route.amountOutBI.toString()}, amountIn: ${amountIn.toString()}, pair: ${toToken.symbol}/${fromToken.symbol}`, + SushiRouterErrorType.NegativeOutput, + ), + ); + } const price = calculatePrice18( amountIn, route.amountOutBI, @@ -278,6 +288,7 @@ export class SushiRouter extends RainSolverRouterBase { this.client as any, this.liquidityProviders, ); + this.signalReset = false; return true; } catch {} return false; diff --git a/src/rpc/helpers.ts b/src/rpc/helpers.ts index 9814665f..4a950d2d 100644 --- a/src/rpc/helpers.ts +++ b/src/rpc/helpers.ts @@ -112,10 +112,9 @@ export function probablyPicksFrom(ranges: number[], weights: number[]): number { // we now match the selection rates against // picked random int to get picked index for (let i = 0; i < ranges.length; i++) { - const weightsSlice = weights.slice(0, i); const offset = ranges .slice(0, i) - .reduce((a, b, j) => a + Math.max(b, Math.ceil(10_000 * weightsSlice[j])), 0); + .reduce((a, b, j) => a + Math.max(b, Math.ceil(10_000 * weights[j])), 0); const lowerBound = offset + 1; const upperBound = offset + ranges[i]; if (lowerBound <= pick && pick <= upperBound) {