Skip to content

Fix stale isAdded state, 4902 recursion, and async race in useNetwork - #664

Open
bravesasha wants to merge 1 commit into
inkonchain:mainfrom
bravesasha:fix/network-isAdded-stale-state-and-4902-recursion
Open

Fix stale isAdded state, 4902 recursion, and async race in useNetwork#664
bravesasha wants to merge 1 commit into
inkonchain:mainfrom
bravesasha:fix/network-isAdded-stale-state-and-4902-recursion

Conversation

@bravesasha

Copy link
Copy Markdown

Fixed a stale-state bug in the useNetwork hook where isAdded was implemented as a sticky OR flag that never reset when the network argument changed, causing it to incorrectly report true for a network that was never actually added to the wallet after switching away from one that was; this could cause the UI to skip wallet_addEthereumChain and call wallet_switchEthereumChain directly, failing with error 4902. The fix resets isAdded/isSelected on network change, adds a 4902 fallback in selectNetwork that triggers addNetwork, makes addNetwork issue its own explicit switch call after adding (since EIP-3085 doesn't guarantee auto-switch) so isSelected reflects the real outcome instead of being assumed true, and guards the async eth_chainId check with a cancellation flag to prevent a stale lookup from a previous network overwriting state for the new one.

- Reset isAdded/isSelected when the network argument changes, so a
  sticky true value from a previously selected network no longer
  leaks into a newly requested network
- Add a 4902 (unrecognized chain) fallback in selectNetwork that
  triggers addNetwork instead of failing silently
- Make addNetwork issue its own explicit wallet_switchEthereumChain
  call after a successful add, since EIP-3085 does not guarantee the
  wallet switches to a newly added chain; isSelected now reflects
  the actual switch result rather than being assumed true
- Guard checkNetwork's async chainId lookup with a cancellation flag
  so a stale result from a previous network can't overwrite state
  after the network prop has already changed
- Document isAdded as a best-effort heuristic, since neither
  eth_chainId nor chainChanged can confirm a chain is known to the
  wallet independent of it being active
@bravesasha
bravesasha requested a review from a team as a code owner September 6, 2026 15:50
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