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));