Repository navigation
fix(assets-controller): clean v5 native balance state update #10567
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
216 changes: 216 additions & 0 deletions
216
packages/assets-controller/src/migrations/dropBalancesOutsideAccountScopes.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import type { InternalAccount } from '@metamask/keyring-internal-api'; | ||
|
|
||
| import { createMockInternalAccount } from '../__fixtures__/MockAssetControllerMessenger.js'; | ||
| import type { Caip19AssetId } from '../types.js'; | ||
| import { dropBalancesOutsideAccountScopes } from './dropBalancesOutsideAccountScopes.js'; | ||
|
|
||
| const BITCOIN_ACCOUNT_ID = 'bitcoin-account-id'; | ||
| const EVM_ACCOUNT_ID = 'evm-account-id'; | ||
| const BITCOIN_NATIVE = | ||
| 'bip122:000000000019d6689c085ae165831e93/slip44:0' as Caip19AssetId; | ||
| const SOLANA_NATIVE = | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501' as Caip19AssetId; | ||
| const MAINNET_NATIVE = 'eip155:1/slip44:60' as Caip19AssetId; | ||
| const LINEA_NATIVE = 'eip155:59144/slip44:60' as Caip19AssetId; | ||
|
|
||
| const bitcoinAccount = (scopes: InternalAccount['scopes']): InternalAccount => | ||
| createMockInternalAccount({ | ||
| id: BITCOIN_ACCOUNT_ID, | ||
| address: 'bc1qdy9vjsg26rrd8tkqr7f26hlhd9524wyjkynemj', | ||
| type: 'bip122:p2wpkh', | ||
| scopes, | ||
| }); | ||
|
|
||
| const evmAccount = (scopes: InternalAccount['scopes']): InternalAccount => | ||
| createMockInternalAccount({ id: EVM_ACCOUNT_ID, scopes }); | ||
|
|
||
| describe('dropBalancesOutsideAccountScopes', () => { | ||
| it('drops balances whose chain namespace is outside the account scopes', () => { | ||
| const balances = { | ||
| [BITCOIN_ACCOUNT_ID]: { | ||
| [BITCOIN_NATIVE]: { amount: '0.00000404' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| [MAINNET_NATIVE]: { amount: '0' }, | ||
| }, | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '0.02' }, | ||
| [LINEA_NATIVE]: { amount: '0' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| bitcoinAccount(['bip122:000000000019d6689c085ae165831e93']), | ||
| evmAccount(['eip155:0']), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(true); | ||
| expect(balances[BITCOIN_ACCOUNT_ID]).toStrictEqual({ | ||
| [BITCOIN_NATIVE]: { amount: '0.00000404' }, | ||
| }); | ||
| expect(balances[EVM_ACCOUNT_ID]).toStrictEqual({ | ||
| [MAINNET_NATIVE]: { amount: '0.02' }, | ||
| [LINEA_NATIVE]: { amount: '0' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps every EVM chain when the account scope is the eip155 wildcard', () => { | ||
| const balances = { | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [LINEA_NATIVE]: { amount: '2' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| evmAccount(['eip155:0']), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(false); | ||
| expect(balances[EVM_ACCOUNT_ID]).toStrictEqual({ | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [LINEA_NATIVE]: { amount: '2' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps balances for every namespace the account scopes include', () => { | ||
| const balances = { | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '2' }, | ||
| [BITCOIN_NATIVE]: { amount: '3' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| evmAccount(['eip155:1', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp']), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(true); | ||
| expect(balances[EVM_ACCOUNT_ID]).toStrictEqual({ | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '2' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves malformed scopes and balance keys alone', () => { | ||
| const balances = { | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| 'not-a-caip-id': { amount: '0' }, | ||
| }, | ||
| } as Record<string, Record<Caip19AssetId, { amount: string }>>; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| evmAccount(['eip155:1', 'garbage' as `${string}:${string}`]), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(true); | ||
| expect(balances[EVM_ACCOUNT_ID]).toStrictEqual({ | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| 'not-a-caip-id': { amount: '0' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves an account untouched when none of its scopes are valid CAIP-2 ids', () => { | ||
| const balances = { | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| evmAccount(['garbage' as `${string}:${string}`]), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(false); | ||
| expect(balances[EVM_ACCOUNT_ID]).toStrictEqual({ | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves an account whose scopes are missing untouched', () => { | ||
| const balances = { | ||
| [BITCOIN_ACCOUNT_ID]: { | ||
| [BITCOIN_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }, | ||
| }; | ||
| const { scopes: _scopes, ...accountWithoutScopes } = bitcoinAccount([ | ||
| 'bip122:000000000019d6689c085ae165831e93', | ||
| ]); | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| accountWithoutScopes as InternalAccount, | ||
| ]); | ||
|
|
||
| expect(removed).toBe(false); | ||
| expect(balances[BITCOIN_ACCOUNT_ID]).toStrictEqual({ | ||
| [BITCOIN_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves an account with no scopes untouched', () => { | ||
| const balances = { | ||
| [BITCOIN_ACCOUNT_ID]: { | ||
| [BITCOIN_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| bitcoinAccount([]), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(false); | ||
| expect(balances[BITCOIN_ACCOUNT_ID]).toStrictEqual({ | ||
| [BITCOIN_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves an account that has no stored balances untouched', () => { | ||
| const balances = { | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| createMockInternalAccount({ | ||
| id: 'missing-account', | ||
| scopes: ['eip155:1'], | ||
| }), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(false); | ||
| expect(balances).toStrictEqual({ | ||
| [EVM_ACCOUNT_ID]: { | ||
| [MAINNET_NATIVE]: { amount: '1' }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves accounts that were not passed in untouched', () => { | ||
| const balances = { | ||
| [BITCOIN_ACCOUNT_ID]: { | ||
| [BITCOIN_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }, | ||
| }; | ||
|
|
||
| const removed = dropBalancesOutsideAccountScopes(balances, [ | ||
| evmAccount(['eip155:1']), | ||
| ]); | ||
|
|
||
| expect(removed).toBe(false); | ||
| expect(balances[BITCOIN_ACCOUNT_ID]).toStrictEqual({ | ||
| [BITCOIN_NATIVE]: { amount: '1' }, | ||
| [SOLANA_NATIVE]: { amount: '0' }, | ||
| }); | ||
| }); | ||
| }); |
84 changes: 84 additions & 0 deletions
84
packages/assets-controller/src/migrations/dropBalancesOutsideAccountScopes.ts
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect! Nicely done! |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import type { InternalAccount } from '@metamask/keyring-internal-api'; | ||
| import { | ||
| isCaipAssetType, | ||
| isCaipChainId, | ||
| parseCaipAssetType, | ||
| parseCaipChainId, | ||
| } from '@metamask/utils'; | ||
|
|
||
| import type { AccountId, AssetBalance, Caip19AssetId } from '../types.js'; | ||
|
|
||
| /** | ||
| * Remove balance entries the given accounts cannot own. | ||
| * | ||
| * Ownership is the chain namespace of the account's scopes, not the | ||
| * currently enabled chains. `eip155:0` keeps every EVM chain, including | ||
| * ones the user has turned off. An account with no scopes is left untouched, | ||
| * as is any account that is not in `accounts`. | ||
| * | ||
| * Mutates `balances`. Intended to run inside an Immer `update` draft, once | ||
| * per unlock, the same way spam cleanup is applied. | ||
| * | ||
| * @param balances - `assetsBalance` being updated. | ||
| * @param accounts - Accounts whose scopes define the namespaces to keep. | ||
| * @returns True when at least one balance entry was removed. | ||
| */ | ||
| export function dropBalancesOutsideAccountScopes( | ||
| balances: Record<AccountId, Record<Caip19AssetId, AssetBalance>>, | ||
| accounts: readonly InternalAccount[], | ||
| ): boolean { | ||
| let removed = false; | ||
| for (const account of accounts) { | ||
| if (dropAccountBalancesOutsideScopes(balances, account)) { | ||
| removed = true; | ||
| } | ||
| } | ||
| return removed; | ||
| } | ||
|
|
||
| /** | ||
| * Remove one account's balances whose chain namespace is outside its scopes. | ||
| * | ||
| * @param balances - `assetsBalance` being updated. | ||
| * @param account - Account whose scopes define the namespaces to keep. | ||
| * @returns True when at least one balance entry was removed. | ||
| */ | ||
| function dropAccountBalancesOutsideScopes( | ||
| balances: Record<AccountId, Record<Caip19AssetId, AssetBalance>>, | ||
| account: InternalAccount, | ||
| ): boolean { | ||
| // Persisted accounts can omit `scopes` even though the type requires it. | ||
| const scopes = account.scopes ?? []; | ||
| if (scopes.length === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| const accountBalances = balances[account.id]; | ||
| if (!accountBalances) { | ||
| return false; | ||
| } | ||
|
|
||
| const namespaces = new Set<string>(); | ||
| for (const scope of scopes) { | ||
| if (isCaipChainId(scope)) { | ||
| namespaces.add(parseCaipChainId(scope).namespace); | ||
| } | ||
| } | ||
| if (namespaces.size === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| let removed = false; | ||
| for (const assetId of Object.keys(accountBalances) as Caip19AssetId[]) { | ||
| // Malformed keys are left alone; this cleanup only targets valid CAIP-19 | ||
| // entries stamped onto the wrong account. | ||
| if (!isCaipAssetType(assetId)) { | ||
| continue; | ||
| } | ||
| if (!namespaces.has(parseCaipAssetType(assetId).chain.namespace)) { | ||
| delete accountBalances[assetId]; | ||
| removed = true; | ||
| } | ||
| } | ||
| return removed; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main fix and reason for this PR. The rest is mainly restoring the corrupted state. This logic will be gone with v6