Skip to content

Replace wagmi useBalance with useWalletTokens for fresher balance data#2097

Merged
MusabShakeel576 merged 1 commit into
qafrom
claude/inspiring-hamilton-ZvZYP
May 26, 2026
Merged

Replace wagmi useBalance with useWalletTokens for fresher balance data#2097
MusabShakeel576 merged 1 commit into
qafrom
claude/inspiring-hamilton-ZvZYP

Conversation

@MusabShakeel576
Copy link
Copy Markdown
Contributor

Summary

Refactored the SendForm component to use the useWalletTokens hook instead of wagmi's useBalance hook. 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

  • Replaced balance fetching: Removed useBalance calls (both native and ERC20) and replaced with useWalletTokens hook that provides live token data across all supported chains
  • Introduced liveToken concept: Added memoized liveToken that looks up the fresh token data from useWalletTokens based on the selected token's contract address and chain ID, ensuring the balance is always current
  • Improved precision handling:
    • Changed balance validation to use parseUnits and BigInt comparisons instead of floating-point arithmetic to prevent precision errors
    • Updated handleMaxPress to format the max amount directly from BigInt to avoid rounding issues that could cause on-chain reverts
  • Simplified imports: Removed unused imports (zeroAddress, useBalance, useUser, TokenType) and added parseUnits from viem
  • Updated validation logic: Refactored the send schema to perform wei-level comparisons, with detailed comments explaining the precision rationale

Notable Implementation Details

  • The liveToken memoization searches across all chain token arrays to find the matching token, falling back to the stored selectedToken if not found
  • Balance validation now uses parseUnits with try-catch to handle invalid input gracefully
  • The "Max" button now formats amounts directly from BigInt to ensure perfect round-trip through parseUnits, preventing on-chain transfer reverts due to rounding
  • Loading state logic updated to only show "..." when actively loading AND balance is zero

https://claude.ai/code/session_01GjrKxQrVGUS4JQxMxEkHxa

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.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
solid-app Ignored Ignored May 26, 2026 12:54pm
solid-app-staging Ignored Ignored May 26, 2026 12:54pm

Request Review

@MusabShakeel576 MusabShakeel576 merged commit d511a25 into qa May 26, 2026
4 of 5 checks passed
@MusabShakeel576 MusabShakeel576 deleted the claude/inspiring-hamilton-ZvZYP branch May 26, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants