@metamask-previews/assets-controller 2.0.2-preview-702bd3940 → 2.0.2-preview-09603a559
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 +0 -3
- package/dist/AssetsController.cjs +15 -74
- package/dist/AssetsController.cjs.map +1 -1
- package/dist/AssetsController.d.cts +1 -3
- package/dist/AssetsController.d.cts.map +1 -1
- package/dist/AssetsController.d.mts +1 -3
- package/dist/AssetsController.d.mts.map +1 -1
- package/dist/AssetsController.mjs +15 -74
- package/dist/AssetsController.mjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.cjs +3 -15
- package/dist/data-sources/BackendWebsocketDataSource.cjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.cts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.mts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.mjs +3 -12
- package/dist/data-sources/BackendWebsocketDataSource.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -24,9 +24,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
24
24
|
|
|
25
25
|
### Fixed
|
|
26
26
|
|
|
27
|
-
- Default `assetsBalance` to `0` for native tokens of each account's supported chains using `NetworkEnablementController.nativeAssetIdentifiers`, so native entries are always present in state even before data sources respond ([#8036](https://github.com/MetaMask/core/pull/8036))
|
|
28
|
-
- Auto-select `'merge'` update mode in `getAssets` when `chainIds` is a subset of enabled chains, preventing partial-chain fetches (e.g. after a swap, adding a custom asset, or data-source chain changes) from wiping balances of other chains ([#8036](https://github.com/MetaMask/core/pull/8036))
|
|
29
|
-
- Convert WebSocket balance updates in `BackendWebsocketDataSource` from raw smallest-units to human-readable amounts using asset decimals (same behavior as RPC/Accounts API), so `assetsBalance` remains consistent across data sources ([#8032](https://github.com/MetaMask/core/pull/8032))
|
|
30
27
|
- Include all assets from balance and each account's custom assets from state in `detectedAssets`, so prices and metadata are fetched for existing assets and custom tokens (previously only assets without metadata were included, so existing assets did not get prices) ([#8021](https://github.com/MetaMask/core/pull/8021))
|
|
31
28
|
- Request `includeAggregators: true` when fetching token metadata from the v3 assets API so aggregator data is returned and stored in `assetsInfo` ([#8021](https://github.com/MetaMask/core/pull/8021))
|
|
32
29
|
|
|
@@ -13,7 +13,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _AssetsController_instances, _AssetsController_isEnabled, _AssetsController_isBasicFunctionality, _AssetsController_defaultUpdateInterval, _AssetsController_trackMetaMetricsEvent, _AssetsController_firstInitFetchReported, _AssetsController_uiOpen, _AssetsController_keyringUnlocked, _AssetsController_controllerMutex, _AssetsController_activeSubscriptions, _AssetsController_enabledChains, _AssetsController_selectedAccounts_get, _AssetsController_backendWebsocketDataSource, _AssetsController_accountsApiDataSource, _AssetsController_snapDataSource, _AssetsController_rpcDataSource, _AssetsController_stakedBalanceDataSource, _AssetsController_allBalanceDataSources_get, _AssetsController_priceDataSource, _AssetsController_detectionMiddleware, _AssetsController_tokenDataSource, _AssetsController_unsubscribeBasicFunctionality, _AssetsController_initializeState, _AssetsController_extractEnabledChains, _AssetsController_normalizeChainReference, _AssetsController_subscribeToEvents, _AssetsController_updateActive, _AssetsController_registerActionHandlers, _AssetsController_executeMiddlewares,
|
|
16
|
+
var _AssetsController_instances, _AssetsController_isEnabled, _AssetsController_isBasicFunctionality, _AssetsController_defaultUpdateInterval, _AssetsController_trackMetaMetricsEvent, _AssetsController_firstInitFetchReported, _AssetsController_uiOpen, _AssetsController_keyringUnlocked, _AssetsController_controllerMutex, _AssetsController_activeSubscriptions, _AssetsController_enabledChains, _AssetsController_selectedAccounts_get, _AssetsController_backendWebsocketDataSource, _AssetsController_accountsApiDataSource, _AssetsController_snapDataSource, _AssetsController_rpcDataSource, _AssetsController_stakedBalanceDataSource, _AssetsController_allBalanceDataSources_get, _AssetsController_priceDataSource, _AssetsController_detectionMiddleware, _AssetsController_tokenDataSource, _AssetsController_unsubscribeBasicFunctionality, _AssetsController_initializeState, _AssetsController_extractEnabledChains, _AssetsController_normalizeChainReference, _AssetsController_subscribeToEvents, _AssetsController_updateActive, _AssetsController_registerActionHandlers, _AssetsController_executeMiddlewares, _AssetsController_updateState, _AssetsController_getAssetsFromState, _AssetsController_tokenStandardToAssetType, _AssetsController_start, _AssetsController_stop, _AssetsController_subscribeAssets, _AssetsController_subscribeAssetsBalance, _AssetsController_subscribeStakedBalance, _AssetsController_buildChainToAccountsMap, _AssetsController_subscribeDataSource, _AssetsController_unsubscribeDataSource, _AssetsController_buildDataRequest, _AssetsController_getEnabledChainsForAccount, _AssetsController_handleAccountGroupChanged, _AssetsController_handleEnabledNetworksChanged;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.AssetsController = exports.getDefaultAssetsControllerState = void 0;
|
|
19
19
|
const base_controller_1 = require("@metamask/base-controller");
|
|
@@ -343,7 +343,6 @@ class AssetsController extends base_controller_1.BaseController {
|
|
|
343
343
|
this.getAssets(__classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get), {
|
|
344
344
|
chainIds: addedEnabledChains,
|
|
345
345
|
forceUpdate: true,
|
|
346
|
-
updateMode: 'merge',
|
|
347
346
|
}).catch((error) => {
|
|
348
347
|
log('Failed to fetch balance for added chains', { error });
|
|
349
348
|
});
|
|
@@ -395,12 +394,7 @@ class AssetsController extends base_controller_1.BaseController {
|
|
|
395
394
|
__classPrivateFieldGet(this, _AssetsController_detectionMiddleware, "f"),
|
|
396
395
|
];
|
|
397
396
|
const { response, durationByDataSource } = await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_executeMiddlewares).call(this, sources, request);
|
|
398
|
-
|
|
399
|
-
// balances from chains that weren't included in this fetch.
|
|
400
|
-
const isPartialChainFetch = options?.chainIds !== undefined &&
|
|
401
|
-
options.chainIds.length < __classPrivateFieldGet(this, _AssetsController_enabledChains, "f").size;
|
|
402
|
-
const updateMode = options?.updateMode ?? (isPartialChainFetch ? 'merge' : 'full');
|
|
403
|
-
await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, { ...response, updateMode });
|
|
397
|
+
await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, { ...response, updateMode: 'full' });
|
|
404
398
|
if (__classPrivateFieldGet(this, _AssetsController_trackMetaMetricsEvent, "f") && !__classPrivateFieldGet(this, _AssetsController_firstInitFetchReported, "f")) {
|
|
405
399
|
__classPrivateFieldSet(this, _AssetsController_firstInitFetchReported, true, "f");
|
|
406
400
|
const durationMs = Date.now() - startTime;
|
|
@@ -514,14 +508,13 @@ class AssetsController extends base_controller_1.BaseController {
|
|
|
514
508
|
assetMetadata;
|
|
515
509
|
}
|
|
516
510
|
});
|
|
517
|
-
// Fetch data for the newly added custom asset
|
|
511
|
+
// Fetch data for the newly added custom asset
|
|
518
512
|
const account = __classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get).find((a) => a.id === accountId);
|
|
519
513
|
if (account) {
|
|
520
514
|
const chainId = extractChainId(normalizedAssetId);
|
|
521
515
|
await this.getAssets([account], {
|
|
522
516
|
chainIds: [chainId],
|
|
523
517
|
forceUpdate: true,
|
|
524
|
-
updateMode: 'merge',
|
|
525
518
|
});
|
|
526
519
|
}
|
|
527
520
|
}
|
|
@@ -868,44 +861,11 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
|
|
|
868
861
|
}
|
|
869
862
|
}
|
|
870
863
|
return { response: result.response, durationByDataSource };
|
|
871
|
-
},
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
if (nativeId) {
|
|
877
|
-
ids.push(nativeId);
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
return ids;
|
|
881
|
-
}, _AssetsController_getNativeAssetIdsForEnabledChains = function _AssetsController_getNativeAssetIdsForEnabledChains() {
|
|
882
|
-
return __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_resolveNativeAssetIds).call(this, __classPrivateFieldGet(this, _AssetsController_enabledChains, "f"));
|
|
883
|
-
}, _AssetsController_getNativeAssetIdsForAccount = function _AssetsController_getNativeAssetIdsForAccount(account) {
|
|
884
|
-
return __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_resolveNativeAssetIds).call(this, __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getEnabledChainsForAccount).call(this, account));
|
|
885
|
-
}, _AssetsController_ensureNativeBalancesDefaultZero = function _AssetsController_ensureNativeBalancesDefaultZero() {
|
|
886
|
-
const accounts = __classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get);
|
|
887
|
-
if (accounts.length === 0) {
|
|
888
|
-
return;
|
|
889
|
-
}
|
|
890
|
-
this.update((state) => {
|
|
891
|
-
const balances = state.assetsBalance;
|
|
892
|
-
for (const account of accounts) {
|
|
893
|
-
const accountId = account.id;
|
|
894
|
-
const nativeAssetIds = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getNativeAssetIdsForAccount).call(this, account);
|
|
895
|
-
if (nativeAssetIds.length === 0) {
|
|
896
|
-
continue;
|
|
897
|
-
}
|
|
898
|
-
if (!balances[accountId]) {
|
|
899
|
-
balances[accountId] = {};
|
|
900
|
-
}
|
|
901
|
-
for (const nativeAssetId of nativeAssetIds) {
|
|
902
|
-
if (!(nativeAssetId in balances[accountId])) {
|
|
903
|
-
balances[accountId][nativeAssetId] = { amount: '0' };
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
});
|
|
908
|
-
}, _AssetsController_updateState = async function _AssetsController_updateState(response) {
|
|
864
|
+
}, _AssetsController_updateState =
|
|
865
|
+
// ============================================================================
|
|
866
|
+
// STATE MANAGEMENT
|
|
867
|
+
// ============================================================================
|
|
868
|
+
async function _AssetsController_updateState(response) {
|
|
909
869
|
const normalizedResponse = normalizeResponse(response);
|
|
910
870
|
const mode = normalizedResponse.updateMode ?? 'merge';
|
|
911
871
|
const releaseLock = await __classPrivateFieldGet(this, _AssetsController_controllerMutex, "f").acquire();
|
|
@@ -934,12 +894,9 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
|
|
|
934
894
|
for (const [accountId, accountBalances] of Object.entries(normalizedResponse.assetsBalance)) {
|
|
935
895
|
const previousBalances = previousState.assetsBalance[accountId] ?? {};
|
|
936
896
|
const customAssetIds = state.customAssets[accountId] ?? [];
|
|
937
|
-
// Full: response is authoritative; preserve custom assets not in response.
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
const effective = mode === 'merge'
|
|
941
|
-
? { ...previousBalances, ...accountBalances }
|
|
942
|
-
: (() => {
|
|
897
|
+
// Full: response is authoritative; preserve custom assets not in response. Merge: response overlays previous.
|
|
898
|
+
const effective = mode === 'full'
|
|
899
|
+
? (() => {
|
|
943
900
|
const next = {
|
|
944
901
|
...accountBalances,
|
|
945
902
|
};
|
|
@@ -951,25 +908,13 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
|
|
|
951
908
|
}
|
|
952
909
|
}
|
|
953
910
|
return next;
|
|
954
|
-
})()
|
|
955
|
-
|
|
956
|
-
const account = __classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get).find((a) => a.id === accountId);
|
|
957
|
-
const nativeAssetIdsForAccount = account
|
|
958
|
-
? __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getNativeAssetIdsForAccount).call(this, account)
|
|
959
|
-
: __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getNativeAssetIdsForEnabledChains).call(this);
|
|
960
|
-
for (const nativeAssetId of nativeAssetIdsForAccount) {
|
|
961
|
-
if (!(nativeAssetId in effective)) {
|
|
962
|
-
effective[nativeAssetId] = { amount: '0' };
|
|
963
|
-
}
|
|
964
|
-
}
|
|
911
|
+
})()
|
|
912
|
+
: { ...previousBalances, ...accountBalances };
|
|
965
913
|
for (const [assetId, balance] of Object.entries(effective)) {
|
|
966
914
|
const previousBalance = previousBalances[assetId];
|
|
967
915
|
const newAmount = balance.amount;
|
|
968
916
|
const oldAmount = previousBalance?.amount;
|
|
969
|
-
|
|
970
|
-
newAmount === '0' &&
|
|
971
|
-
nativeAssetIdsForAccount.includes(assetId);
|
|
972
|
-
if (oldAmount !== newAmount && !isNewDefaultNativeZero) {
|
|
917
|
+
if (oldAmount !== newAmount) {
|
|
973
918
|
changedBalances.push({
|
|
974
919
|
accountId,
|
|
975
920
|
assetId,
|
|
@@ -1122,7 +1067,6 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
|
|
|
1122
1067
|
enabledChainCount: __classPrivateFieldGet(this, _AssetsController_enabledChains, "f").size,
|
|
1123
1068
|
});
|
|
1124
1069
|
__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_subscribeAssets).call(this);
|
|
1125
|
-
__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_ensureNativeBalancesDefaultZero).call(this);
|
|
1126
1070
|
this.getAssets(__classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get), {
|
|
1127
1071
|
chainIds: [...__classPrivateFieldGet(this, _AssetsController_enabledChains, "f")],
|
|
1128
1072
|
forceUpdate: true,
|
|
@@ -1330,7 +1274,6 @@ async function _AssetsController_handleAccountGroupChanged() {
|
|
|
1330
1274
|
forceUpdate: true,
|
|
1331
1275
|
});
|
|
1332
1276
|
}
|
|
1333
|
-
__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_ensureNativeBalancesDefaultZero).call(this);
|
|
1334
1277
|
}, _AssetsController_handleEnabledNetworksChanged = async function _AssetsController_handleEnabledNetworksChanged(enabledNetworkMap) {
|
|
1335
1278
|
const previousChains = __classPrivateFieldGet(this, _AssetsController_enabledChains, "f");
|
|
1336
1279
|
__classPrivateFieldSet(this, _AssetsController_enabledChains, __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_extractEnabledChains).call(this, enabledNetworkMap), "f");
|
|
@@ -1359,14 +1302,12 @@ async function _AssetsController_handleAccountGroupChanged() {
|
|
|
1359
1302
|
// The data will simply not be updated until the network is re-enabled.
|
|
1360
1303
|
// Refresh subscriptions for new chain set
|
|
1361
1304
|
__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_subscribeAssets).call(this);
|
|
1362
|
-
// Do one-time fetch for newly enabled chains
|
|
1305
|
+
// Do one-time fetch for newly enabled chains
|
|
1363
1306
|
if (addedChains.length > 0 && __classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get).length > 0) {
|
|
1364
1307
|
await this.getAssets(__classPrivateFieldGet(this, _AssetsController_instances, "a", _AssetsController_selectedAccounts_get), {
|
|
1365
1308
|
chainIds: addedChains,
|
|
1366
1309
|
forceUpdate: true,
|
|
1367
|
-
updateMode: 'merge',
|
|
1368
1310
|
});
|
|
1369
1311
|
}
|
|
1370
|
-
__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_ensureNativeBalancesDefaultZero).call(this);
|
|
1371
1312
|
};
|
|
1372
1313
|
//# sourceMappingURL=AssetsController.cjs.map
|