Repository navigation
feat(wallet): add @metamask/assets-controllers to the default initialization configurations - #11314
dawnseeker8 wants to merge 4 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 <dawnseeker8@users.noreply.github.com>
Co-authored-by: Xiaoming Wang <dawnseeker8@users.noreply.github.com>
…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 <dawnseeker8@users.noreply.github.com>
Co-authored-by: Xiaoming Wang <dawnseeker8@users.noreply.github.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ 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 ea8cfb0. Configure here.
| console.error('Failed to fetch initial balances:', result.reason); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Initial balances skip when locked
Medium Severity
MultichainBalancesController.init() is a one-shot fetch that Wallet.init() runs before unlock, but #updateBalance returns immediately while the keyring is locked. There is no KeyringController:unlock listener, and #initialized then blocks a later init(), so existing non-EVM accounts never get that startup refresh.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit ea8cfb0. Configure here.
|
duplicate PR found with #11315 (may be due to cursor creation bug). |


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.Checklist
Note
High Risk
Breaking constructor/init contract across many asset controllers plus Wallet default registration risks messenger collisions, wrong init ordering, and subtle pre-init behavior if hosts skip
wallet.init()or duplicate controllers.Reviewed by Cursor Bugbot for commit ea8cfb0. Bugbot is set up for automated code reviews on this repo. Configure here.