Skip to content
Closed
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
15 changes: 11 additions & 4 deletions src/ammCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,23 @@ function computePriceImpact(spotPrice: string, effectivePrice: string): string {

const spot = parseDecimal(spotPrice);
const effective = parseDecimal(effectivePrice);

const spotScaled = spot.int * spot.scale + spot.frac;
const effectiveScaled = effective.int * effective.scale + effective.frac;

if (spotScaled === 0n) return "0.00";
// Issue #1011 — normalize both values to their common scale before
// comparing, so that an integer spot price ("1") and a fractional
// effective price ("0.91") are not compared at different scales.
const commonScale = spot.scale > effective.scale ? spot.scale : effective.scale;
const spotCommon = spotScaled * (commonScale / spot.scale);
const effectiveCommon = effectiveScaled * (commonScale / effective.scale);

if (spotCommon === 0n) return "0.00";

// (spot - effective) / spot * 100 with 4 decimal places of precision
const SCALE = 10000n;
const numerator = (spotScaled - effectiveScaled) * SCALE * 100n;
const denominator = spotScaled;
const numerator = (spotCommon - effectiveCommon) * SCALE * 100n;
const denominator = spotCommon;

if (numerator <= 0n) return "0.00";

Expand Down