Fix stale isAdded state, 4902 recursion, and async race in useNetwork - #664
Open
bravesasha wants to merge 1 commit into
Open
Conversation
- 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
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.
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.