Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions app/src/lib/launchpad/first-buy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion app/src/lib/launchpad/first-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading