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
15 changes: 15 additions & 0 deletions packages/sdk/src/ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@ describe("normalizeSorobanContractId", () => {
assert.throws(() => normalizeSorobanContractId("C123"), /contractId/);
});
});

describe("normalizeRoundId numeric boundaries", () => {
for (const value of [0, -0, -1, 1.5, NaN, Infinity, -Infinity, Number.MAX_SAFE_INTEGER + 1, 0n, -1n]) {
it(`rejects ${String(value)} (${typeof value})`, () => {
assert.throws(() => normalizeRoundId(value), /roundId must be a positive/);
});
}
Comment on lines +41 to +45
it("preserves safe numbers and arbitrary-precision positive bigint/string IDs", () => {
assert.equal(normalizeRoundId(1), 1n);
assert.equal(normalizeRoundId(Number.MAX_SAFE_INTEGER), BigInt(Number.MAX_SAFE_INTEGER));
const large = 2n ** 100n;
assert.equal(normalizeRoundId(large), large);
assert.equal(normalizeRoundId(large.toString()), large);
});
});
9 changes: 6 additions & 3 deletions packages/sdk/src/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ function toTrimmedString(value: string | undefined): string | undefined {
}

export function normalizeRoundId(value: string | number | bigint): bigint {
if (typeof value === "bigint") return value;
if (typeof value === "bigint") {
if (value < 1n) throw new Error("roundId must be a positive integer");
return value;
}

if (typeof value === "number") {
if (!Number.isInteger(value)) {
throw new Error(`roundId must be an integer, got ${JSON.stringify(value)}`);
if (!Number.isSafeInteger(value) || value < 1) {
throw new Error(`roundId must be a positive safe integer, got ${JSON.stringify(value)}`);
}
Comment on lines +16 to 18
return BigInt(value);
}
Expand Down
Loading