-
-
Notifications
You must be signed in to change notification settings - Fork 295
fix(assets-controller): clean up unused assetsInfo/assetsPrice entries on startup #9806
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
Open
Prithpal-Sooriya
wants to merge
14
commits into
main
Choose a base branch
from
cursor/assets-cleanup-unused-metadata-bedb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+451
−1
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
004d52c
Add cleanupUnusedMetadata to prune unreferenced assetsInfo/assetsPric…
cursoragent 68e24b5
Add changelog entry for unused metadata cleanup
cursoragent 640aba2
Extract isNativeAssetId into native-assets and remove type-level impo…
cursoragent d05dff6
Remove type assertions at cleanupUnusedMetadata call sites
cursoragent 2dbf926
Only run unused-metadata cleanup after a successful startup refresh
cursoragent 5029578
Condense cleanupUnusedMetadata: trim comments, dedupe types, use flat…
cursoragent 684370c
Convert isNativeAssetId tests to a parameterized table
cursoragent 06154e5
Shorten changelog entry for unused metadata cleanup
cursoragent f6967a4
Trim isNativeAssetId comments and drop lazy-init helper
cursoragent 06bda7f
Convert cleanupUnusedMetadata tests to a parameterized table
cursoragent 26d01e4
Remove comment above startup cleanup call
cursoragent e883fff
Remove CleanupUnusedMetadataState doc comment
cursoragent 7976fd0
Shorten cleanupUnusedMetadata JSDoc
cursoragent ff39d6b
Extract kept-asset ID sources into named variables
cursoragent 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
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
262 changes: 262 additions & 0 deletions
262
packages/assets-controller/src/utils/cleanupUnusedMetadata.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,262 @@ | ||
| import type { AssetsControllerState } from '../AssetsController.js'; | ||
| import type { AssetMetadata, AssetPrice, Caip19AssetId } from '../types.js'; | ||
| import { cleanupUnusedMetadata } from './cleanupUnusedMetadata.js'; | ||
|
|
||
| const SELECTED_ACCOUNT = 'account-1'; | ||
| const OTHER_ACCOUNT = 'account-2'; | ||
|
|
||
| /** USDC on mainnet (checksummed). */ | ||
| const HELD_ASSET = 'eip155:1/erc20:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; | ||
| /** DAI on mainnet. */ | ||
| const UNREFERENCED_ASSET = | ||
| 'eip155:1/erc20:0x6B175474E89094C44Da98b954EedeAC495271d0F'; | ||
| /** USDT on mainnet. */ | ||
| const ZERO_BALANCE_ASSET = | ||
| 'eip155:1/erc20:0xdAC17F958D2ee523a2206206994597C13D831ec7'; | ||
| /** cbETH on Base. */ | ||
| const CUSTOM_ASSET = | ||
| 'eip155:8453/erc20:0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22'; | ||
| /** ETH on mainnet. */ | ||
| const NATIVE_SLIP44_ASSET = 'eip155:1/slip44:60'; | ||
| /** SOL — slip44 native on a chain outside the hardcoded (EVM-only) registry. */ | ||
| const NATIVE_SOLANA_ASSET = | ||
| 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501'; | ||
| /** Zero-address ERC-20 native convention on a chain with no registry entry. */ | ||
| const NATIVE_ZERO_ADDRESS_ASSET = | ||
| 'eip155:424242/erc20:0x0000000000000000000000000000000000000000'; | ||
| /** METIS — native only recognizable through the hardcoded registry. */ | ||
| const NATIVE_REGISTRY_ASSET = | ||
| 'eip155:1088/erc20:0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000'; | ||
| /** mUSD on Monad — default tracked, has metadata but no balance until the chain is enabled. */ | ||
| const MUSD_ON_MONAD_ASSET = | ||
| 'eip155:143/erc20:0xacA92E438df0B2401fF60dA7E4337B687a2435DA'; | ||
| /** BAYC #1234 — NFT asset IDs carry a tokenId suffix. */ | ||
| const NFT_ASSET = | ||
| 'eip155:1/erc721:0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D/1234'; | ||
|
|
||
| function buildMetadata(symbol: string): AssetMetadata { | ||
| return { type: 'erc20', symbol, name: symbol, decimals: 18 }; | ||
| } | ||
|
|
||
| function buildPrice(value: number): AssetPrice { | ||
| return { | ||
| assetPriceType: 'fungible', | ||
| price: value, | ||
| usdPrice: value, | ||
| lastUpdated: 1700000000000, | ||
| }; | ||
| } | ||
|
|
||
| function buildState( | ||
| overrides: Partial<AssetsControllerState> = {}, | ||
| ): AssetsControllerState { | ||
| return { | ||
| assetsInfo: {}, | ||
| assetsBalance: {}, | ||
| assetsPrice: {}, | ||
| customAssets: {}, | ||
| assetPreferences: {}, | ||
| selectedCurrency: 'usd', | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| type CleanupCase = { | ||
| description: string; | ||
| assetId: string; | ||
| /** State slices referencing the asset (none means unreferenced). */ | ||
| references?: Partial<AssetsControllerState>; | ||
| expectKept: boolean; | ||
| }; | ||
|
|
||
| const cleanupCases: CleanupCase[] = [ | ||
| { | ||
| description: 'removes an asset that nothing references', | ||
| assetId: UNREFERENCED_ASSET, | ||
| expectKept: false, | ||
| }, | ||
| { | ||
| description: 'removes a malformed asset ID that nothing references', | ||
| assetId: 'not-a-caip-id', | ||
| expectKept: false, | ||
| }, | ||
| { | ||
| description: 'removes an unreferenced NFT asset ID', | ||
| assetId: NFT_ASSET, | ||
| expectKept: false, | ||
| }, | ||
| { | ||
| description: 'keeps an asset with a non-zero balance', | ||
| assetId: HELD_ASSET, | ||
| references: { | ||
| assetsBalance: { [SELECTED_ACCOUNT]: { [HELD_ASSET]: { amount: '5' } } }, | ||
| }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps an asset whose only balance entry is a zero amount', | ||
| assetId: ZERO_BALANCE_ASSET, | ||
| references: { | ||
| assetsBalance: { | ||
| [SELECTED_ACCOUNT]: { [ZERO_BALANCE_ASSET]: { amount: '0' } }, | ||
| }, | ||
| }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps an asset held only by a non-selected account', | ||
| assetId: HELD_ASSET, | ||
| references: { | ||
| assetsBalance: { | ||
| [SELECTED_ACCOUNT]: {}, | ||
| [OTHER_ACCOUNT]: { [HELD_ASSET]: { amount: '42' } }, | ||
| }, | ||
| }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps an asset that is only referenced by customAssets', | ||
| assetId: CUSTOM_ASSET, | ||
| references: { customAssets: { [SELECTED_ACCOUNT]: [CUSTOM_ASSET] } }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps an asset whose balance key differs in casing', | ||
| assetId: HELD_ASSET, | ||
| references: { | ||
| assetsBalance: { | ||
| [SELECTED_ACCOUNT]: { [HELD_ASSET.toLowerCase()]: { amount: '1' } }, | ||
| }, | ||
| }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps an asset whose customAssets entry differs in casing', | ||
| assetId: CUSTOM_ASSET, | ||
| references: { | ||
| customAssets: { | ||
| [SELECTED_ACCOUNT]: [CUSTOM_ASSET.toLowerCase() as Caip19AssetId], | ||
| }, | ||
| }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps a malformed asset ID that a balance entry references', | ||
| assetId: 'not-a-caip-id', | ||
| references: { | ||
| assetsBalance: { | ||
| [SELECTED_ACCOUNT]: { 'not-a-caip-id': { amount: '1' } }, | ||
| }, | ||
| }, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps a slip44 native asset', | ||
| assetId: NATIVE_SLIP44_ASSET, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps a slip44 native on a chain outside the native registry', | ||
| assetId: NATIVE_SOLANA_ASSET, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps a zero-address ERC-20 native', | ||
| assetId: NATIVE_ZERO_ADDRESS_ASSET, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: 'keeps a registry-only native (METIS dead address)', | ||
| assetId: NATIVE_REGISTRY_ASSET, | ||
| expectKept: true, | ||
| }, | ||
| { | ||
| description: | ||
| 'keeps a default tracked asset with no balance (mUSD on a disabled chain)', | ||
| assetId: MUSD_ON_MONAD_ASSET, | ||
| expectKept: true, | ||
| }, | ||
| ]; | ||
|
|
||
| describe('cleanupUnusedMetadata', () => { | ||
| it.each(cleanupCases)( | ||
| '$description', | ||
| ({ assetId, references = {}, expectKept }) => { | ||
| const state = buildState({ | ||
| assetsInfo: { [assetId]: buildMetadata('TEST') }, | ||
| assetsPrice: { [assetId]: buildPrice(1) }, | ||
| ...references, | ||
| }); | ||
|
|
||
| cleanupUnusedMetadata(state); | ||
|
|
||
| expect(state.assetsInfo).toStrictEqual( | ||
| expectKept ? { [assetId]: buildMetadata('TEST') } : {}, | ||
| ); | ||
| expect(state.assetsPrice).toStrictEqual( | ||
| expectKept ? { [assetId]: buildPrice(1) } : {}, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it('removes only unreferenced entries, leaving referenced ones in place', () => { | ||
| const state = buildState({ | ||
| assetsInfo: { | ||
| [HELD_ASSET]: buildMetadata('USDC'), | ||
| [UNREFERENCED_ASSET]: buildMetadata('DAI'), | ||
| }, | ||
| assetsPrice: { | ||
| [HELD_ASSET]: buildPrice(1), | ||
| [UNREFERENCED_ASSET]: buildPrice(1), | ||
| }, | ||
| assetsBalance: { | ||
| [SELECTED_ACCOUNT]: { [HELD_ASSET]: { amount: '5000000' } }, | ||
| }, | ||
| }); | ||
|
|
||
| cleanupUnusedMetadata(state); | ||
|
|
||
| expect(state.assetsInfo).toStrictEqual({ | ||
| [HELD_ASSET]: buildMetadata('USDC'), | ||
| }); | ||
| expect(state.assetsPrice).toStrictEqual({ [HELD_ASSET]: buildPrice(1) }); | ||
| }); | ||
|
|
||
| it('removes an unreferenced price entry even when the asset has no assetsInfo entry', () => { | ||
| const state = buildState({ | ||
| assetsPrice: { [UNREFERENCED_ASSET]: buildPrice(1) }, | ||
| }); | ||
|
|
||
| cleanupUnusedMetadata(state); | ||
|
|
||
| expect(state.assetsPrice).toStrictEqual({}); | ||
| }); | ||
|
|
||
| it('leaves assetPreferences untouched, including entries for removed assets', () => { | ||
| const state = buildState({ | ||
| assetsInfo: { [UNREFERENCED_ASSET]: buildMetadata('DAI') }, | ||
| assetsPrice: { [UNREFERENCED_ASSET]: buildPrice(1) }, | ||
| assetPreferences: { | ||
| [UNREFERENCED_ASSET]: { hidden: true }, | ||
| [HELD_ASSET]: { hidden: false }, | ||
| }, | ||
| }); | ||
|
|
||
| cleanupUnusedMetadata(state); | ||
|
|
||
| expect(state.assetsInfo).toStrictEqual({}); | ||
| expect(state.assetsPrice).toStrictEqual({}); | ||
| expect(state.assetPreferences).toStrictEqual({ | ||
| [UNREFERENCED_ASSET]: { hidden: true }, | ||
| [HELD_ASSET]: { hidden: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('does nothing on empty state', () => { | ||
| const state = buildState(); | ||
|
|
||
| cleanupUnusedMetadata(state); | ||
|
|
||
| expect(state).toStrictEqual(buildState()); | ||
| }); | ||
| }); |
42 changes: 42 additions & 0 deletions
42
packages/assets-controller/src/utils/cleanupUnusedMetadata.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,42 @@ | ||||||||||||||
| import { DEFAULT_TRACKED_ASSETS_BY_CHAIN } from '../defaults.js'; | ||||||||||||||
| import type { AssetsControllerStateInternal } from '../types.js'; | ||||||||||||||
| import { isNativeAssetId } from './native-assets.js'; | ||||||||||||||
|
|
||||||||||||||
| type AssetIdKeyedRecord = Record<string, unknown>; | ||||||||||||||
|
|
||||||||||||||
| export type CleanupUnusedMetadataState = { | ||||||||||||||
| assetsInfo: AssetIdKeyedRecord; | ||||||||||||||
| assetsBalance: Record<string, AssetIdKeyedRecord>; | ||||||||||||||
| assetsPrice: AssetIdKeyedRecord; | ||||||||||||||
| customAssets: AssetsControllerStateInternal['customAssets']; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Delete `assetsInfo` / `assetsPrice` entries for assets that are not held, | ||||||||||||||
| * custom, default tracked, or native. | ||||||||||||||
| * | ||||||||||||||
| * @param state - The controller state to clean up (mutated in place). | ||||||||||||||
| */ | ||||||||||||||
| export function cleanupUnusedMetadata(state: CleanupUnusedMetadataState): void { | ||||||||||||||
| const defaultTrackedAssetIds = [ | ||||||||||||||
| ...DEFAULT_TRACKED_ASSETS_BY_CHAIN.values(), | ||||||||||||||
| ].flat(); | ||||||||||||||
| const heldAssetIds = Object.values(state.assetsBalance).flatMap( | ||||||||||||||
| (accountBalances) => Object.keys(accountBalances), | ||||||||||||||
| ); | ||||||||||||||
| const customAssetIds = Object.values(state.customAssets).flat(); | ||||||||||||||
|
|
||||||||||||||
| const keptAssetIds = new Set( | ||||||||||||||
| [...defaultTrackedAssetIds, ...heldAssetIds, ...customAssetIds].map( | ||||||||||||||
| (assetId) => assetId.toLowerCase(), | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| const isUnused = (assetId: string): boolean => | ||||||||||||||
| !keptAssetIds.has(assetId.toLowerCase()) && !isNativeAssetId(assetId); | ||||||||||||||
|
|
||||||||||||||
| for (const slice of [state.assetsInfo, state.assetsPrice]) { | ||||||||||||||
|
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. nit
Suggested change
|
||||||||||||||
| for (const assetId of Object.keys(slice).filter(isUnused)) { | ||||||||||||||
| delete slice[assetId]; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.