Skip to content

fix(AccountTrackerController): prevent balance wipe during account sync#8505

Merged
Prithpal-Sooriya merged 1 commit intomainfrom
fix-account-tracker-controller-account-sync-balance-reset
Apr 17, 2026
Merged

fix(AccountTrackerController): prevent balance wipe during account sync#8505
Prithpal-Sooriya merged 1 commit intomainfrom
fix-account-tracker-controller-account-sync-balance-reset

Conversation

@Prithpal-Sooriya
Copy link
Copy Markdown
Contributor

@Prithpal-Sooriya Prithpal-Sooriya commented Apr 17, 2026

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

  • 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
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.#syncAccounts from overwriting existing address entries (and their stakedBalance) when syncing accounts for a chain that previously had no accountsByChainId entry, 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-controllers changelog.

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

@Prithpal-Sooriya Prithpal-Sooriya force-pushed the fix-account-tracker-controller-account-sync-balance-reset branch from 7da1ced to 072248e Compare April 17, 2026 10:41
@Prithpal-Sooriya Prithpal-Sooriya marked this pull request as ready for review April 17, 2026 10:45
@Prithpal-Sooriya Prithpal-Sooriya requested review from a team as code owners April 17, 2026 10:45
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.
@Prithpal-Sooriya Prithpal-Sooriya force-pushed the fix-account-tracker-controller-account-sync-balance-reset branch from 072248e to 87a6b6a Compare April 17, 2026 10:47
accountsByChainId[chainId][address] = {
balance: '0x0',
};
if (!accountsByChainId[chainId][address]) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice catch

@Prithpal-Sooriya Prithpal-Sooriya added this pull request to the merge queue Apr 17, 2026
Merged via the queue into main with commit e976824 Apr 17, 2026
346 checks passed
@Prithpal-Sooriya Prithpal-Sooriya deleted the fix-account-tracker-controller-account-sync-balance-reset branch April 17, 2026 11:10
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.

3 participants