Repository navigation
feat(wallet): add @metamask/assets-controllers to the default initialization configurations - #11315
dawnseeker8 wants to merge 28 commits into
Conversation
… into init Constructors of TokensController, TokenBalancesController, TokenDetectionController, TokenRatesController, AccountTrackerController, NftController, MultichainBalancesController, MultichainAssetsRatesController and MultichainAssetsController no longer call other controllers or start background work. The work moves to a new idempotent init method. TokenListController gains init, which calls initialize once. NftController exposes addNfts through its messenger, and TokensController falls back to the selected network client's provider. Co-authored-by: Xiaoming Wang <[email protected]>
Co-authored-by: Xiaoming Wang <[email protected]>
…gurations Adds the 16 messenger-based @metamask/assets-controllers controllers to the wallet's default initialization configurations. Each configuration delegates the actions and events its controller's messenger declares, builds callback options from messenger calls so construction order does not matter, and defaults the token prices and token list services. Wallet.init() runs the controllers' init methods. Co-authored-by: Xiaoming Wang <[email protected]>
Co-authored-by: Xiaoming Wang <[email protected]>
The Check Changelog workflow requires Unreleased entries to reference this pull request. Co-authored-by: Cursor <[email protected]>
Keep the assets-controllers entries for this pull request in Unreleased. The merge had placed them under the released 113.2.0 section. Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md
TokenRatesController applies native asset identifiers from later NetworkEnablementController updates, and TokenDetectionController reads useTokenDetection again when it is still unset at detection time. Co-authored-by: Xiaoming Wang <[email protected]>
Bump the wallet's @metamask/assets-controllers range to ^113.3.0 so yarn install --immutable passes, and move this PR's changelog entries out of the released 113.3.0 section back to Unreleased. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
Keep this branch's unreleased changelog entries and main's released 113.3.1 and 21.0.1 sections. Point @metamask/wallet at @metamask/assets-controllers ^113.3.1 so the workspace range matches the released package. Co-authored-by: Xiaoming Wang <[email protected]>
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md # packages/wallet/CHANGELOG.md
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]>
…er is registered The first re-scan runs during init, before a client may have registered PhishingController. Retry that miss every minute, then return to the configured interval once bulkScanTokens can be called. Co-authored-by: Xiaoming Wang <[email protected]>
…controllers-draft-1943 Keep this branch's unreleased assets-controllers entries and main's profile-sync-controller bump to ^36.1.1. Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md
Prithpal-Sooriya
left a comment
There was a problem hiding this comment.
LGTM! Most of the legacy AssetControllers will be removed, but we can do this as a separate task - nice work!
Do we want to refrain from adding them to |
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md
hi, @FrederikBolding , @Prithpal-Sooriya mentioned that since we are still in the middle of moving |
|
Hey @FrederikBolding @dawnseeker8 I'd say most of these controllers have been deprecated, but there are some that we will keep and not remove anytime soon - NFTController and Deficontroller. I think I agree, if we want, we can fully remove all these other controllers that aren't being used (e.g. CurrencyRateController, TokensController, etc), but we're also making sure that both mobile and extension clients have fully stopped using them (https://consensyssoftware.atlassian.net/browse/ASSETS-4135 // https://consensyssoftware.atlassian.net/browse/ASSETS-4141) |
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md # packages/wallet/CHANGELOG.md
🛑 Files requiring codeowner reviewTo merge this PR, you'll need to get approvals from the following owners. @MetaMask/core-platform (27 files)
@MetaMask/metamask-assets (27 files)
@MetaMask/core-platform (expand for more) |
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md # packages/wallet/CHANGELOG.md
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]>
MultichainBalancesController.init and TokensController.init awaited the initial non-EVM balance fetch and token-list enrichment. Those used to run fire-and-forget from the constructor, so a hung snap or token-list request kept Wallet.init() from settling. Start both in the background again, and keep logging or swallowing their failures. Co-authored-by: Xiaoming Wang <[email protected]>
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]>
…troller init test The init-pending test returned partial MultichainAssetsController and KeyringController states, which lint:tsc:check rejected. Co-authored-by: Xiaoming Wang <[email protected]>
NftDetectionController started with disabled false and only updated that flag on PreferencesController:stateChange, which is not replayed, so a persisted useNftDetection false was ignored. init now sets disabled from the current preference unless an explicit disabled option was passed. NftController and AssetsContractController likewise read persisted IPFS and NFT media preferences during init. Co-authored-by: Xiaoming Wang <[email protected]>
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]>
| this.#isIpfsGatewayEnabled = preferences.isIpfsGatewayEnabled; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Init can clobber NFT preference events
Medium Severity
PreferencesController:stateChange updates NFT preference fields but never sets #nftPreferencesRead, so a later init still calls getState and can overwrite those values. AssetsContractController and NftDetectionController set their equivalent flags in the event handler so a pre-init change is kept.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 1e537a0. Configure here.
…controllers-draft-1943 Co-authored-by: Xiaoming Wang <[email protected]> # Conflicts: # packages/assets-controllers/CHANGELOG.md # packages/wallet/CHANGELOG.md
Workspace constraints require dependents to match the version main released. Update the wallet dependency range, changelog, and lockfile. Co-authored-by: Xiaoming Wang <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e182d37. Configure here.
| async init(): Promise<void> { | ||
| this.#initPromise ??= this.initialize(); | ||
| await this.#initPromise; | ||
| } |
There was a problem hiding this comment.
Failed token list init never retries
Medium Severity
init() stores the first initialize() promise in #initPromise forever, while initialize() swallows errors and then clears #initializePromise so it can be tried again. A failed storage load therefore looks successful to init(), and a later init() (including a second Wallet.init()) does not reload the token-list cache or attach the persist subscription.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit e182d37. Configure here.


