fix(AccountTrackerController): prevent balance wipe during account sync#8505
Merged
Prithpal-Sooriya merged 1 commit intomainfrom Apr 17, 2026
Merged
Conversation
7da1ced to
072248e
Compare
Added a test to ensure that existing balances are preserved when syncing accounts, specifically when the selected chain has no state entry. Updated the controller logic to only initialize new addresses with a zero balance if they do not already exist in the accountsByChainId state, preventing unintended overwrites of existing balances.
072248e to
87a6b6a
Compare
salimtb
reviewed
Apr 17, 2026
| accountsByChainId[chainId][address] = { | ||
| balance: '0x0', | ||
| }; | ||
| if (!accountsByChainId[chainId][address]) { |
salimtb
approved these changes
Apr 17, 2026
juanmigdr
approved these changes
Apr 17, 2026
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.
Explanation
Added a test to ensure that existing balances are preserved when syncing accounts, specifically when the selected chain has no state entry. Updated the controller logic to only initialize new addresses with a zero balance if they do not already exist in the accountsByChainId state, preventing unintended overwrites of existing balances.
References
MetaMask/metamask-extension#40474
Checklist
Note
Medium Risk
Touches core balance state mutation logic; a small conditional change could affect how new accounts/chains are initialized, so regressions would surface as missing/incorrect balances across networks.
Overview
Prevents
AccountTrackerController.#syncAccountsfrom overwriting existing address entries (and theirstakedBalance) when syncing accounts for a chain that previously had noaccountsByChainIdentry, by only initializing{ balance: '0x0' }when the address key is absent.Adds a regression test covering the scenario where refreshing on a new chain should initialize the new chain’s account state without wiping balances on other chains, and documents the fix in the
assets-controllerschangelog.Reviewed by Cursor Bugbot for commit 87a6b6a. Bugbot is set up for automated code reviews on this repo. Configure here.