Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@
"count": 1
},
"no-restricted-syntax": {
"count": 2
"count": 1
}
},
"packages/assets-controllers/src/NftController.test.ts": {
Expand Down
5 changes: 5 additions & 0 deletions packages/assets-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `MultichainBalancesController` no longer silently drops balance updates from `AccountsController:accountBalancesUpdated` when the account does not yet have an entry in `state.balances` ([#8484](https://github.com/MetaMask/core/pull/8484))
- Previously, if a Snap sent balance updates before `updateBalance` had initialized `state.balances[accountId]`, the update was ignored because of an `accountId in state.balances` guard. The handler now initializes the entry before merging, ensuring balances are always persisted.

## [104.1.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,34 @@ describe('MultichainBalancesController', () => {
);
});

it('initializes and stores balances when "AccountsController:accountBalancesUpdated" fires before the account has an entry in state', async () => {
const { controller, messenger } = setupController({
state: { balances: {} },
mocks: {
listMultichainAccounts: [],
},
});

expect(controller.state.balances[mockBtcAccount.id]).toBeUndefined();

const balanceUpdate = {
balances: {
[mockBtcAccount.id]: mockBalanceResult,
},
};

messenger.publish(
'AccountsController:accountBalancesUpdated',
balanceUpdate,
);

await waitForAllPromises();

expect(controller.state.balances[mockBtcAccount.id]).toStrictEqual(
mockBalanceResult,
);
});

it('updates balances when receiving "AccountsController:accountBalancesUpdated" event', async () => {
const mockInitialBalances = {
[mockBtcNativeAsset]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ export class MultichainBalancesController extends BaseController<
this.update((state: Draft<MultichainBalancesControllerState>) => {
Object.entries(balanceUpdate.balances).forEach(
([accountId, assetBalances]) => {
if (accountId in state.balances) {
Object.assign(state.balances[accountId], assetBalances);
}
state.balances[accountId] ??= {};
Object.assign(state.balances[accountId], assetBalances);
},
);
});
Expand Down
Loading