Skip to content

fix/account-tracker-prevent-empty-state-update #5942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/assets-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Prevented `AccountTrackerController` from updating state with empty or unchanged account balance data during refresh ([#5942](https://github.com/MetaMask/core/pull/5942))
- Added guards to skip state updates when fetched balances are empty or identical to existing state
- Reduces unnecessary `stateChange` emissions and preserves previously-cached balances under network failure scenarios

## [68.1.0]

### Added
Expand Down
25 changes: 19 additions & 6 deletions packages/assets-controllers/src/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { StaticIntervalPollingController } from '@metamask/polling-controller';
import type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';
import { assert } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { cloneDeep } from 'lodash';
import { cloneDeep, isEqual } from 'lodash';

import type {
AssetsContractController,
Expand Down Expand Up @@ -396,12 +396,25 @@ export class AccountTrackerController extends StaticIntervalPollingController<Ac

// Update the state once all networkClientId updates are completed
allResults.forEach((result) => {
if (result.status === 'fulfilled') {
const { chainId, accountsForChain } = result.value;
this.update((state) => {
state.accountsByChainId[chainId] = accountsForChain;
});
if (result.status !== 'fulfilled') {
return;
}
const { chainId, accountsForChain } = result.value;

// skip if nothing was fetched
if (Object.keys(accountsForChain).length === 0) {
return;
}

// skip if nothing actually changed
if (isEqual(this.state.accountsByChainId[chainId], accountsForChain)) {
return;
}

// only mutate the state if there is something to update
this.update((state) => {
state.accountsByChainId[chainId] = accountsForChain;
});
});
} finally {
releaseLock();
Expand Down
Loading