Skip to content
Draft
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
4 changes: 4 additions & 0 deletions packages/assets-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Preserve existing per-asset balance fields when applying merge-mode balance updates, so amount-only updates do not drop extra fields already present on the prior balance ([#9453](https://github.com/MetaMask/core/pull/9453))

## [10.2.0]

### Added
Expand Down
34 changes: 34 additions & 0 deletions packages/assets-controller/src/AssetsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,40 @@ describe('AssetsController', () => {
});
});

it('preserves extra balance fields when merge update changes amount', async () => {
const initialState: Partial<AssetsControllerState> = {
assetsBalance: {
[MOCK_ACCOUNT_ID]: {
[MOCK_ASSET_ID]: {
amount: '1',
metadata: { limit: '1000', authorized: true },
},
},
},
};

await withController({ state: initialState }, async ({ controller }) => {
await controller.handleAssetsUpdate(
{
updateMode: 'merge',
assetsBalance: {
[MOCK_ACCOUNT_ID]: {
[MOCK_ASSET_ID]: { amount: '2' },
},
},
},
'TestSource',
);

expect(
controller.state.assetsBalance[MOCK_ACCOUNT_ID]?.[MOCK_ASSET_ID],
).toStrictEqual({
amount: '2',
metadata: { limit: '1000', authorized: true },
});
});
});

it('preserves existing balances when merge update adds new chain data', async () => {
const polygonNative = 'eip155:137/slip44:966' as Caip19AssetId;
const initialState: Partial<AssetsControllerState> = {
Expand Down
11 changes: 10 additions & 1 deletion packages/assets-controller/src/AssetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,16 @@ function mergeAccountBalances(
replaceCoveredChains: boolean,
): Record<string, AssetBalance> {
if (!replaceCoveredChains) {
return { ...previousBalances, ...accountBalances };
// Per-asset shallow merge so amount updates do not drop extra fields
// already on the prior balance (e.g. enrichment metadata).
const next: Record<string, AssetBalance> = { ...previousBalances };
for (const [assetId, balance] of Object.entries(accountBalances)) {
next[assetId] = {
...(previousBalances[assetId] ?? { amount: '0' }),
...balance,
};
}
return next;
}

const coveredChains = new Set(
Expand Down