Skip to content

fix(MultichainBalancesController): initialize balance entry when snap update arrives before account is in state#8484

Open
salimtb wants to merge 3 commits intomainfrom
fix/multichain-balances-handle-uninitialized-account
Open

fix(MultichainBalancesController): initialize balance entry when snap update arrives before account is in state#8484
salimtb wants to merge 3 commits intomainfrom
fix/multichain-balances-handle-uninitialized-account

Conversation

@salimtb
Copy link
Copy Markdown
Contributor

@salimtb salimtb commented Apr 16, 2026

Previously, #handleOnAccountBalancesUpdated would silently drop balance updates if state.balances[accountId] had not yet been populated (i.e. the key was absent). This caused a race condition where a Snap could send balances before updateBalance had created the entry, leaving the account with no persisted balance.

The handler now initializes the entry with ??= before merging, consistent with every other code path in the controller that writes to state.balances.

Explanation

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

Low Risk
Small, localized state-update change with added test coverage; low chance of unintended side effects beyond persisting previously-dropped balance updates.

Overview
Fixes a race in MultichainBalancesController where balances published via AccountsController:accountBalancesUpdated could be ignored if state.balances[accountId] was not yet initialized.

The handler now always initializes state.balances[accountId] before merging updates, and adds a unit test covering the “event arrives before state entry exists” case; the assets-controllers changelog is updated accordingly.

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

salimtb added 3 commits April 16, 2026 12:24
… update arrives before account is in state

Previously, `#handleOnAccountBalancesUpdated` would silently drop balance
updates if `state.balances[accountId]` had not yet been populated (i.e. the
key was absent). This caused a race condition where a Snap could send balances
before `updateBalance` had created the entry, leaving the account with no
persisted balance.

The handler now initializes the entry with `??=` before merging, consistent
with every other code path in the controller that writes to `state.balances`.
@salimtb salimtb marked this pull request as ready for review April 16, 2026 10:32
@salimtb salimtb requested review from a team as code owners April 16, 2026 10:32
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