From 5099c9f11ba440b60f43bf7c4d20fb6edd5a10b5 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Thu, 17 Sep 2026 05:32:51 +0000 Subject: [PATCH 1/6] fix(wallet): trim zeros exposed by the 4-significant-digit cut formatTokenBalanceFull promises "up to 4 significant digits with trailing zeros removed", but it trimmed the fraction before capping it to 4 digits. When the cut lands on a zero that was only interior in the untrimmed fraction, that zero becomes trailing and was still rendered: 1.20001 HD showed as "1.2000" and 1.00001 HD as "1.0000". Both branches now re-trim after the slice. The sub-1 branch keeps its leading zeros (they are emitted separately) and its first significant digit is non-zero, so a digit always survives. formatTokenBalance inherits the fix below the K threshold, where it delegates here. --- .../wallet-balances.trailing-zeros.test.ts | 47 +++++++++++++++++++ src/shared/tokens.ts | 9 +++- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/main/wallet-balances.trailing-zeros.test.ts diff --git a/src/main/wallet-balances.trailing-zeros.test.ts b/src/main/wallet-balances.trailing-zeros.test.ts new file mode 100644 index 000000000..8b92b78e9 --- /dev/null +++ b/src/main/wallet-balances.trailing-zeros.test.ts @@ -0,0 +1,47 @@ +// @vitest-environment node +// @lat: [[wallet-token-balances#Tests]] + +import { describe, expect, it } from "vitest"; +import { formatTokenBalance, formatTokenBalanceFull } from "../shared/tokens"; + +// Both formatters promise "up to 4 significant digits with trailing zeros +// removed", but they trimmed the fraction *before* capping it to 4 digits. +// When the cut lands on a zero that was only interior in the untrimmed +// fraction, that zero becomes trailing and was still rendered: 1.20001 HD +// showed as "1.2000", which reads as a balance pinned to 4 decimals rather +// than the exact 1.2 the digits round to. +describe("token balance trailing zeros after the 4-digit cut", () => { + it("drops zeros the cut turns into trailing zeros", () => { + // 1.20001 tokens → "1.2", not "1.2000" + expect(formatTokenBalanceFull("1200010000000000000", 18)).toBe("1.2"); + }); + + it("drops the whole fraction when the cut leaves only zeros", () => { + // 1.00001 tokens → "1", not "1.0000" + expect(formatTokenBalanceFull("1000010000000000000", 18)).toBe("1"); + }); + + it("re-trims a sub-1 balance whose leading zeros are preserved", () => { + // 0.0001200005 tokens → "0.00012": the three leading zeros stay, the + // zeros exposed by the cut go. + expect(formatTokenBalanceFull("120000500000000", 18)).toBe("0.00012"); + }); + + it("applies to 6-decimal tokens", () => { + // 1.20001 USDC-like → "1.2" + expect(formatTokenBalanceFull("1200010", 6)).toBe("1.2"); + }); + + it("re-trims through the compact formatter below the K threshold", () => { + // 999.90001 tokens stays under 1,000, so the compact path falls through + // to the full formatter and inherits the same cut. + expect(formatTokenBalance("999900010000000000000", 18)).toBe("999.9"); + }); + + it("keeps interior zeros that survive the cut (control)", () => { + // 1.2001 tokens — all four digits are significant, so the trim must not + // reach the interior zeros. Passes with and without the fix; it pins the + // over-trimming regression the fix must not introduce. + expect(formatTokenBalanceFull("1200100000000000000", 18)).toBe("1.2001"); + }); +}); diff --git a/src/shared/tokens.ts b/src/shared/tokens.ts index 1a5556182..1bd8f21ed 100644 --- a/src/shared/tokens.ts +++ b/src/shared/tokens.ts @@ -68,13 +68,18 @@ export function formatTokenBalanceFull(raw: string, decimals: number): string { const trimmedFrac = fractionalPart.replace(/0+$/, ""); if (integerPart !== "0") { - const capped = trimmedFrac.slice(0, 4); + // Trim again after capping: the cut can land on a zero that was only + // interior before (1.20001 → "20001" → "2000"), and those zeros are + // trailing once the digits behind them are dropped. + 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); + // Same cut, same re-trim (0.0001200005 → "1200005" → "1200" → "12"). The + // leading digit is non-zero, so this always keeps at least one digit. + const visible = significantDigits.slice(0, 4).replace(/0+$/, ""); return `0.${"0".repeat(firstNonZero)}${visible}`; } From c649d675c706ca3d20f8292b13859085a8b4a7ed Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Thu, 17 Sep 2026 06:50:36 +0000 Subject: [PATCH 2/6] test(wallet): cover the remaining trailing-zero boundary rows Adds the ledger rows the first pass left untested: an interior zero kept while the cut's exposed tail goes, the sub-1 branch with no leading zeros at both 18 and 6 decimals, a fraction entirely past the 4-digit window, and a control pinning that the compact K path never reaches the changed lines. --- .../wallet-balances.trailing-zeros.test.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/main/wallet-balances.trailing-zeros.test.ts b/src/main/wallet-balances.trailing-zeros.test.ts index 8b92b78e9..b1fa9e8ff 100644 --- a/src/main/wallet-balances.trailing-zeros.test.ts +++ b/src/main/wallet-balances.trailing-zeros.test.ts @@ -44,4 +44,39 @@ describe("token balance trailing zeros after the 4-digit cut", () => { // over-trimming regression the fix must not introduce. expect(formatTokenBalanceFull("1200100000000000000", 18)).toBe("1.2001"); }); + + it("keeps an interior zero while dropping the tail the cut exposes", () => { + // 1.0200001 tokens → "1.02". The trim is anchored, so it must stop at the + // "2" and leave the zero in front of it alone while still removing the + // two zeros the cut turned into a tail. Was "1.0200". + expect(formatTokenBalanceFull("1020000100000000000", 18)).toBe("1.02"); + }); + + it("re-trims a sub-1 balance with no leading zeros", () => { + // 0.100001 tokens → "0.1". firstNonZero is 0 here, so no leading zeros are + // re-emitted and the cut's exposed tail is the whole visible fraction bar + // the first digit. Was "0.1000". + expect(formatTokenBalanceFull("100001000000000000", 18)).toBe("0.1"); + }); + + it("re-trims a sub-1 balance for 6-decimal tokens", () => { + // 0.100001 USDC-like → "0.1". The sub-1 branch is reached at a different + // decimals scale than the 18-decimal cases above. Was "0.1000". + expect(formatTokenBalanceFull("100001", 6)).toBe("0.1"); + }); + + it("returns the bare integer when the fraction is past the 4-digit window", () => { + // 1.000000001 tokens → "1". The "< 0.0001" early return only guards the + // sub-1 branch, so here the cut yields four zeros, the re-trim empties + // them, and the existing `capped ?` guard drops the point. Was "1.0000". + expect(formatTokenBalanceFull("1000000001000000000", 18)).toBe("1"); + }); + + it("leaves the compact K path untouched (control)", () => { + // 1000.20001 tokens is at/above the K threshold, so formatTokenBalance + // returns via compactScale and never reaches the changed lines. Passes + // with and without the fix; it controls for the compact path, which the + // control above does not cover. + expect(formatTokenBalance("1000200010000000000000", 18)).toBe("1K"); + }); }); From 8725595e5c1c36d4ceb82c41814ae2e1a9258d7c Mon Sep 17 00:00:00 2001 From: askalf Date: Fri, 18 Sep 2026 08:07:01 -0400 Subject: [PATCH 3/6] test(wallet): state the truncation invariant instead of the patch history --- .../wallet-balances.trailing-zeros.test.ts | 43 +++++++------------ src/shared/tokens.ts | 7 +-- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/main/wallet-balances.trailing-zeros.test.ts b/src/main/wallet-balances.trailing-zeros.test.ts index b1fa9e8ff..2a1ec8571 100644 --- a/src/main/wallet-balances.trailing-zeros.test.ts +++ b/src/main/wallet-balances.trailing-zeros.test.ts @@ -4,12 +4,9 @@ import { describe, expect, it } from "vitest"; import { formatTokenBalance, formatTokenBalanceFull } from "../shared/tokens"; -// Both formatters promise "up to 4 significant digits with trailing zeros -// removed", but they trimmed the fraction *before* capping it to 4 digits. -// When the cut lands on a zero that was only interior in the untrimmed -// fraction, that zero becomes trailing and was still rendered: 1.20001 HD -// showed as "1.2000", which reads as a balance pinned to 4 decimals rather -// than the exact 1.2 the digits round to. +// Truncated fractional output omits the trailing zeros the truncation +// exposes while preserving interior zeros: 1.20001 renders as "1.2", never +// as "1.2000". describe("token balance trailing zeros after the 4-digit cut", () => { it("drops zeros the cut turns into trailing zeros", () => { // 1.20001 tokens → "1.2", not "1.2000" @@ -22,8 +19,8 @@ describe("token balance trailing zeros after the 4-digit cut", () => { }); it("re-trims a sub-1 balance whose leading zeros are preserved", () => { - // 0.0001200005 tokens → "0.00012": the three leading zeros stay, the - // zeros exposed by the cut go. + // 0.0001200005 tokens → "0.00012": the leading zeros stay, the zeros + // exposed by the truncation go. expect(formatTokenBalanceFull("120000500000000", 18)).toBe("0.00012"); }); @@ -33,50 +30,40 @@ describe("token balance trailing zeros after the 4-digit cut", () => { }); it("re-trims through the compact formatter below the K threshold", () => { - // 999.90001 tokens stays under 1,000, so the compact path falls through - // to the full formatter and inherits the same cut. + // 999.90001 tokens stays under 1,000, so the compact formatter renders it + // in full and truncates the same way. expect(formatTokenBalance("999900010000000000000", 18)).toBe("999.9"); }); it("keeps interior zeros that survive the cut (control)", () => { - // 1.2001 tokens — all four digits are significant, so the trim must not - // reach the interior zeros. Passes with and without the fix; it pins the - // over-trimming regression the fix must not introduce. + // 1.2001 tokens: every visible digit is significant, so nothing is trimmed. expect(formatTokenBalanceFull("1200100000000000000", 18)).toBe("1.2001"); }); it("keeps an interior zero while dropping the tail the cut exposes", () => { - // 1.0200001 tokens → "1.02". The trim is anchored, so it must stop at the - // "2" and leave the zero in front of it alone while still removing the - // two zeros the cut turned into a tail. Was "1.0200". + // 1.0200001 tokens → "1.02": the interior zero stays, the exposed tail goes. expect(formatTokenBalanceFull("1020000100000000000", 18)).toBe("1.02"); }); it("re-trims a sub-1 balance with no leading zeros", () => { - // 0.100001 tokens → "0.1". firstNonZero is 0 here, so no leading zeros are - // re-emitted and the cut's exposed tail is the whole visible fraction bar - // the first digit. Was "0.1000". + // 0.100001 tokens → "0.1": with no leading zeros, the exposed tail is the + // whole visible fraction but its first digit. expect(formatTokenBalanceFull("100001000000000000", 18)).toBe("0.1"); }); it("re-trims a sub-1 balance for 6-decimal tokens", () => { - // 0.100001 USDC-like → "0.1". The sub-1 branch is reached at a different - // decimals scale than the 18-decimal cases above. Was "0.1000". + // 0.100001 USDC-like → "0.1", at a 6-decimal scale. expect(formatTokenBalanceFull("100001", 6)).toBe("0.1"); }); it("returns the bare integer when the fraction is past the 4-digit window", () => { - // 1.000000001 tokens → "1". The "< 0.0001" early return only guards the - // sub-1 branch, so here the cut yields four zeros, the re-trim empties - // them, and the existing `capped ?` guard drops the point. Was "1.0000". + // 1.000000001 tokens → "1": the visible fraction is all zeros, so the + // decimal point goes with it. expect(formatTokenBalanceFull("1000000001000000000", 18)).toBe("1"); }); it("leaves the compact K path untouched (control)", () => { - // 1000.20001 tokens is at/above the K threshold, so formatTokenBalance - // returns via compactScale and never reaches the changed lines. Passes - // with and without the fix; it controls for the compact path, which the - // control above does not cover. + // 1000.20001 tokens is at the K threshold, so the compact scale renders it. expect(formatTokenBalance("1000200010000000000000", 18)).toBe("1K"); }); }); diff --git a/src/shared/tokens.ts b/src/shared/tokens.ts index 1bd8f21ed..70f659c87 100644 --- a/src/shared/tokens.ts +++ b/src/shared/tokens.ts @@ -68,17 +68,14 @@ export function formatTokenBalanceFull(raw: string, decimals: number): string { const trimmedFrac = fractionalPart.replace(/0+$/, ""); if (integerPart !== "0") { - // Trim again after capping: the cut can land on a zero that was only - // interior before (1.20001 → "20001" → "2000"), and those zeros are - // trailing once the digits behind them are dropped. + // 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); - // Same cut, same re-trim (0.0001200005 → "1200005" → "1200" → "12"). The - // leading digit is non-zero, so this always keeps at least one digit. + // 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}`; } From 7b6127dcc48ffd651d9bf73a975d8c8febd16358 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Tue, 29 Sep 2026 10:10:23 -0400 Subject: [PATCH 4/6] test(wallet): move the trailing-zero cases into the formatTokenBalanceFull tests One case per truncation branch, beside the existing formatter tests. --- src/main/wallet-balances.test.ts | 10 +++ .../wallet-balances.trailing-zeros.test.ts | 69 ------------------- 2 files changed, 10 insertions(+), 69 deletions(-) delete mode 100644 src/main/wallet-balances.trailing-zeros.test.ts diff --git a/src/main/wallet-balances.test.ts b/src/main/wallet-balances.test.ts index defa7ed09..2889354c8 100644 --- a/src/main/wallet-balances.test.ts +++ b/src/main/wallet-balances.test.ts @@ -106,6 +106,16 @@ 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("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/main/wallet-balances.trailing-zeros.test.ts b/src/main/wallet-balances.trailing-zeros.test.ts deleted file mode 100644 index 2a1ec8571..000000000 --- a/src/main/wallet-balances.trailing-zeros.test.ts +++ /dev/null @@ -1,69 +0,0 @@ -// @vitest-environment node -// @lat: [[wallet-token-balances#Tests]] - -import { describe, expect, it } from "vitest"; -import { formatTokenBalance, formatTokenBalanceFull } from "../shared/tokens"; - -// Truncated fractional output omits the trailing zeros the truncation -// exposes while preserving interior zeros: 1.20001 renders as "1.2", never -// as "1.2000". -describe("token balance trailing zeros after the 4-digit cut", () => { - it("drops zeros the cut turns into trailing zeros", () => { - // 1.20001 tokens → "1.2", not "1.2000" - expect(formatTokenBalanceFull("1200010000000000000", 18)).toBe("1.2"); - }); - - it("drops the whole fraction when the cut leaves only zeros", () => { - // 1.00001 tokens → "1", not "1.0000" - expect(formatTokenBalanceFull("1000010000000000000", 18)).toBe("1"); - }); - - it("re-trims a sub-1 balance whose leading zeros are preserved", () => { - // 0.0001200005 tokens → "0.00012": the leading zeros stay, the zeros - // exposed by the truncation go. - expect(formatTokenBalanceFull("120000500000000", 18)).toBe("0.00012"); - }); - - it("applies to 6-decimal tokens", () => { - // 1.20001 USDC-like → "1.2" - expect(formatTokenBalanceFull("1200010", 6)).toBe("1.2"); - }); - - it("re-trims through the compact formatter below the K threshold", () => { - // 999.90001 tokens stays under 1,000, so the compact formatter renders it - // in full and truncates the same way. - expect(formatTokenBalance("999900010000000000000", 18)).toBe("999.9"); - }); - - it("keeps interior zeros that survive the cut (control)", () => { - // 1.2001 tokens: every visible digit is significant, so nothing is trimmed. - expect(formatTokenBalanceFull("1200100000000000000", 18)).toBe("1.2001"); - }); - - it("keeps an interior zero while dropping the tail the cut exposes", () => { - // 1.0200001 tokens → "1.02": the interior zero stays, the exposed tail goes. - expect(formatTokenBalanceFull("1020000100000000000", 18)).toBe("1.02"); - }); - - it("re-trims a sub-1 balance with no leading zeros", () => { - // 0.100001 tokens → "0.1": with no leading zeros, the exposed tail is the - // whole visible fraction but its first digit. - expect(formatTokenBalanceFull("100001000000000000", 18)).toBe("0.1"); - }); - - it("re-trims a sub-1 balance for 6-decimal tokens", () => { - // 0.100001 USDC-like → "0.1", at a 6-decimal scale. - expect(formatTokenBalanceFull("100001", 6)).toBe("0.1"); - }); - - it("returns the bare integer when the fraction is past the 4-digit window", () => { - // 1.000000001 tokens → "1": the visible fraction is all zeros, so the - // decimal point goes with it. - expect(formatTokenBalanceFull("1000000001000000000", 18)).toBe("1"); - }); - - it("leaves the compact K path untouched (control)", () => { - // 1000.20001 tokens is at the K threshold, so the compact scale renders it. - expect(formatTokenBalance("1000200010000000000000", 18)).toBe("1K"); - }); -}); From 99429853dcd52593841b6f1074ab598967cb997b Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 5 Oct 2026 14:22:31 -0400 Subject: [PATCH 5/6] test(wallet): cover a cut that leaves only zeros --- src/main/wallet-balances.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/wallet-balances.test.ts b/src/main/wallet-balances.test.ts index 2889354c8..7078e4881 100644 --- a/src/main/wallet-balances.test.ts +++ b/src/main/wallet-balances.test.ts @@ -112,6 +112,10 @@ describe("formatTokenBalanceFull", () => { 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"); From 5899873cc3b3fc3a5db23f6ab17a593aa621e051 Mon Sep 17 00:00:00 2001 From: Fathah KA <48355244+fathah@users.noreply.github.com> Date: Sun, 11 Oct 2026 10:08:31 +0530 Subject: [PATCH 6/6] docs(wallet): describe full-balance zero trimming --- lat.md/wallet-token-balances.md | 2 ++ 1 file changed, 2 insertions(+) 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.