diff --git a/lat.md/wallet-token-balances.md b/lat.md/wallet-token-balances.md index 0f727e1eb..0846f335b 100644 --- a/lat.md/wallet-token-balances.md +++ b/lat.md/wallet-token-balances.md @@ -34,6 +34,8 @@ Token metadata (contract address, symbol, decimals) lives in [[src/shared/tokens [[src/shared/tokens.ts#formatTokenBalance]] converts raw BigInt strings to compact form: zero → "0", ≥1M → "1.5M", ≥1K → "10.5K", tiny non-zero → "< 0.0001", otherwise up to 4 significant digits. [[src/shared/tokens.ts#formatTokenBalanceFull]] produces the same without K/M suffixes — used for tooltip display of exact amounts. +Full balance tooltips trim trailing zeros after the four-digit cut: `1.20001` displays as `1.2`, and `1.00001` as `1`. [[src/main/wallet-balances.test.ts]] covers both fractional branches and cuts leaving an integer-only result. + ### IPC & UI The `get-token-balances` IPC channel exposes balance reads to the renderer. Balances auto-fetch when the wallet pane loads; previously cached balances display immediately while fresh ones load, then update in place. diff --git a/src/main/wallet-balances.test.ts b/src/main/wallet-balances.test.ts index defa7ed09..7078e4881 100644 --- a/src/main/wallet-balances.test.ts +++ b/src/main/wallet-balances.test.ts @@ -106,6 +106,20 @@ describe("formatTokenBalanceFull", () => { it("formats fractions with 4 significant digits", () => { expect(formatTokenBalanceFull("123400000000000000", 18)).toBe("0.1234"); }); + + it("trims zeros exposed by the 4-digit cut", () => { + // 1.20001 tokens → "1.2" not "1.2000" + expect(formatTokenBalanceFull("1200010000000000000", 18)).toBe("1.2"); + }); + + it("drops the fraction when the cut leaves only zeros", () => { + expect(formatTokenBalanceFull("1000010000000000000", 18)).toBe("1"); + }); + + it("trims zeros exposed by the cut below 1", () => { + // 0.0001200005 tokens → "0.00012" + expect(formatTokenBalanceFull("120000500000000", 18)).toBe("0.00012"); + }); }); describe("formatTokenBalance precision for huge balances", () => { diff --git a/src/shared/tokens.ts b/src/shared/tokens.ts index 1a5556182..70f659c87 100644 --- a/src/shared/tokens.ts +++ b/src/shared/tokens.ts @@ -68,13 +68,15 @@ export function formatTokenBalanceFull(raw: string, decimals: number): string { const trimmedFrac = fractionalPart.replace(/0+$/, ""); if (integerPart !== "0") { - const capped = trimmedFrac.slice(0, 4); + // Truncation can expose trailing zeros (1.20001 → "2000"). + const capped = trimmedFrac.slice(0, 4).replace(/0+$/, ""); return capped ? `${integerPart}.${capped}` : integerPart; } if (firstNonZero >= 4) return "< 0.0001"; const significantDigits = trimmedFrac.slice(firstNonZero); - const visible = significantDigits.slice(0, 4); + // The first significant digit is nonzero, so trimming keeps at least one. + const visible = significantDigits.slice(0, 4).replace(/0+$/, ""); return `0.${"0".repeat(firstNonZero)}${visible}`; }