Replace wagmi useBalance with useWalletTokens for fresher balance data#2097
Merged
Conversation
The send form pulled balance from wagmi's useBalance, which has a 5-minute default staleTime in the app's QueryClient and isn't invalidated by the safe-account send flow (useSend doesn't go through useTransactionAwait). After a previous send, the token picker — fed by useWalletTokens (5s polling + SSE-invalidated) — showed the fresh balance, but selecting a token brought the user back to a SendForm displaying wagmi's stale cache. Drop wagmi's useBalance and source the balance from useWalletTokens instead, looking up the live token by contractAddress + chainId. The picker and the send screen now read from the same query. Max button also lost precision: it did `balanceAmount.toString()` where balanceAmount was Number(formatUnits(wei, decimals)). For balances whose low-order wei round up when squeezed into a float64, parseUnits later produced a BigInt above the actual on-chain balance and the transfer reverted — while Send stayed enabled because the Zod check was a JS-Number compare. Build the max string from the wei BigInt via formatUnits so it round-trips through parseUnits exactly, and validate amount <= balance in wei so the button can't enable for amounts that exceed the balance after rounding.
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactored the SendForm component to use the
useWalletTokenshook instead of wagmi'suseBalancehook. This ensures balance data stays current after a send transaction by leveraging the hook's 5-second polling and SSE-based invalidation, rather than relying on wagmi's 5-minute default staleTime.Key Changes
useBalancecalls (both native and ERC20) and replaced withuseWalletTokenshook that provides live token data across all supported chainsliveTokenthat looks up the fresh token data fromuseWalletTokensbased on the selected token's contract address and chain ID, ensuring the balance is always currentparseUnitsandBigIntcomparisons instead of floating-point arithmetic to prevent precision errorshandleMaxPressto format the max amount directly fromBigIntto avoid rounding issues that could cause on-chain revertszeroAddress,useBalance,useUser,TokenType) and addedparseUnitsfrom viemNotable Implementation Details
liveTokenmemoization searches across all chain token arrays to find the matching token, falling back to the storedselectedTokenif not foundparseUnitswith try-catch to handle invalid input gracefullyBigIntto ensure perfect round-trip throughparseUnits, preventing on-chain transfer reverts due to roundinghttps://claude.ai/code/session_01GjrKxQrVGUS4JQxMxEkHxa