Skip to content

fix: resolve WalletProvider requireWallet stale closure (Closes #490) - #575

Open
waterWang wants to merge 1 commit into
Protocol-Guild:mainfrom
waterWang:fix/490-walletprovider-stale-closure
Open

fix: resolve WalletProvider requireWallet stale closure (Closes #490)#575
waterWang wants to merge 1 commit into
Protocol-Guild:mainfrom
waterWang:fix/490-walletprovider-stale-closure

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Fixes the stale closure in requireWallet that caused the first transaction after wallet connect to always fail with "Wallet connection required".

Root Cause

requireWallet captured address from the render where it was defined. After connect() resolved, the state update from onWalletSelected (which calls setAddress) hadn't been applied yet, so the post-modal check if (!address) always read the stale null.

Changes

  • addressRef (useRef): Kept in sync with address state via useEffect, so async callbacks always read the latest value
  • onWalletSelected: Writes addressRef.current synchronously — even before the state update renders, requireWallet sees the fresh address
  • connect/requireWallet: Wrapped in useCallback to prevent unnecessary re-renders

Verification

  • npx tsc --noEmit — passes
  • npm run lint — 0 errors, 0 warnings
  • npx prettier --check — passes

Acceptance Criteria

  • First transaction after wallet connect succeeds
  • No stale closure in requireWallet
  • Wallet modal closes and activates callback

…col-Guild#490)

requireWallet captured `address` from the render where it was defined.
After connect() resolves, the state update from onWalletSelected hasn't
been applied yet, so the post-modal check always read the stale null and
threw "Wallet connection required" even though the user just connected.

- Keep addressRef in sync with the address state via useEffect
- Write addressRef.current synchronously in onWalletSelected so
  requireWallet sees the fresh value without waiting for a re-render
- Wrap connect/requireWallet in useCallback to avoid re-renders
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.

1 participant