fix: format custom token balances with token decimals#604
fix: format custom token balances with token decimals#604Agilulfo1820 merged 3 commits intovechain:mainfrom
Conversation
👋 Thanks for your contribution!Since this PR comes from a forked repository, the lint and build will only run for internal PRs for security reasons. Thank you for your patience! 🙏 |
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughUpdated token balance formatting to support arbitrary token decimals: Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.ts`:
- Around line 40-43: The query cache key for useGetCustomTokenBalances must
include token.decimals because formatTokenBalance(tokenBalanceOriginal[0],
Number(token.decimals)) depends on it; update the key that currently uses
token.address and address to also include token.decimals so React Query will
refetch/segregate cached results when decimals change (locate the
useQuery/useGetCustomTokenBalances call and adjust its key tuple to include
token.decimals alongside token.address and address).
- Around line 26-35: In useGetCustomTokenBalances, validate addresses with
isValidAddress() before calling getErc20Balance and stop queries from running on
invalid inputs by adding an enabled flag to each query entry (pattern used in
useIpfsMetadatas); update the queryKey returned by getCustomTokenBalanceQueryKey
to include token.decimals because formatTokenBalance/scaled depends on it;
specifically ensure each mapped query uses enabled: !!address &&
isValidAddress(address) && !!token.address && isValidAddress(token.address) &&
!!network.nodeUrl and that the queryFn still calls
getErc20Balance(token.address, address, { networkUrl: network.nodeUrl }) and
then formats with formatTokenBalance(balance, token.decimals).
In `@packages/vechain-kit/src/utils/formattingUtils.tsx`:
- Around line 95-96: formatTokenBalance currently forwards a potentially
NaN/invalid decimals value to formatUnits causing runtime errors; inside
formatTokenBalance normalize and validate the decimals parameter (e.g., coerce
to a finite number via Number(...) or Number.isFinite check, default to 18 when
invalid, and Math.trunc/clamp to a non-negative integer) before calling
formatUnits so only a safe integer decimals is passed to formatUnits.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 4a39adea-d665-4bf6-abac-14c316a47d1a
📒 Files selected for processing (2)
packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.tspackages/vechain-kit/src/utils/formattingUtils.tsx
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.ts`:
- Around line 13-17: getCustomTokenBalanceQueryKey currently uses decimals as-is
which can become NaN when token.decimals is undefined, causing unstable cache
keys; update the function (and the similar key helper around lines 25-29) to
normalize decimals to a stable value (e.g., use a fallback like 0 or null and/or
coerce with Number(decimals ?? 0)) so the returned array always contains a
consistent primitive and avoids NaN-based inequality (refer to
getCustomTokenBalanceQueryKey to locate and change the key composition).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: ec78bd6a-f0f9-4d46-9aaa-e286943ea63a
📒 Files selected for processing (2)
packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.tspackages/vechain-kit/src/utils/formattingUtils.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/vechain-kit/src/utils/formattingUtils.tsx
Summary
formatTokenBalanceusedformatEther(hardcoded 18 decimals) to scale raw on-chain balances. Custom tokens with non-18 decimals displayed incorrect amounts in the asset list (e.g. a 9-decimal token with 979 tokens showed as 0).Changes
formatEther(balance)withformatUnits(balance, decimals)informatTokenBalance, defaulting to 18Number(token.decimals)fromuseGetCustomTokenBalanceswhen formatting custom token balancesBackward compatibility
decimalsparameter defaults to18, so all existing callers (B3TR, VOT3, veDelegate, SASS) are unaffectedSummary by CodeRabbit