Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/router/sushi/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum SushiRouterErrorType {
FetchFailed,
WasmEncodedError,
UndefinedTradeDestinationAddress,
NegativeOutput,
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/router/sushi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class SushiRouter extends RainSolverRouterBase {

/** The sushi router data fetcher instance for interacting */
dataFetcher: RainDataFetcher;
signalReset = false;
Comment thread
rouzwelt marked this conversation as resolved.

constructor(
chainId: number,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -278,6 +288,7 @@ export class SushiRouter extends RainSolverRouterBase {
this.client as any,
this.liquidityProviders,
);
this.signalReset = false;
return true;
} catch {}
return false;
Expand Down
3 changes: 1 addition & 2 deletions src/rpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading