@metamask/assets-controller 9.0.1 → 9.1.0

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +43 -2
  2. package/dist/AssetsController.cjs +25 -15
  3. package/dist/AssetsController.cjs.map +1 -1
  4. package/dist/AssetsController.d.cts.map +1 -1
  5. package/dist/AssetsController.d.mts.map +1 -1
  6. package/dist/AssetsController.mjs +25 -15
  7. package/dist/AssetsController.mjs.map +1 -1
  8. package/dist/data-sources/AccountsApiDataSource.cjs +1 -1
  9. package/dist/data-sources/AccountsApiDataSource.cjs.map +1 -1
  10. package/dist/data-sources/AccountsApiDataSource.mjs +1 -1
  11. package/dist/data-sources/AccountsApiDataSource.mjs.map +1 -1
  12. package/dist/data-sources/PriceDataSource.cjs +64 -13
  13. package/dist/data-sources/PriceDataSource.cjs.map +1 -1
  14. package/dist/data-sources/PriceDataSource.d.cts +13 -0
  15. package/dist/data-sources/PriceDataSource.d.cts.map +1 -1
  16. package/dist/data-sources/PriceDataSource.d.mts +13 -0
  17. package/dist/data-sources/PriceDataSource.d.mts.map +1 -1
  18. package/dist/data-sources/PriceDataSource.mjs +64 -13
  19. package/dist/data-sources/PriceDataSource.mjs.map +1 -1
  20. package/dist/data-sources/SnapDataSource.cjs +2 -2
  21. package/dist/data-sources/SnapDataSource.cjs.map +1 -1
  22. package/dist/data-sources/SnapDataSource.mjs +2 -2
  23. package/dist/data-sources/SnapDataSource.mjs.map +1 -1
  24. package/dist/index.cjs +3 -1
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +2 -2
  27. package/dist/index.d.cts.map +1 -1
  28. package/dist/index.d.mts +2 -2
  29. package/dist/index.d.mts.map +1 -1
  30. package/dist/index.mjs +1 -1
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/middlewares/DetectionMiddleware.cjs +43 -23
  33. package/dist/middlewares/DetectionMiddleware.cjs.map +1 -1
  34. package/dist/middlewares/DetectionMiddleware.d.cts +12 -13
  35. package/dist/middlewares/DetectionMiddleware.d.cts.map +1 -1
  36. package/dist/middlewares/DetectionMiddleware.d.mts +12 -13
  37. package/dist/middlewares/DetectionMiddleware.d.mts.map +1 -1
  38. package/dist/middlewares/DetectionMiddleware.mjs +43 -23
  39. package/dist/middlewares/DetectionMiddleware.mjs.map +1 -1
  40. package/dist/selectors/balance.cjs +191 -8
  41. package/dist/selectors/balance.cjs.map +1 -1
  42. package/dist/selectors/balance.d.cts +80 -0
  43. package/dist/selectors/balance.d.cts.map +1 -1
  44. package/dist/selectors/balance.d.mts +80 -0
  45. package/dist/selectors/balance.d.mts.map +1 -1
  46. package/dist/selectors/balance.mjs +186 -7
  47. package/dist/selectors/balance.mjs.map +1 -1
  48. package/dist/utils/dedupingBatchFetcher.cjs +157 -0
  49. package/dist/utils/dedupingBatchFetcher.cjs.map +1 -0
  50. package/dist/utils/dedupingBatchFetcher.d.cts +65 -0
  51. package/dist/utils/dedupingBatchFetcher.d.cts.map +1 -0
  52. package/dist/utils/dedupingBatchFetcher.d.mts +65 -0
  53. package/dist/utils/dedupingBatchFetcher.d.mts.map +1 -0
  54. package/dist/utils/dedupingBatchFetcher.mjs +153 -0
  55. package/dist/utils/dedupingBatchFetcher.mjs.map +1 -0
  56. package/package.json +12 -12
package/CHANGELOG.md CHANGED
@@ -7,6 +7,46 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [9.1.0]
11
+
12
+ ### Added
13
+
14
+ - Add `calculateBalanceForAllWallets` and `calculateBalanceChangeForAccountGroup` selectors for computing wallet- and group-level balances from the unified `AssetsController` state ([#9230](https://github.com/MetaMask/core/pull/9230))
15
+ - Add `priceFreshnessTtlMs` option to `PriceDataSourceConfig` controlling how long a fetched price is considered fresh before it is re-fetched (defaults to the poll interval, 60 000 ms) ([#9189](https://github.com/MetaMask/core/pull/9189))
16
+ - Add `PriceDataSource.invalidatePriceCache()` to force the next fetch to bypass the freshness cache; `AssetsController` now calls it when the selected currency changes so cached prices in the previous currency are refreshed ([#9189](https://github.com/MetaMask/core/pull/9189))
17
+
18
+ ### Changed
19
+
20
+ - Bump `@metamask/account-tree-controller` from `^7.5.2` to `^7.5.3` ([#9231](https://github.com/MetaMask/core/pull/9231))
21
+ - Bump `@metamask/assets-controllers` from `^109.2.1` to `^109.2.2` ([#9231](https://github.com/MetaMask/core/pull/9231))
22
+ - Bump `@metamask/accounts-controller` from `^39.0.2` to `^39.0.3` ([#9231](https://github.com/MetaMask/core/pull/9231))
23
+ - Bump `@metamask/keyring-api` from `^23.1.0` to `^23.3.0` ([#9249](https://github.com/MetaMask/core/pull/9249))
24
+
25
+ ### Fixed
26
+
27
+ - Deduplicate price fetches in `PriceDataSource` so overlapping triggers (enrichment middleware, subscription polls, and manual refreshes) no longer issue duplicate price API requests for the same asset; assets fetched within the freshness TTL are skipped and concurrent in-flight fetches are joined ([#9189](https://github.com/MetaMask/core/pull/9189))
28
+ - Remove a redundant one-time balance fetch in `AssetsController` when the enabled network list changes, since subscription refresh and the enabled-networks handler already cover those fetches ([#9189](https://github.com/MetaMask/core/pull/9189))
29
+ - `DetectionMiddleware` no longer triggers redundant metadata and price fetches for assets already present in `assetsBalance` or `assetsInfo` state ([#9215](https://github.com/MetaMask/core/pull/9215))
30
+ - `AccountsApiDataSource` and `SnapDataSource` now use `merge` update mode instead of `full`, preventing assets on custom or partially-supported networks from being incorrectly wiped on each poll ([#9215](https://github.com/MetaMask/core/pull/9215))
31
+
32
+ ## [9.0.2]
33
+
34
+ ### Changed
35
+
36
+ - Bump `@metamask/assets-controllers` from `^109.0.0` to `^109.2.1` ([#9110](https://github.com/MetaMask/core/pull/9110), [#9202](https://github.com/MetaMask/core/pull/9202), [#9218](https://github.com/MetaMask/core/pull/9218))
37
+ - Bump `@metamask/utils` from `^11.9.0` to `^11.11.0` ([#9074](https://github.com/MetaMask/core/pull/9074))
38
+ - Bump `@metamask/transaction-controller` from `^67.1.0` to `^68.1.1` ([#9089](https://github.com/MetaMask/core/pull/9089), [#9177](https://github.com/MetaMask/core/pull/9177), [#9203](https://github.com/MetaMask/core/pull/9203), [#9218](https://github.com/MetaMask/core/pull/9218))
39
+ - Bump `@metamask/keyring-controller` from `^27.0.0` to `^27.1.0` ([#9129](https://github.com/MetaMask/core/pull/9129))
40
+ - Bump `@metamask/accounts-controller` from `^39.0.1` to `^39.0.2` ([#9218](https://github.com/MetaMask/core/pull/9218))
41
+ - Bump `@metamask/controller-utils` from `^12.2.0` to `^12.3.0` ([#9218](https://github.com/MetaMask/core/pull/9218))
42
+ - Bump `@metamask/network-controller` from `^32.0.0` to `^33.0.0` ([#9218](https://github.com/MetaMask/core/pull/9218))
43
+ - Bump `@metamask/network-enablement-controller` from `^5.3.0` to `^5.4.0` ([#9218](https://github.com/MetaMask/core/pull/9218))
44
+ - Bump `@metamask/polling-controller` from `^16.0.6` to `^16.0.7` ([#9218](https://github.com/MetaMask/core/pull/9218))
45
+
46
+ ### Fixed
47
+
48
+ - `AssetsController` reconciliate and self-heals stale assetInfo metadata types ([#9099](https://github.com/MetaMask/core/pull/9099))
49
+
10
50
  ## [9.0.1]
11
51
 
12
52
  ### Changed
@@ -23,7 +63,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
23
63
 
24
64
  - **BREAKING:** `RpcDataSourceOptions`, `TokenDataSourceOptions`, and `BackendWebsocketDataSourceOptions` no longer accept `isNativeAsset: (assetId) => boolean`; replace it with `getAssetType: (assetId) => 'native' | 'erc20' | 'spl'` ([#9063](https://github.com/MetaMask/core/pull/9063))
25
65
  - Token detection via `TokensApiClient` is now gated behind the `/v2/supportedNetworks` endpoint; chains absent from the supported-networks list are skipped and return an empty token list, and token-list request failures return an empty array instead of throwing ([#9063](https://github.com/MetaMask/core/pull/9063))
26
-
27
66
  - Use `encodeFunctionData` from `@metamask/controller-utils` for improved performance ([#9057](https://github.com/MetaMask/core/pull/9057))
28
67
  - Bump `@metamask/assets-controllers` from `^108.6.0` to `^109.0.0` ([#9078](https://github.com/MetaMask/core/pull/9078))
29
68
  - Bump `@metamask/transaction-controller` from `^67.0.0` to `^67.1.0` ([#9066](https://github.com/MetaMask/core/pull/9066))
@@ -613,7 +652,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
613
652
  - Refactor `RpcDataSource` to delegate polling to `BalanceFetcher` and `TokenDetector` services ([#7709](https://github.com/MetaMask/core/pull/7709))
614
653
  - Refactor `BalanceFetcher` and `TokenDetector` to extend `StaticIntervalPollingControllerOnly` for independent polling management ([#7709](https://github.com/MetaMask/core/pull/7709))
615
654
 
616
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@9.0.1...HEAD
655
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@9.1.0...HEAD
656
+ [9.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@9.0.2...@metamask/assets-controller@9.1.0
657
+ [9.0.2]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@9.0.1...@metamask/assets-controller@9.0.2
617
658
  [9.0.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@9.0.0...@metamask/assets-controller@9.0.1
618
659
  [9.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@8.3.3...@metamask/assets-controller@9.0.0
619
660
  [8.3.3]: https://github.com/MetaMask/core/compare/@metamask/assets-controller@8.3.2...@metamask/assets-controller@8.3.3
@@ -748,6 +748,8 @@ class AssetsController extends base_controller_1.BaseController {
748
748
  if (!__classPrivateFieldGet(this, _AssetsController_isBasicFunctionality, "f").call(this)) {
749
749
  return;
750
750
  }
751
+ // Currency changed — old cached prices are in the wrong currency.
752
+ __classPrivateFieldGet(this, _AssetsController_priceDataSource, "f").invalidatePriceCache();
751
753
  this.getAssets(__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSelectedAccounts).call(this), {
752
754
  forceUpdate: true,
753
755
  dataTypes: ['price'],
@@ -1157,23 +1159,12 @@ _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctional
1157
1159
  const addedChains = activeChains.filter((ch) => !previousSet.has(ch));
1158
1160
  const removedChains = previous.filter((ch) => !activeChains.includes(ch));
1159
1161
  if (addedChains.length > 0 || removedChains.length > 0) {
1160
- // Refresh subscriptions to use updated data source availability
1162
+ // Refresh subscriptions to use updated data source availability.
1163
+ // No one-time fetch needed here — #handleEnabledNetworksChanged
1164
+ // handles fetches when the user enables a network, and
1165
+ // #subscribeAssets re-subscribes with the correct chain assignment.
1161
1166
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_subscribeAssets).call(this);
1162
1167
  }
1163
- // If chains were added and we have selected accounts, do one-time fetch
1164
- if (addedChains.length > 0 && __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSelectedAccounts).call(this).length > 0) {
1165
- const addedEnabledChains = addedChains.filter((chain) => __classPrivateFieldGet(this, _AssetsController_enabledChains, "f").has(chain));
1166
- if (addedEnabledChains.length > 0) {
1167
- log('Fetching balances for newly added chains', { addedEnabledChains });
1168
- this.getAssets(__classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSelectedAccounts).call(this), {
1169
- chainIds: addedEnabledChains,
1170
- forceUpdate: true,
1171
- updateMode: 'merge',
1172
- }).catch((error) => {
1173
- log('Failed to fetch balance for added chains', { error });
1174
- });
1175
- }
1176
- }
1177
1168
  }, _AssetsController_executeMiddlewares =
1178
1169
  // ============================================================================
1179
1170
  // MIDDLEWARE EXECUTION
@@ -1417,6 +1408,25 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1417
1408
  }
1418
1409
  }
1419
1410
  }
1411
+ // Reconcile & self-heal stale asset "types" (e.g. erc20 -> native)
1412
+ // using IDs from new response assetInfo and assetBalance
1413
+ const assetsInfoAssetIdsSet = new Set([
1414
+ ...Object.keys(normalizedResponse.assetsInfo ?? {}),
1415
+ ...Object.values(normalizedResponse.assetsBalance ?? {}).flatMap((accountBalances) => Object.keys(accountBalances)),
1416
+ ]);
1417
+ for (const assetId of assetsInfoAssetIdsSet) {
1418
+ const entry = metadata[assetId];
1419
+ if (!entry) {
1420
+ continue;
1421
+ }
1422
+ const correctType = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getAssetType).call(this, assetId);
1423
+ if (entry.type !== correctType) {
1424
+ metadata[assetId] = { ...entry, type: correctType };
1425
+ if (!changedMetadata.includes(assetId)) {
1426
+ changedMetadata.push(assetId);
1427
+ }
1428
+ }
1429
+ }
1420
1430
  if (normalizedResponse.assetsBalance) {
1421
1431
  for (const [accountId, accountBalances] of Object.entries(normalizedResponse.assetsBalance)) {
1422
1432
  const previousBalances = previousState.assetsBalance[accountId] ?? {};