From 3b23f4b72328f4b0a0875c133de95c4ee1cd5e68 Mon Sep 17 00:00:00 2001 From: bus Date: Tue, 6 May 2025 22:15:30 +0000 Subject: [PATCH] Fix silently returning an incorrect swap impact result --- src/swap.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/swap.ts b/src/swap.ts index 5ffd9b8..2c915a2 100644 --- a/src/swap.ts +++ b/src/swap.ts @@ -161,6 +161,9 @@ export class CrocSwapPlan { (this.baseToken.tokenAddr, this.quoteToken.tokenAddr, (await this.context).chain.poolIndex, this.sellBase, this.qtyInBase, await this.qty, TIP, limitPrice); + if ((impact[0] > 0 && impact[1] > 0) || (impact[0] < 0 && impact[1] < 0)) + throw new Error("Invalid impact: base and quote flows have matching signs") + const baseQty = this.baseToken.toDisplay(impact[0] < 0 ? -impact[0] : impact[0]) const quoteQty = this.quoteToken.toDisplay(impact[1] < 0 ? -impact[1] : impact[1]) const spotPrice = decodeCrocPrice(impact[2])