Skip to content

Commit f779944

Browse files
authored
Merge pull request #28 from Ayush7614/fix/amount-for-usd-finite-guard
fix(app): reject non-finite usd in amountForUsd
2 parents d067240 + 74f6368 commit f779944

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

‎app/src/lib/launchpad/first-buy.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ test("amountForUsd: short decimals parseUnits accepts, never more places than th
3030
assert.equal(amountForUsd(25, 1e12, 2), null, "rounds to nothing at 2 decimals");
3131
assert.equal(amountForUsd(0, 1, 6), null);
3232
assert.equal(amountForUsd(25, 0, 6), null);
33+
assert.equal(amountForUsd(Infinity, 1, 18), null, "non-finite usd never becomes \"Infinity\" for parseUnits");
34+
assert.equal(amountForUsd(NaN, 1, 18), null);
35+
assert.equal(amountForUsd(25, Infinity, 18), null);
36+
assert.equal(amountForUsd(25, NaN, 18), null);
3337
for (const [usd, dec] of [[2464.485, 18], [223.05, 18], [0.0000195, 18], [1, 6]] as const) {
3438
const a = amountForUsd(25, usd, dec)!;
3539
assert.ok(parseUnits(a, dec) > 0n, `parseUnits accepts ${a}`);

‎app/src/lib/launchpad/first-buy.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function trimZeros(s: string): string {
2828

2929
/** `usd` worth of a quote priced at `quoteUsd`, as a short decimal string that parseUnits accepts; null when it rounds to nothing. */
3030
export function amountForUsd(usd: number, quoteUsd: number, decimals: number): string | null {
31-
if (!(usd > 0) || !(quoteUsd > 0) || !Number.isFinite(quoteUsd)) return null;
31+
if (!(usd > 0) || !(quoteUsd > 0) || !Number.isFinite(usd) || !Number.isFinite(quoteUsd)) return null;
3232
const v = usd / quoteUsd;
3333
let s = v >= 1 ? v.toFixed(2) : v.toPrecision(2);
3434
if (/e/i.test(s)) s = v.toFixed(Math.min(decimals, 12));

0 commit comments

Comments
 (0)