fix: wallet init race lets external connect win before node is set#19
Open
alejoamiras wants to merge 1 commit into
Open
fix: wallet init race lets external connect win before node is set#19alejoamiras wants to merge 1 commit into
alejoamiras wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Problem
Clicking Connect Wallet while the embedded wallet is still initializing, then completing the external wallet flow before
createEmbeddedWalletresolves, throws"Wallet not initialized"fromContractsContext.registerBaseContractsand leaves the "Connecting to network..." banner stuck.Cause
nodeis only assigned to reducer state viawallet/INIT_EMBEDDED. That dispatch is gated on!hasConnectedExternalWalletRef.current.confirmConnectionflips the ref to true before the slowcreateEmbeddedWalletresolves, so the gate fails,INIT_EMBEDDEDis skipped, andnodestays null.SET_EXTERNALdid not touchnodeorisLoading, so both stay stale.Fix
Decouple node lifecycle from embedded wallet lifecycle. The node belongs to the network, not the wallet.
wallet/SET_NODEaction publishes the node synchronously before awaitingcreateEmbeddedWallet. External flows no longer wait on the slow embedded init.SET_EXTERNALclearsisLoadinganderror. The user has a usable wallet at that point.RESTORE_EMBEDDEDclearsisLoadingfor symmetry.initIdRefgeneration counter on the init effect. A stalecreateEmbeddedWalletresolving after a network switch checks it and bails before overwriting current state.embeddedWalletRefandembeddedAddressRefat init start so a network switch can't restore the previous network's wallet on later disconnect.disconnectWalletfalls back toactions.disconnect()when there's no embedded wallet to restore. Otherwise the disconnected external wallet stays visible in state.Test
"Wallet not initialized"inregisterBaseContracts, banner persists.