@metamask-previews/assets-controller 20.3.0-preview-a553ac65f → 20.3.0-preview-40b7a961e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/AssetsController.d.ts +7 -4
- package/dist/AssetsController.d.ts.map +1 -1
- package/dist/AssetsController.js +230 -59
- package/dist/AssetsController.js.map +1 -1
- package/dist/data-sources/AbstractDataSource.d.ts +13 -0
- package/dist/data-sources/AbstractDataSource.d.ts.map +1 -1
- package/dist/data-sources/AbstractDataSource.js +15 -0
- package/dist/data-sources/AbstractDataSource.js.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.ts +8 -0
- package/dist/data-sources/AccountsApiDataSource.d.ts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.js +23 -1
- package/dist/data-sources/AccountsApiDataSource.js.map +1 -1
- package/dist/data-sources/RpcDataSource.d.ts +8 -0
- package/dist/data-sources/RpcDataSource.d.ts.map +1 -1
- package/dist/data-sources/RpcDataSource.js +9 -0
- package/dist/data-sources/RpcDataSource.js.map +1 -1
- package/dist/data-sources/SnapDataSource.d.ts +8 -0
- package/dist/data-sources/SnapDataSource.d.ts.map +1 -1
- package/dist/data-sources/SnapDataSource.js +9 -1
- package/dist/data-sources/SnapDataSource.js.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.d.ts +10 -0
- package/dist/data-sources/StakedBalanceDataSource.d.ts.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.js +5 -2
- package/dist/data-sources/StakedBalanceDataSource.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add `bootstrap()` to `AbstractDataSource` for setup that reads other controllers or makes requests ([#11351](https://github.com/MetaMask/core/pull/11351))
|
|
13
|
+
- The default implementation does nothing. `AssetsController` calls it on every balance data source the first time the UI opens.
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
- **BREAKING:** `AssetsController` no longer calls other controllers or makes requests in its constructor; it bootstraps the first time the UI opens ([#11351](https://github.com/MetaMask/core/pull/11351))
|
|
18
|
+
- The constructor only subscribes to events and registers action handlers, so it no longer depends on the order in which controllers are constructed.
|
|
19
|
+
- The first `ClientController:stateChange` with `isUiOpen: true` bootstraps the balance data sources, fetches the native asset map, calls `subscribeToBasicFunctionalityChange`, subscribes to `NetworkEnablementController:stateChange`, and runs the unlock cleanups if the keyring is already unlocked. Assets are then tracked as before, once the keyring is unlocked and the account tree is initialized.
|
|
20
|
+
- `subscribeToBasicFunctionalityChange` is called before the controller counts as bootstrapped, so a listener called synchronously is ignored rather than skipping the first forced fetch. If it throws, the error is reported through `captureException` and bootstrapping continues.
|
|
21
|
+
- Until then, network, transaction and `KeyringController:unlock` events and `handleBasicFunctionalityChange` are ignored.
|
|
22
|
+
- Enabled chains are no longer cached on the controller; they are read from `NetworkEnablementController:getState` each time they are needed, so they are correct before bootstrap too. They are empty if `NetworkEnablementController` is not registered.
|
|
23
|
+
- A controller destroyed during or after bootstrap does not start tracking assets.
|
|
24
|
+
- Clients must call `ClientController:setUiOpen(true)` when the UI opens, or assets are never loaded.
|
|
25
|
+
- **BREAKING:** `AccountsApiDataSource`, `RpcDataSource` and `SnapDataSource` no longer call other controllers or make requests in their constructors ([#11351](https://github.com/MetaMask/core/pull/11351))
|
|
26
|
+
- Call `bootstrap()` to fetch the Accounts API supported networks (and start refreshing them), read `NetworkController` state, or discover keyring snaps, respectively. `AssetsController` does this for you.
|
|
27
|
+
- `AccountsApiDataSource` ignores `RemoteFeatureFlagController:stateChange` until it has been bootstrapped.
|
|
28
|
+
- `AssetsController` treats a controller whose actions are not registered (such as `AccountTreeController`, `ClientController` or `KeyringController`) as not ready instead of throwing ([#11351](https://github.com/MetaMask/core/pull/11351))
|
|
29
|
+
|
|
10
30
|
## [20.3.0]
|
|
11
31
|
|
|
12
32
|
### Added
|
|
@@ -155,10 +155,13 @@ export type AssetsControllerOptions = {
|
|
|
155
155
|
* based on which chains they support. When active chains change, the controller
|
|
156
156
|
* dynamically adjusts subscriptions.
|
|
157
157
|
*
|
|
158
|
-
* 4. **Client + Keyring Lifecycle**:
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
158
|
+
* 4. **Client + Keyring Lifecycle**: The constructor never calls other
|
|
159
|
+
* controllers. The first time the UI opens (ClientController), the controller
|
|
160
|
+
* bootstraps: it reads enabled networks and bootstraps its data sources.
|
|
161
|
+
* After that it starts subscriptions only when both the UI is open and the
|
|
162
|
+
* wallet is unlocked (KeyringController), and stops when either the UI
|
|
163
|
+
* closes or the keyring locks. See client-controller README for the
|
|
164
|
+
* combined pattern.
|
|
162
165
|
*
|
|
163
166
|
* ## Architecture
|
|
164
167
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssetsController.d.ts","sourceRoot":"","sources":["../src/AssetsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8DAA8D,EAC9D,qCAAqC,EACrC,wCAAwC,EACxC,oDAAoD,EACpD,uCAAuC,EACxC,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,0CAA0C,EAAE,MAAM,+BAA+B,CAAC;AAChG,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EACV,8BAA8B,EAC9B,gCAAgC,EACjC,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,4DAA4D,EAAE,MAAM,sCAAsC,CAAC;AACzH,OAAO,KAAK,EAAE,aAAa,EAAgB,MAAM,4BAA4B,CAAC;AAC9E,OAAO,KAA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;IACH,SAAS,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CActC;IAED;;;;;;;OAOG;IACG,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CA6BvD;IA0BD;;;;OAIG;IACH,mBAAmB,CAAC,gBAAgB,EAAE,iBAAiB,GAAG,IAAI,CA8B7D;IAMD;;;;;;OAMG;IACH,oBAAoB,CAAC,QAAQ,EAAE,eAAe,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,CAkC3E;IAED;;OAEG;IACH,sBAAsB,IAAI,IAAI,CAS7B;IA23BD;;;;;;;OAOG;IACH,8BAA8B,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI,CAMtD;IA0qBD;;;;;;;;;OASG;IACG,kBAAkB,CACtB,QAAQ,EAAE,YAAY,EACtB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,IAAI,CAAC,CAYf;IAsMD,OAAO,IAAI,IAAI,CAqDd;CACF"}
|
package/dist/AssetsController.js
CHANGED
|
@@ -321,10 +321,13 @@ function effectiveAccountBalancesV6(previousBalances, updatedBalances, undeletab
|
|
|
321
321
|
* based on which chains they support. When active chains change, the controller
|
|
322
322
|
* dynamically adjusts subscriptions.
|
|
323
323
|
*
|
|
324
|
-
* 4. **Client + Keyring Lifecycle**:
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
324
|
+
* 4. **Client + Keyring Lifecycle**: The constructor never calls other
|
|
325
|
+
* controllers. The first time the UI opens (ClientController), the controller
|
|
326
|
+
* bootstraps: it reads enabled networks and bootstraps its data sources.
|
|
327
|
+
* After that it starts subscriptions only when both the UI is open and the
|
|
328
|
+
* wallet is unlocked (KeyringController), and stops when either the UI
|
|
329
|
+
* closes or the keyring locks. See client-controller README for the
|
|
330
|
+
* combined pattern.
|
|
328
331
|
*
|
|
329
332
|
* ## Architecture
|
|
330
333
|
*
|
|
@@ -426,8 +429,30 @@ let AssetsController = (() => {
|
|
|
426
429
|
* its forced `getAssets()` call) runs a second time for the same accounts.
|
|
427
430
|
*/
|
|
428
431
|
#startInFlight = false;
|
|
429
|
-
/**
|
|
430
|
-
|
|
432
|
+
/**
|
|
433
|
+
* Get the currently enabled chains from NetworkEnablementController.
|
|
434
|
+
*
|
|
435
|
+
* Read each time they are needed rather than cached, so there is a single
|
|
436
|
+
* source of truth. Empty if NetworkEnablementController is not registered.
|
|
437
|
+
*
|
|
438
|
+
* @returns Set of CAIP-2 chain IDs for all enabled networks.
|
|
439
|
+
*/
|
|
440
|
+
#getEnabledChains() {
|
|
441
|
+
const enabledNetworkMap = this.#callIfRegistered(() => this.messenger.call('NetworkEnablementController:getState')
|
|
442
|
+
.enabledNetworkMap, undefined);
|
|
443
|
+
return enabledNetworkMap
|
|
444
|
+
? this.#extractEnabledChains(enabledNetworkMap)
|
|
445
|
+
: new Set();
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Whether `#bootstrap()` has finished. Until then, event handlers only
|
|
449
|
+
* record state (such as enabled chains) and never fetch.
|
|
450
|
+
*/
|
|
451
|
+
#isBootstrapped = false;
|
|
452
|
+
/** Set by the first `#bootstrap()` call; later calls reuse it. */
|
|
453
|
+
#bootstrapPromise = null;
|
|
454
|
+
#isDestroyed = false;
|
|
455
|
+
#subscribeToBasicFunctionalityChange;
|
|
431
456
|
/**
|
|
432
457
|
* Get the currently selected accounts from AccountTreeController.
|
|
433
458
|
* This includes all accounts in the same group as the selected account
|
|
@@ -436,23 +461,44 @@ let AssetsController = (() => {
|
|
|
436
461
|
* @returns Array of InternalAccount objects from the selected account group.
|
|
437
462
|
*/
|
|
438
463
|
#getSelectedAccounts() {
|
|
439
|
-
const accounts = this.messenger.call('AccountTreeController:getAccountsFromSelectedAccountGroup');
|
|
464
|
+
const accounts = this.#callIfRegistered(() => this.messenger.call('AccountTreeController:getAccountsFromSelectedAccountGroup'), []);
|
|
440
465
|
if (accounts.length > 0) {
|
|
441
466
|
return accounts;
|
|
442
467
|
}
|
|
443
|
-
const selectedAccount = this.messenger.call('AccountsController:getSelectedAccount');
|
|
468
|
+
const selectedAccount = this.#callIfRegistered(() => this.messenger.call('AccountsController:getSelectedAccount'), undefined);
|
|
444
469
|
if (selectedAccount) {
|
|
445
470
|
return [selectedAccount];
|
|
446
471
|
}
|
|
447
472
|
return [];
|
|
448
473
|
}
|
|
474
|
+
/**
|
|
475
|
+
* Run a messenger call, returning `fallback` if it throws.
|
|
476
|
+
*
|
|
477
|
+
* The wallet may construct this controller before (or without) the
|
|
478
|
+
* controllers it reads from. A missing handler means that controller is not
|
|
479
|
+
* ready, which is treated the same as "not initialized" rather than crashing
|
|
480
|
+
* the caller.
|
|
481
|
+
*
|
|
482
|
+
* @param call - The messenger call to run.
|
|
483
|
+
* @param fallback - The value to return if the call throws.
|
|
484
|
+
* @returns The call's result, or `fallback`.
|
|
485
|
+
*/
|
|
486
|
+
#callIfRegistered(call, fallback) {
|
|
487
|
+
try {
|
|
488
|
+
return call();
|
|
489
|
+
}
|
|
490
|
+
catch (error) {
|
|
491
|
+
log('Messenger call failed; treating dependency as not ready', error);
|
|
492
|
+
return fallback;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
449
495
|
/**
|
|
450
496
|
* Whether `AccountTreeController.init()` has completed and the tree is ready.
|
|
451
497
|
*
|
|
452
498
|
* @returns True when the account tree has been initialized.
|
|
453
499
|
*/
|
|
454
500
|
#isAccountTreeInitialized() {
|
|
455
|
-
return this.messenger.call('AccountTreeController:isInitialized');
|
|
501
|
+
return this.#callIfRegistered(() => this.messenger.call('AccountTreeController:isInitialized'), false);
|
|
456
502
|
}
|
|
457
503
|
/**
|
|
458
504
|
* Whether the client (UI) is open, read from ClientController state.
|
|
@@ -460,7 +506,7 @@ let AssetsController = (() => {
|
|
|
460
506
|
* @returns True when the UI is open.
|
|
461
507
|
*/
|
|
462
508
|
#isUiOpen() {
|
|
463
|
-
return clientControllerSelectors.selectIsUiOpen(this.messenger.call('ClientController:getState'));
|
|
509
|
+
return this.#callIfRegistered(() => clientControllerSelectors.selectIsUiOpen(this.messenger.call('ClientController:getState')), false);
|
|
464
510
|
}
|
|
465
511
|
/**
|
|
466
512
|
* Whether the keyring is unlocked, read from KeyringController.
|
|
@@ -468,7 +514,7 @@ let AssetsController = (() => {
|
|
|
468
514
|
* @returns True when the wallet vault is unlocked.
|
|
469
515
|
*/
|
|
470
516
|
#isKeyringUnlocked() {
|
|
471
|
-
return this.messenger.call('KeyringController:isUnlocked');
|
|
517
|
+
return this.#callIfRegistered(() => this.messenger.call('KeyringController:isUnlocked'), false);
|
|
472
518
|
}
|
|
473
519
|
#accountActivityDataSource;
|
|
474
520
|
#accountsApiDataSource;
|
|
@@ -523,7 +569,9 @@ let AssetsController = (() => {
|
|
|
523
569
|
captureException,
|
|
524
570
|
}));
|
|
525
571
|
}
|
|
526
|
-
this.#
|
|
572
|
+
this.#seedNativeAssetsMap(queryApiClient);
|
|
573
|
+
this.#subscribeToBasicFunctionalityChange =
|
|
574
|
+
subscribeToBasicFunctionalityChange;
|
|
527
575
|
this.#onActiveChainsUpdated = (dataSourceName, chains, previousChains) => {
|
|
528
576
|
try {
|
|
529
577
|
this.#handleActiveChainsUpdate(dataSourceName, chains, previousChains);
|
|
@@ -608,24 +656,23 @@ let AssetsController = (() => {
|
|
|
608
656
|
log('Initializing AssetsController', {
|
|
609
657
|
defaultUpdateInterval: this.#defaultUpdateInterval,
|
|
610
658
|
});
|
|
611
|
-
|
|
659
|
+
// The constructor only subscribes to events and registers action
|
|
660
|
+
// handlers. It never calls other controllers, so it does not depend on
|
|
661
|
+
// the order in which the wallet constructs them. Everything that reads
|
|
662
|
+
// other controllers or makes requests happens in `#bootstrap()`, the first
|
|
663
|
+
// time the UI opens.
|
|
612
664
|
this.#subscribeToEvents();
|
|
613
665
|
this.#registerActionHandlers();
|
|
614
|
-
// Subscriptions start only when both UI is open and keyring unlocked -> #updateActive().
|
|
615
|
-
// Subscribe to basic-functionality changes after construction so a synchronous
|
|
616
|
-
// onChange during subscribe cannot run before data sources are initialized.
|
|
617
|
-
if (subscribeToBasicFunctionalityChange) {
|
|
618
|
-
const unsubscribe = subscribeToBasicFunctionalityChange((isBasic) => this.handleBasicFunctionalityChange(isBasic));
|
|
619
|
-
if (typeof unsubscribe === 'function') {
|
|
620
|
-
this.#unsubscribeBasicFunctionality = unsubscribe;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
666
|
}
|
|
624
667
|
// ============================================================================
|
|
625
668
|
// INITIALIZATION
|
|
626
669
|
// ============================================================================
|
|
627
|
-
|
|
628
|
-
|
|
670
|
+
/**
|
|
671
|
+
* Fetch the native asset map into the query cache, replacing the seed data
|
|
672
|
+
* written by {@link AssetsController.#seedNativeAssetsMap}.
|
|
673
|
+
*/
|
|
674
|
+
#fetchNativeAssetsMap() {
|
|
675
|
+
this.#queryApiClient.queryClient
|
|
629
676
|
.fetchQuery({
|
|
630
677
|
queryKey: NATIVE_ASSETS_QUERY_KEY,
|
|
631
678
|
queryFn: buildNativeAssetsFromApi,
|
|
@@ -637,6 +684,13 @@ let AssetsController = (() => {
|
|
|
637
684
|
// #isNativeAsset falls back to the seed data from buildNativeAssetsFromConstant.
|
|
638
685
|
log('Failed to populate native asset cache, falling back to seed data', error);
|
|
639
686
|
});
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Seed the native asset map in the query cache from constants.
|
|
690
|
+
*
|
|
691
|
+
* @param queryApiClient - The API client whose cache to seed.
|
|
692
|
+
*/
|
|
693
|
+
#seedNativeAssetsMap(queryApiClient) {
|
|
640
694
|
// Seed the cache synchronously so that synchronous consumers (e.g.
|
|
641
695
|
// #isNativeAsset, #resolveNativeAssetIds) have data available immediately.
|
|
642
696
|
// When fetchQuery resolves (currently instant, future: API), it overwrites
|
|
@@ -644,12 +698,89 @@ let AssetsController = (() => {
|
|
|
644
698
|
const initialNativeAssetMap = buildNativeAssetsFromConstant();
|
|
645
699
|
queryApiClient.setCachedData(NATIVE_ASSETS_QUERY_KEY, initialNativeAssetMap);
|
|
646
700
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
701
|
+
/**
|
|
702
|
+
* Finish the work the constructor defers: read enabled networks, bootstrap
|
|
703
|
+
* the data sources and subscribe to basic-functionality changes, then start
|
|
704
|
+
* tracking assets if the keyring is unlocked and the account tree is ready.
|
|
705
|
+
*
|
|
706
|
+
* Runs the first time the UI opens (`ClientController:stateChange`), by
|
|
707
|
+
* which time the wallet has constructed every controller. Only the first
|
|
708
|
+
* call does the work; later calls return the same promise.
|
|
709
|
+
*
|
|
710
|
+
* @returns A promise that resolves once bootstrapping has finished.
|
|
711
|
+
*/
|
|
712
|
+
async #bootstrap() {
|
|
713
|
+
this.#bootstrapPromise ??= this.#runBootstrap();
|
|
714
|
+
return this.#bootstrapPromise;
|
|
715
|
+
}
|
|
716
|
+
async #runBootstrap() {
|
|
717
|
+
log('Bootstrapping AssetsController');
|
|
718
|
+
this.#fetchNativeAssetsMap();
|
|
719
|
+
// Data sources report their active chains through `#onActiveChainsUpdated`
|
|
720
|
+
// while bootstrapping. Those reports are ignored until `#isBootstrapped`
|
|
721
|
+
// is set; `#updateActive()` below subscribes with the final chains.
|
|
722
|
+
await Promise.allSettled([...this.#allBalanceDataSources, this.#stakedBalanceDataSource].map(async (source) => source.bootstrap()));
|
|
723
|
+
if (this.#isDestroyed) {
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
// Subscribe before marking the controller bootstrapped, so a listener the
|
|
727
|
+
// client calls synchronously is ignored by `handleBasicFunctionalityChange`
|
|
728
|
+
// instead of subscribing early and making `#start()` skip the forced
|
|
729
|
+
// startup fetch. Nothing is lost: `#updateActive()` below reads the
|
|
730
|
+
// current value through `isBasicFunctionality`.
|
|
731
|
+
this.#subscribeToBasicFunctionality();
|
|
732
|
+
// Subscribed here rather than in the constructor: by now the wallet has
|
|
733
|
+
// constructed NetworkEnablementController, so the selector starts from its
|
|
734
|
+
// current state and each change is diffed against the previous one.
|
|
735
|
+
this.messenger.subscribe('NetworkEnablementController:stateChange', (enabledNetworkMap, previousEnabledNetworkMap) => {
|
|
736
|
+
this.#handleEnabledNetworksChanged(enabledNetworkMap, previousEnabledNetworkMap).catch(console.error);
|
|
737
|
+
}, ({ enabledNetworkMap }) => enabledNetworkMap);
|
|
738
|
+
this.#isBootstrapped = true;
|
|
739
|
+
// `KeyringController:unlock` handlers skip their work before bootstrap,
|
|
740
|
+
// so run it now if the wallet was unlocked before the UI opened.
|
|
741
|
+
if (this.#isKeyringUnlocked()) {
|
|
742
|
+
await this.#runUnlockCleanups();
|
|
743
|
+
}
|
|
744
|
+
// `destroy()` may have run while the cleanups were awaited.
|
|
745
|
+
if (this.#isDestroyed) {
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
this.#updateActive();
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Call the client's `subscribeToBasicFunctionalityChange`, keeping the
|
|
752
|
+
* unsubscribe function it returns. If it throws, the error is logged and
|
|
753
|
+
* reported, and bootstrapping carries on without the subscription, so
|
|
754
|
+
* asset tracking still starts.
|
|
755
|
+
*/
|
|
756
|
+
#subscribeToBasicFunctionality() {
|
|
757
|
+
if (!this.#subscribeToBasicFunctionalityChange) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
try {
|
|
761
|
+
const unsubscribe = this.#subscribeToBasicFunctionalityChange((isBasic) => this.handleBasicFunctionalityChange(isBasic));
|
|
762
|
+
if (typeof unsubscribe === 'function') {
|
|
763
|
+
this.#unsubscribeBasicFunctionality = unsubscribe;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
catch (error) {
|
|
767
|
+
log('Failed to subscribe to basic functionality changes', error);
|
|
768
|
+
this.#captureException?.(error instanceof Error ? error : new Error(String(error)));
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Remove balances outside the current scope and spam tokens. Runs on
|
|
773
|
+
* unlock, and at bootstrap when the wallet was already unlocked.
|
|
774
|
+
*/
|
|
775
|
+
async #runUnlockCleanups() {
|
|
776
|
+
try {
|
|
777
|
+
this.#runOutOfScopeBalanceCleanup();
|
|
778
|
+
}
|
|
779
|
+
catch {
|
|
780
|
+
/* Do nothing */
|
|
781
|
+
}
|
|
782
|
+
await this.#runSpamCleanup().catch(() => {
|
|
783
|
+
/* Do nothing */
|
|
653
784
|
});
|
|
654
785
|
}
|
|
655
786
|
/**
|
|
@@ -707,10 +838,8 @@ let AssetsController = (() => {
|
|
|
707
838
|
this.messenger.subscribe('AccountTreeController:uninitialized', () => {
|
|
708
839
|
this.#updateActive();
|
|
709
840
|
});
|
|
710
|
-
//
|
|
711
|
-
|
|
712
|
-
this.#handleEnabledNetworksChanged(enabledNetworkMap).catch(console.error);
|
|
713
|
-
});
|
|
841
|
+
// `NetworkEnablementController:stateChange` is subscribed to in
|
|
842
|
+
// `#bootstrap()`; enabled chains are read on demand until then.
|
|
714
843
|
// When a network is added or removed from NetworkController, refresh
|
|
715
844
|
// assets across every data source so balances, prices, and metadata
|
|
716
845
|
// stay consistent. On add we also seed default tracked assets (e.g.
|
|
@@ -718,34 +847,50 @@ let AssetsController = (() => {
|
|
|
718
847
|
// entries appear immediately without waiting for it to also be
|
|
719
848
|
// enabled in NetworkEnablementController.
|
|
720
849
|
this.messenger.subscribe('NetworkController:networkAdded', (networkConfiguration) => {
|
|
850
|
+
// Seeding default tracked assets only updates state, so it runs even
|
|
851
|
+
// before bootstrap; the refresh fetches, so it waits for bootstrap.
|
|
721
852
|
this.#handleNetworkAdded(networkConfiguration.chainId);
|
|
853
|
+
if (!this.#isBootstrapped) {
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
722
856
|
this.#refreshAssetsAfterNetworkAdded(networkConfiguration.chainId).catch((error) => {
|
|
723
857
|
log('Failed to refresh assets after network added', { error });
|
|
724
858
|
});
|
|
725
859
|
});
|
|
726
860
|
this.messenger.subscribe('NetworkController:networkRemoved', () => {
|
|
861
|
+
if (!this.#isBootstrapped) {
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
727
864
|
this.#refreshAssetsAfterNetworkChange();
|
|
728
865
|
});
|
|
729
866
|
// Selected EVM network switch (network picker). Enablement changes are
|
|
730
867
|
// handled separately via NetworkEnablementController:stateChange.
|
|
731
868
|
this.messenger.subscribe('NetworkController:networkDidChange', (networkState) => {
|
|
869
|
+
if (!this.#isBootstrapped) {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
732
872
|
this.#handleNetworkDidChange(networkState).catch(console.error);
|
|
733
873
|
});
|
|
734
|
-
// Client + Keyring lifecycle:
|
|
735
|
-
|
|
874
|
+
// Client + Keyring lifecycle: the first time the UI opens, bootstrap.
|
|
875
|
+
// After that, only run when UI is open AND keyring is unlocked.
|
|
876
|
+
this.messenger.subscribe('ClientController:stateChange', (isUiOpen) => {
|
|
877
|
+
if (!this.#isBootstrapped) {
|
|
878
|
+
if (isUiOpen) {
|
|
879
|
+
this.#bootstrap().catch((error) => {
|
|
880
|
+
log('Failed to bootstrap AssetsController', error);
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
736
885
|
this.#updateActive();
|
|
737
886
|
}, clientControllerSelectors.selectIsUiOpen);
|
|
738
887
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
739
888
|
this.messenger.subscribe('KeyringController:unlock', async () => {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
catch {
|
|
744
|
-
/* Do nothing */
|
|
889
|
+
// Before bootstrap, `#bootstrap()` runs the cleanups itself.
|
|
890
|
+
if (!this.#isBootstrapped) {
|
|
891
|
+
return;
|
|
745
892
|
}
|
|
746
|
-
await this.#
|
|
747
|
-
/* Do nothing */
|
|
748
|
-
});
|
|
893
|
+
await this.#runUnlockCleanups();
|
|
749
894
|
this.#updateActive();
|
|
750
895
|
});
|
|
751
896
|
this.messenger.subscribe('KeyringController:lock', () => {
|
|
@@ -755,6 +900,9 @@ let AssetsController = (() => {
|
|
|
755
900
|
// Ensures balances for the account making the transaction are updated
|
|
756
901
|
// (e.g. for gas estimations), except on AccountActivity-active chains.
|
|
757
902
|
this.messenger.subscribe('TransactionController:unapprovedTransactionAdded', (transactionMeta) => {
|
|
903
|
+
if (!this.#isBootstrapped) {
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
758
906
|
this.#refreshAssetsForEVMTransaction(transactionMeta);
|
|
759
907
|
});
|
|
760
908
|
// Post-tx refresh via the full fetch pipeline (Accounts API + RPC fallback).
|
|
@@ -762,6 +910,9 @@ let AssetsController = (() => {
|
|
|
762
910
|
// RpcDataSource also listens for transactionConfirmed, but only refreshes
|
|
763
911
|
// chains it owns via an active subscription.
|
|
764
912
|
this.messenger.subscribe('TransactionController:transactionConfirmed', (transactionMeta) => {
|
|
913
|
+
if (!this.#isBootstrapped) {
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
765
916
|
this.#refreshAssetsForEVMTransaction(transactionMeta);
|
|
766
917
|
});
|
|
767
918
|
// Non-EVM equivalent of the above: Snap keyrings report transaction
|
|
@@ -769,6 +920,9 @@ let AssetsController = (() => {
|
|
|
769
920
|
// not covered by AccountActivity (WebSocket down / unsupported), refresh
|
|
770
921
|
// via the Accounts API with the server cache bypassed.
|
|
771
922
|
this.messenger.subscribe('MultichainTransactionsController:transactionConfirmed', (transaction) => {
|
|
923
|
+
if (!this.#isBootstrapped) {
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
772
926
|
this.#refreshAssetsForNonEvmTransaction(transaction);
|
|
773
927
|
});
|
|
774
928
|
}
|
|
@@ -896,6 +1050,11 @@ let AssetsController = (() => {
|
|
|
896
1050
|
* the keyring is unlocked, and the account tree has finished `init()`.
|
|
897
1051
|
*/
|
|
898
1052
|
#updateActive() {
|
|
1053
|
+
// Event subscriptions outlive `destroy()`, so lifecycle events can still
|
|
1054
|
+
// arrive; never start tracking on a destroyed controller.
|
|
1055
|
+
if (!this.#isBootstrapped || this.#isDestroyed) {
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
899
1058
|
const shouldRun = this.#isUiOpen() &&
|
|
900
1059
|
this.#isKeyringUnlocked() &&
|
|
901
1060
|
this.#isAccountTreeInitialized();
|
|
@@ -915,7 +1074,7 @@ let AssetsController = (() => {
|
|
|
915
1074
|
const releaseLock = await this.#accountRefreshMutex.acquire();
|
|
916
1075
|
try {
|
|
917
1076
|
await this.getAssets(accounts, {
|
|
918
|
-
chainIds: [...this.#
|
|
1077
|
+
chainIds: [...this.#getEnabledChains()],
|
|
919
1078
|
forceUpdate: true,
|
|
920
1079
|
});
|
|
921
1080
|
// Seed before subscribe so the price poll / update fetch sees natives
|
|
@@ -955,7 +1114,10 @@ let AssetsController = (() => {
|
|
|
955
1114
|
* @param previousChains - Previous chains; used to compute added/removed.
|
|
956
1115
|
*/
|
|
957
1116
|
#handleActiveChainsUpdate(dataSourceId, activeChains, previousChains) {
|
|
958
|
-
if (!this.#
|
|
1117
|
+
if (!this.#isBootstrapped ||
|
|
1118
|
+
!this.#isUiOpen() ||
|
|
1119
|
+
!this.#isKeyringUnlocked() ||
|
|
1120
|
+
!this.#isEnabled()) {
|
|
959
1121
|
return;
|
|
960
1122
|
}
|
|
961
1123
|
log('Data source active chains changed', {
|
|
@@ -1009,7 +1171,7 @@ let AssetsController = (() => {
|
|
|
1009
1171
|
// PUBLIC API: QUERY METHODS
|
|
1010
1172
|
// ============================================================================
|
|
1011
1173
|
async getAssets(accounts, options) {
|
|
1012
|
-
const chainIds = options?.chainIds ?? [...this.#
|
|
1174
|
+
const chainIds = options?.chainIds ?? [...this.#getEnabledChains()];
|
|
1013
1175
|
const assetTypes = options?.assetTypes ?? ['fungible'];
|
|
1014
1176
|
const dataTypes = options?.dataTypes ?? ['balance', 'metadata', 'price'];
|
|
1015
1177
|
if (accounts.length === 0 || chainIds.length === 0) {
|
|
@@ -1894,7 +2056,7 @@ let AssetsController = (() => {
|
|
|
1894
2056
|
* @returns Array of native asset IDs, one per enabled chain that has a registered identifier.
|
|
1895
2057
|
*/
|
|
1896
2058
|
#getNativeAssetIdsForEnabledChains() {
|
|
1897
|
-
return this.#resolveNativeAssetIds(this.#
|
|
2059
|
+
return this.#resolveNativeAssetIds(this.#getEnabledChains());
|
|
1898
2060
|
}
|
|
1899
2061
|
/**
|
|
1900
2062
|
* Returns native asset IDs for the chains that this account supports
|
|
@@ -1990,7 +2152,7 @@ let AssetsController = (() => {
|
|
|
1990
2152
|
// why mUSD appears on mainnet/Linea immediately (those are normally
|
|
1991
2153
|
// enabled at startup) but only appears on Monad once the user
|
|
1992
2154
|
// turns the Monad network on.
|
|
1993
|
-
const candidateChains = chainsToSeed ?? [...this.#
|
|
2155
|
+
const candidateChains = chainsToSeed ?? [...this.#getEnabledChains()];
|
|
1994
2156
|
const targetChains = candidateChains.filter((chainId) => CHAINS_WITH_DEFAULT_TRACKED_ASSETS.has(chainId));
|
|
1995
2157
|
if (targetChains.length === 0) {
|
|
1996
2158
|
return;
|
|
@@ -2355,7 +2517,7 @@ let AssetsController = (() => {
|
|
|
2355
2517
|
*/
|
|
2356
2518
|
#start() {
|
|
2357
2519
|
const accounts = this.#getSelectedAccounts();
|
|
2358
|
-
const chainIds = [...this.#
|
|
2520
|
+
const chainIds = [...this.#getEnabledChains()];
|
|
2359
2521
|
if (accounts.length === 0 || chainIds.length === 0) {
|
|
2360
2522
|
return;
|
|
2361
2523
|
}
|
|
@@ -2419,6 +2581,9 @@ let AssetsController = (() => {
|
|
|
2419
2581
|
* @param _isBasic - The new value (for call-site clarity; the getter is the source of truth).
|
|
2420
2582
|
*/
|
|
2421
2583
|
handleBasicFunctionalityChange(_isBasic) {
|
|
2584
|
+
if (!this.#isBootstrapped) {
|
|
2585
|
+
return;
|
|
2586
|
+
}
|
|
2422
2587
|
this.#stop();
|
|
2423
2588
|
this.#subscribeAssets();
|
|
2424
2589
|
}
|
|
@@ -2431,7 +2596,7 @@ let AssetsController = (() => {
|
|
|
2431
2596
|
*/
|
|
2432
2597
|
#subscribeAssets(options) {
|
|
2433
2598
|
const accounts = this.#getSelectedAccounts();
|
|
2434
|
-
const enabledChains = [...this.#
|
|
2599
|
+
const enabledChains = [...this.#getEnabledChains()];
|
|
2435
2600
|
if (accounts.length === 0 || enabledChains.length === 0) {
|
|
2436
2601
|
return;
|
|
2437
2602
|
}
|
|
@@ -2728,7 +2893,7 @@ let AssetsController = (() => {
|
|
|
2728
2893
|
// Wildcard scope (e.g., "eip155:0" means all enabled chains in that namespace)
|
|
2729
2894
|
if (reference === '0') {
|
|
2730
2895
|
// Add enabled chains
|
|
2731
|
-
for (const chain of this.#
|
|
2896
|
+
for (const chain of this.#getEnabledChains()) {
|
|
2732
2897
|
if (isCaipChainId(chain)) {
|
|
2733
2898
|
const chainParsed = parseCaipChainId(chain);
|
|
2734
2899
|
if (chainParsed.namespace === namespace) {
|
|
@@ -2786,7 +2951,7 @@ let AssetsController = (() => {
|
|
|
2786
2951
|
this.#ensureDefaultTrackedAssetsSeeded();
|
|
2787
2952
|
if (accounts.length > 0) {
|
|
2788
2953
|
await this.getAssets(accounts, {
|
|
2789
|
-
chainIds: [...this.#
|
|
2954
|
+
chainIds: [...this.#getEnabledChains()],
|
|
2790
2955
|
forceUpdate: true,
|
|
2791
2956
|
});
|
|
2792
2957
|
}
|
|
@@ -2796,12 +2961,17 @@ let AssetsController = (() => {
|
|
|
2796
2961
|
releaseLock();
|
|
2797
2962
|
}
|
|
2798
2963
|
}
|
|
2799
|
-
async #handleEnabledNetworksChanged(enabledNetworkMap) {
|
|
2800
|
-
|
|
2801
|
-
|
|
2964
|
+
async #handleEnabledNetworksChanged(enabledNetworkMap, previousEnabledNetworkMap) {
|
|
2965
|
+
// Without a previous map (no initial state was available when
|
|
2966
|
+
// subscribing), treat every enabled chain as newly enabled: at worst one
|
|
2967
|
+
// extra fetch, never a missed one.
|
|
2968
|
+
const previousChains = previousEnabledNetworkMap
|
|
2969
|
+
? this.#extractEnabledChains(previousEnabledNetworkMap)
|
|
2970
|
+
: new Set();
|
|
2971
|
+
const enabledChains = this.#extractEnabledChains(enabledNetworkMap);
|
|
2802
2972
|
// Find newly enabled chains (in new set but not in previous)
|
|
2803
2973
|
const addedChains = [];
|
|
2804
|
-
for (const chain of
|
|
2974
|
+
for (const chain of enabledChains) {
|
|
2805
2975
|
if (!previousChains.has(chain)) {
|
|
2806
2976
|
addedChains.push(chain);
|
|
2807
2977
|
}
|
|
@@ -2809,13 +2979,13 @@ let AssetsController = (() => {
|
|
|
2809
2979
|
// Find disabled chains to clean up (in previous but not in new)
|
|
2810
2980
|
const removedChains = [];
|
|
2811
2981
|
for (const chain of previousChains) {
|
|
2812
|
-
if (!
|
|
2982
|
+
if (!enabledChains.has(chain)) {
|
|
2813
2983
|
removedChains.push(chain);
|
|
2814
2984
|
}
|
|
2815
2985
|
}
|
|
2816
2986
|
log('Enabled networks changed', {
|
|
2817
2987
|
previousCount: previousChains.size,
|
|
2818
|
-
newCount:
|
|
2988
|
+
newCount: enabledChains.size,
|
|
2819
2989
|
addedChains,
|
|
2820
2990
|
removedChains,
|
|
2821
2991
|
});
|
|
@@ -3136,6 +3306,7 @@ let AssetsController = (() => {
|
|
|
3136
3306
|
// CLEANUP
|
|
3137
3307
|
// ============================================================================
|
|
3138
3308
|
destroy() {
|
|
3309
|
+
this.#isDestroyed = true;
|
|
3139
3310
|
log('Destroying AssetsController', {
|
|
3140
3311
|
dataSourceCount: this.#allBalanceDataSources.length,
|
|
3141
3312
|
subscriptionCount: this.#activeSubscriptions.size,
|