Skip to content

Commit 1211cbc

Browse files
committed
Fix bigint escrow formatting
1 parent 8b9d81b commit 1211cbc

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

apps/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev": "vite",
99
"build": "vite build",
1010
"preview": "vite preview",
11-
"test": "node --import tsx --test src/demo/trace-health-check.test.ts src/lib/config.test.ts src/dashboard/fixture-health-check.test.ts src/lib/round-status.test.ts src/components/dashboard/RoundStatusCard.test.tsx",
11+
"test": "node --import tsx --test src/demo/trace-health-check.test.ts src/lib/config.test.ts src/lib/chain.test.ts src/dashboard/fixture-health-check.test.ts src/lib/round-status.test.ts src/components/dashboard/RoundStatusCard.test.tsx",
1212
"typecheck": "tsc --noEmit -p tsconfig.json"
1313
},
1414
"dependencies": {

apps/web/src/lib/amount.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2026 Sub Rosa contributors
2+
3+
export function formatEscrowAmount(value: bigint, tokenLabel: string): string {
4+
const SCALE = 10_000_000n;
5+
const QUANTA = 10_000n;
6+
7+
const whole = value / SCALE;
8+
const remainder = value % SCALE;
9+
const fractional = (remainder * QUANTA + (SCALE / 2n)) / SCALE;
10+
11+
const normalizedWhole = fractional >= QUANTA ? whole + 1n : whole;
12+
const normalizedFraction = fractional >= QUANTA ? 0n : fractional;
13+
14+
return `${normalizedWhole}.${normalizedFraction.toString().padStart(4, "0")} ${tokenLabel}`;
15+
}

apps/web/src/lib/chain.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from "node:assert/strict";
2+
import { test } from "node:test";
3+
4+
import { formatEscrowAmount } from "./amount";
5+
6+
test("formatEscrowAmount preserves exact 4-decimal display above Number.MAX_SAFE_INTEGER", () => {
7+
assert.equal(formatEscrowAmount(9007199254740992n, "token"), "900719925.4741 token");
8+
assert.equal(formatEscrowAmount(9007199254741500n, "token"), "900719925.4742 token");
9+
});
10+
11+
test("formatEscrowAmount keeps four-decimal display for standard escrow values", () => {
12+
assert.equal(formatEscrowAmount(10_000_000n, "token"), "1.0000 token");
13+
assert.equal(formatEscrowAmount(1_000_000n, "token"), "0.1000 token");
14+
assert.equal(formatEscrowAmount(123_456_789n, "token"), "12.3457 token");
15+
});

apps/web/src/lib/chain.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
import { RoundContract } from "@sub-rosa/sdk";
99
import { useMemo } from "react";
1010

11+
import { formatEscrowAmount } from "./amount";
12+
1113
export const LOGO_SRC = "/sub-rosa-logo.png";
1214
export const RPC_URL = import.meta.env.VITE_RPC_URL ?? "https://soroban-testnet.stellar.org";
1315
export const NETWORK =
@@ -70,7 +72,7 @@ export function toDemoEscrowAmount(value: number): bigint {
7072
}
7173

7274
export function formatDemoAmount(value: bigint): string {
73-
return `${(Number(value) / 10_000_000).toFixed(4)} ${ESCROW_TOKEN_LABEL}`;
75+
return formatEscrowAmount(value, ESCROW_TOKEN_LABEL);
7476
}
7577

7678
export async function sha256Bytes(text: string): Promise<Uint8Array> {

0 commit comments

Comments
 (0)