Skip to content

fix(assets-controllers): refetch snap balances after vault unlock when fetch was skipped while locked#8579

Open
khanti42 wants to merge 4 commits intomainfrom
fix/multichain-balances-refetch-after-unlock
Open

fix(assets-controllers): refetch snap balances after vault unlock when fetch was skipped while locked#8579
khanti42 wants to merge 4 commits intomainfrom
fix/multichain-balances-refetch-after-unlock

Conversation

@khanti42
Copy link
Copy Markdown
Contributor

@khanti42 khanti42 commented Apr 24, 2026

Explanation

  • Subscribe to KeyringController:stateChange in MultichainBalancesController.
  • When the vault transitions from locked → unlocked, call updateBalance for any snap-backed (non-EVM) account that still has no cached balance entries.

Problem

MultichainAssetsController:accountAssetListUpdated triggers balance fetches only when KeyringController:getState().isUnlocked is true. If that event fires while the vault is locked, the fetch is skipped and never retried after unlock, so the UI can show empty multichain token rows despite assets in MultichainAssetsController state.

Consumer note

Extension (and any other consumer) must allow KeyringController:stateChange on the MultichainBalancesController restricted messenger; otherwise this subscription will not receive events.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Adds a new KeyringController:stateChange subscription and triggers balance refetches on unlock, which can change runtime behavior and requires consumers to allow an additional messenger event.

Overview
Ensures MultichainBalancesController retries fetching balances after a vault unlock by subscribing to KeyringController:stateChange and, on a locked→unlocked transition, calling updateBalance for non-EVM accounts that still have no cached balances.

Updates the controller’s allowed messenger events (and test messenger delegation) to include KeyringController:stateChange, and documents the fix in the assets-controllers changelog.

Reviewed by Cursor Bugbot for commit 9b21bf2. Bugbot is set up for automated code reviews on this repo. Configure here.

@khanti42 khanti42 requested review from a team as code owners April 24, 2026 15:37
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9b21bf2. Configure here.

for (const account of this.#listAccounts()) {
const hasBalance =
this.state.balances[account.id] &&
Object.keys(this.state.balances[account.id]).length > 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlock handler skips accounts with existing partial balances

Medium Severity

The hasBalance check only triggers updateBalance for accounts with a completely empty balance map. Since balances state is persisted (persist: true), an account can have existing balance entries from a prior session while new assets get added via accountAssetListUpdated while locked. On unlock, the account already has entries so hasBalance is true, and the newly-added assets never get their balances fetched — the exact scenario this PR aims to fix, but only partially addressed.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9b21bf2. Configure here.

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