Fix #36: Correct USDC decimal precision in balance display#53
Fix #36: Correct USDC decimal precision in balance display#53DGMC70 wants to merge 1 commit intoBlockRunAI:mainfrom
Conversation
Fixes BlockRunAI#36 - stats display showing incorrect precision Previously formatUSDC used toFixed(2) which rounded small amounts like 0.004 USDC to 0.00 or 0.01, causing display inconsistency with the stats dashboard that uses toFixed(4). Now using 4 decimal places consistently across the codebase.
1bcMax
left a comment
There was a problem hiding this comment.
Thanks for the contribution! I appreciate you looking into #36.
However, after reviewing the codebase more carefully, I found a few issues with this approach:
1. Tests will break
test-balance.ts has 9 test cases asserting toFixed(2) output:
assertEqual(monitor.formatUSDC(0n), "$0.00");
assertEqual(monitor.formatUSDC(100n), "$0.00"); // $0.0001
assertEqual(monitor.formatUSDC(10000n), "$0.01"); // $0.01
assertEqual(monitor.formatUSDC(12345678n), "$12.35"); // $12.345678These would all fail with toFixed(4).
2. formatUSDC is for user-facing balance display, not stats
formatUSDC is used in CLI messages like:
"Wallet balance: $X.XX""Low balance: $X.XX. Fund: ...""Insufficient funds. Balance: $X.XX, Need: $X.XX"
Showing "$0.0040" in these contexts looks odd to users.
3. stats.ts already uses toFixed(4) independently
The stats dashboard (lines 227-283) already formats costs with toFixed(4) using its own inline formatting — it doesn't go through formatUSDC. So there's no actual inconsistency to fix here; they're intentionally different precisions for different contexts.
4. JSDoc / type comments not updated
The interface comments on lines 35 and 51 still say "$X.XX" format.
Suggestion
If #36 is about cost display in the stats dashboard being inaccurate, the fix should target the stats aggregation logic rather than the balance formatter. These are two separate formatting contexts with different precision needs.
|
Thanks for the contribution and for looking into this issue! Closing this PR for the reasons outlined in the review. Feel free to open a new PR if you'd like to take a different approach. |
Problem
Issue #36 reported that the dashboard shows incorrect USDC amounts - for example, showing $0.01 when the actual on-chain cost is 0.004 USDC.
Root Cause
The
formatUSDCfunction inbalance.tsusedtoFixed(2)(2 decimal places), while the stats dashboard usestoFixed(4)(4 decimal places). This inconsistency caused small amounts to be rounded incorrectly.Solution
formatUSDCto usetoFixed(4)for consistent precisionstats.tsdashboard displayChanges
src/balance.ts: Changed$${dollars.toFixed(2)}→$${dollars.toFixed(4)}Testing
/statscommand outputFixes #36