From 74f63680c8f4cf3e506320b1096aa4af3ba0eb00 Mon Sep 17 00:00:00 2001 From: Ayush7614 Date: Fri, 11 Sep 2026 14:27:27 +0530 Subject: [PATCH] fix(app): reject non-finite usd in amountForUsd so the suggestion never becomes "Infinity" amountForUsd(Infinity, 1, 18) returned the literal string "Infinity", which viem parseUnits rejects downstream in suggestFirstBuy. Guard the usd input with Number.isFinite like quoteUsd already was, and cover Infinity/NaN for both inputs. --- app/src/lib/launchpad/first-buy.test.ts | 4 ++++ app/src/lib/launchpad/first-buy.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/lib/launchpad/first-buy.test.ts b/app/src/lib/launchpad/first-buy.test.ts index cd853c00..7589d4f3 100644 --- a/app/src/lib/launchpad/first-buy.test.ts +++ b/app/src/lib/launchpad/first-buy.test.ts @@ -30,6 +30,10 @@ test("amountForUsd: short decimals parseUnits accepts, never more places than th assert.equal(amountForUsd(25, 1e12, 2), null, "rounds to nothing at 2 decimals"); assert.equal(amountForUsd(0, 1, 6), null); assert.equal(amountForUsd(25, 0, 6), null); + assert.equal(amountForUsd(Infinity, 1, 18), null, "non-finite usd never becomes \"Infinity\" for parseUnits"); + assert.equal(amountForUsd(NaN, 1, 18), null); + assert.equal(amountForUsd(25, Infinity, 18), null); + assert.equal(amountForUsd(25, NaN, 18), null); for (const [usd, dec] of [[2464.485, 18], [223.05, 18], [0.0000195, 18], [1, 6]] as const) { const a = amountForUsd(25, usd, dec)!; assert.ok(parseUnits(a, dec) > 0n, `parseUnits accepts ${a}`); diff --git a/app/src/lib/launchpad/first-buy.ts b/app/src/lib/launchpad/first-buy.ts index 759306cc..af0f0027 100644 --- a/app/src/lib/launchpad/first-buy.ts +++ b/app/src/lib/launchpad/first-buy.ts @@ -28,7 +28,7 @@ function trimZeros(s: string): string { /** `usd` worth of a quote priced at `quoteUsd`, as a short decimal string that parseUnits accepts; null when it rounds to nothing. */ export function amountForUsd(usd: number, quoteUsd: number, decimals: number): string | null { - if (!(usd > 0) || !(quoteUsd > 0) || !Number.isFinite(quoteUsd)) return null; + if (!(usd > 0) || !(quoteUsd > 0) || !Number.isFinite(usd) || !Number.isFinite(quoteUsd)) return null; const v = usd / quoteUsd; let s = v >= 1 ? v.toFixed(2) : v.toPrecision(2); if (/e/i.test(s)) s = v.toFixed(Math.min(decimals, 12));