fix: handle wallet account switch correctly#166
Open
0xEdouardEth wants to merge 2 commits intosolana-foundation:mainfrom
Open
fix: handle wallet account switch correctly#1660xEdouardEth wants to merge 2 commits intosolana-foundation:mainfrom
0xEdouardEth wants to merge 2 commits intosolana-foundation:mainfrom
Conversation
## Summary
- When a user switches accounts in their wallet (e.g. Phantom), `onAccountsChanged` was already
handling the empty-accounts case (disconnect) but silently ignored the non-empty case; leaving
the store with a stale session reference so React components (address display, balance, etc.)
would not re-render with the new account.
- Added the missing `else` branch in `connectWallet` to call `updateState` with the new
`accounts[0]` when the wallet reports a different account, keeping the store in sync.
- Added two unit tests covering both cases: account switch updates the store, and empty accounts
array triggers a full disconnect.
The wallet standard session had two independent mutable variables (currentAccount and sessionAccount) updated by account change listeners. This scattered mutation made it easy for future changes to update one but forget the other, leading to inconsistent state where signing operations could reference a stale account. This consolidates both into a single sessionState object, making the mutation surface explicit and co-located. The session's account property is now a getter that always reads from sessionState, so consumers always see the latest account after a wallet-side switch instead of a stale snapshot captured at session creation time.
|
@0xEdouardEth is attempting to deploy a commit to the Solana Foundation Team on Vercel. A member of the Team first needs to authorize it. |
f129370 to
b5b2d88
Compare
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.
Summary
When a user switches accounts in their wallet, the app failed to reflect the change properly:
This PR fixes both issues:
onAccountsChangedlistener now updates the store with the new account (and disconnects gracefully when no accounts remain)sessionStateobject with a getter, so all signing operations always read the latest accountTest plan