Explanation
@metamask/walletdoes not yet construct any of the legacy@metamask/assets-controllerscontrollers. Hosts construct them, and several constructors call other controllers immediately (AccountsController:getSelectedAccount,KeyringController:getState,TokensController:getState,NetworkController:getNetworkClientById, and others) or start async work. So the host has to construct them in a specific order, after their dependencies.This PR does two things, one commit each.
1.
@metamask/assets-controllers: constructors no longer call other controllers.The constructors of
TokensController,TokenBalancesController,TokenDetectionController,TokenRatesController,AccountTrackerController,NftController,MultichainBalancesController,MultichainAssetsRatesControllerandMultichainAssetsControllernow only register handlers and subscribe to events.accountsByChainId), move to a new idempotentinit()method.init()returns early when the controller is deprecated.TokenListControllergainsinit(), which callsinitialize()once.TokenDetectionController.init()reports a missingPreferencesControllerthroughmessenger.captureExceptioninstead of throwing. The nextPreferencesController:stateChangefills in the value.NftControllerexposesaddNftsthrough its messenger.TokensController'sproviderandchainIdare now optional. Without a provider it uses the selected network client's provider (new allowed actionNetworkController:getSelectedNetworkClient).Every changed controller has a test that constructs it on a messenger with no handlers registered, plus an idempotency test. Existing tests that relied on constructor side effects now call
init().2.
@metamask/wallet: the 16 messenger-based controllers become defaults.The configurations live in
src/initialization/instances/assets-controllers/, one file per controller in one folder, aspermission-controller/does. The codeowners script allows oneinitializationPathper package.getMessengerdelegates exactly the external actions and events the controller's messenger type declares. That includes controllers the client registers (PreferencesController,SnapController,PhishingController,AccountTreeController,AuthenticationController,AccountActivityService).AccountTrackerController.getStakedBalanceForChainandTokenDetectionController.getBalancesInSingleCallcallAssetsContractController.NftDetectionController.addNfts,removeNftandgetNftStatecallNftController.instanceOptions:CodefiTokenPricesServiceV2fortokenPricesService.TokenListServicefortokenListService.chainId.trackMetaMetricsEvent.NftDetectionControllerdisabled: falseandRatesControllerincludeUsdRate: false.Wallet.init()already callsinit()on every instance that has one, so it now starts the assets controllers too. Polling is still started by the client.DeFiPositionsControllerV2is not included. It is newer, not in the ticket's scope, and can be added separately.Notes for reviewers
PreferencesControllerdependency appears at runtime, not at construction.AccountTrackerController.refresh(triggered byAccountsController:selectedEvmAccountChangeandNetworkController:networkAdded) callsPreferencesController:getStatewithout a guard. Hosts already registerPreferencesControllerbefore unlocking, soWallet.test.tsnow registers a stub for it in the tests that unlock or import an SRP. The wallet changelog calls this out.wallet.init()still has one rejected result,MultichainNetworkController:getStatefromNetworkEnablementController. It was already there before this PR and comes from another controller the wallet doesn't construct.assets-controllers.test.ts. It mocks@metamask/assets-controllerswith subclasses that record their constructor options, so the forwarded options, default services and messenger-backed callbacks can be checked directly. The instances are still real controllers.References
AssetsControllerin the wallet), but starts the deferred work fromWallet.init()instead of on the first UI open.@metamask/assets-controllersto the default initialization configurations #11314, which is ready for review oncursor/wallet-assets-controllers-1943.Checklist
Note
High Risk
Breaking constructor and messenger contracts across many asset controllers, plus new default wallet registrations that can collide with existing hosts and depend on runtime
PreferencesControllerregistration.Reviewed by Cursor Bugbot for commit e182d37. Bugbot is set up for automated code reviews on this repo. Configure here.