fix(send): use live token balance and preserve precision on Max#2098
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.
Cherry-pick of ecbc1f9 from qa (#2097) to master.
Summary
useBalanceinSendFormand source balance fromuseWalletTokens— the same query the token picker uses (5s polling + SSE-invalidated). After a previous send the picker showed the fresh balance butSendFormshowed wagmi's stale cache (QueryClient defaultstaleTimeis 5 minutes and the safe-account send flow doesn't go throughuseTransactionAwaitto invalidate it).formatUnits(BigInt(balance), decimals)instead ofbalanceAmount.toString()on a JSNumber. For balances whose low-order wei round up when squeezed into a float64 (e.g.999999999999999999n→"1"→1000000000000000000n, 1 wei above the actual balance),parseUnitslater produced a transfer amount above the on-chain balance and the transfer reverted — while Send stayed enabled because the Zod check was a JS-Number compare.amount <= balancein wei viaparseUnitsso the Send button can't enable for amounts that round above the balance afterparseUnitsrounding. Also drop the.transform(val => Number(val))step so the precision-perfect amount string isn't laundered through aNumberbefore reachinguseSend.Test plan
Generated by Claude Code