feat: unify balance query keys and fix useRefreshBalances#602
feat: unify balance query keys and fix useRefreshBalances#602Emilvooo wants to merge 1 commit 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! 🙏 |
📝 WalkthroughWalkthroughThis PR introduces centralized query key management by creating a Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes 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.
🧹 Nitpick comments (2)
packages/vechain-kit/src/hooks/api/wallet/useRefreshBalances.ts (1)
8-14: Avoid global cache fan-out for account-scoped refreshes.
refresh()is triggered from single-account flows likeuseTransferVET,useTransferERC20, and the account modal refresh button, but invalidatingbalance.all/price.allnow refetches every cached balance and price query for every address/token in the session. That fixes the stale-key gap, but it also turns one wallet action into a full cache sweep.Consider introducing an address-scoped balance prefix in
VECHAIN_KIT_QUERY_KEYSand lettingrefreshtarget the affected account, with a global fallback only when you truly want a full refresh.♻️ Suggested direction
-export const useRefreshBalances = () => { +export const useRefreshBalances = (address?: string) => { const queryClient = useQueryClient(); const refresh = async () => { await Promise.all([ queryClient.invalidateQueries({ - queryKey: VECHAIN_KIT_QUERY_KEYS.balance.all, + queryKey: address + ? VECHAIN_KIT_QUERY_KEYS.balance.byAddress(address) + : VECHAIN_KIT_QUERY_KEYS.balance.all, }), queryClient.invalidateQueries({ queryKey: VECHAIN_KIT_QUERY_KEYS.price.all, }), ]);Add a matching
balance.byAddress(address)prefix inpackages/vechain-kit/src/constants/queryKeys.ts, then compose the per-token balance keys under it.packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.ts (1)
23-50: Consider addingenabledcondition to prevent unnecessary query execution.The queries will execute and throw errors when
addressis undefined. Adding anenabledcondition would prevent unnecessary error handling and align with the coding guidelines pattern.♻️ Suggested improvement
return useQueries({ queries: customTokens.map((token) => ({ queryKey: getCustomTokenBalanceQueryKey(token.address, address), + enabled: !!token.address && !!address && !!network.nodeUrl, queryFn: async () => {As per coding guidelines: "Always conditionally enable queries using the
enabledproperty to prevent unnecessary contract calls, checking for all required parameters"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.ts` around lines 23 - 50, The queries in useGetCustomTokenBalances are running even when required params are missing; update the useQueries call (the customTokens.map block that builds each query with getCustomTokenBalanceQueryKey and queryFn calling getErc20Balance/formatTokenBalance) to include an enabled property that checks all prerequisites (e.g., enabled: !!address && !!token.address && !!network?.nodeUrl) so the query only executes when address, token.address and network.nodeUrl are present.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.ts`:
- Around line 23-50: The queries in useGetCustomTokenBalances are running even
when required params are missing; update the useQueries call (the
customTokens.map block that builds each query with getCustomTokenBalanceQueryKey
and queryFn calling getErc20Balance/formatTokenBalance) to include an enabled
property that checks all prerequisites (e.g., enabled: !!address &&
!!token.address && !!network?.nodeUrl) so the query only executes when address,
token.address and network.nodeUrl are present.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 61aa3d34-b767-46e1-8b37-f48ea1b358e1
📒 Files selected for processing (10)
packages/vechain-kit/src/constants/index.tspackages/vechain-kit/src/constants/queryKeys.tspackages/vechain-kit/src/hooks/api/wallet/useGetB3trBalance.tspackages/vechain-kit/src/hooks/api/wallet/useGetCustomTokenBalances.tspackages/vechain-kit/src/hooks/api/wallet/useGetErc20Balance.tspackages/vechain-kit/src/hooks/api/wallet/useGetTokenUsdPrice.tspackages/vechain-kit/src/hooks/api/wallet/useGetVot3Balance.tspackages/vechain-kit/src/hooks/api/wallet/useRefreshBalances.tspackages/vechain-kit/src/hooks/index.tspackages/vechain-kit/src/hooks/thor/accounts/useAccountBalance.ts
Summary
['VECHAIN_KIT', 'BALANCE', ...]prefixuseRefreshBalancesnot invalidating B3TR, VOT3, and ERC-20 balancesVECHAIN_KIT_QUERY_KEYSconstant for stable cache invalidationProblem
Balance query keys used three different root prefixes (
VECHAIN_KIT_BALANCE,VEBETTERDAO_BALANCE,['VECHAIN_KIT', 'BALANCE', ...]). Because of this,useRefreshBalancesonly invalidated the first prefix, leaving B3TR, VOT3, and ERC-20 balances stale after transactions.Solution
All keys now share
['VECHAIN_KIT', 'BALANCE']as root, defined in a singleVECHAIN_KIT_QUERY_KEYSconstant. This allows invalidating all balances at once:useRefreshBalancesuses this prefix to correctly invalidate all balance types.Breaking changes
Consumers hardcoding query key strings (e.g.
['VECHAIN_KIT_BALANCE', address]) need to switch to the exportedgetXxxQueryKeyfunctions orVECHAIN_KIT_QUERY_KEYS. Consumers already using the exported functions are unaffected.Summary by CodeRabbit