@metamask-previews/assets-controller 9.1.0-preview-a4e0c04 → 9.1.0-preview-9298fa429

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 +6 -2
  2. package/dist/AssetsController.cjs +150 -130
  3. package/dist/AssetsController.cjs.map +1 -1
  4. package/dist/AssetsController.d.cts +10 -7
  5. package/dist/AssetsController.d.cts.map +1 -1
  6. package/dist/AssetsController.d.mts +10 -7
  7. package/dist/AssetsController.d.mts.map +1 -1
  8. package/dist/AssetsController.mjs +150 -130
  9. package/dist/AssetsController.mjs.map +1 -1
  10. package/dist/data-sources/AccountsApiDataSource.cjs +1 -1
  11. package/dist/data-sources/AccountsApiDataSource.cjs.map +1 -1
  12. package/dist/data-sources/AccountsApiDataSource.mjs +1 -1
  13. package/dist/data-sources/AccountsApiDataSource.mjs.map +1 -1
  14. package/dist/data-sources/BackendWebsocketDataSource.cjs +26 -60
  15. package/dist/data-sources/BackendWebsocketDataSource.cjs.map +1 -1
  16. package/dist/data-sources/BackendWebsocketDataSource.d.cts.map +1 -1
  17. package/dist/data-sources/BackendWebsocketDataSource.d.mts.map +1 -1
  18. package/dist/data-sources/BackendWebsocketDataSource.mjs +26 -57
  19. package/dist/data-sources/BackendWebsocketDataSource.mjs.map +1 -1
  20. package/dist/data-sources/PriceDataSource.cjs +2 -0
  21. package/dist/data-sources/PriceDataSource.cjs.map +1 -1
  22. package/dist/data-sources/PriceDataSource.d.cts.map +1 -1
  23. package/dist/data-sources/PriceDataSource.d.mts.map +1 -1
  24. package/dist/data-sources/PriceDataSource.mjs +2 -0
  25. package/dist/data-sources/PriceDataSource.mjs.map +1 -1
  26. package/dist/data-sources/RpcDataSource.cjs +3 -2
  27. package/dist/data-sources/RpcDataSource.cjs.map +1 -1
  28. package/dist/data-sources/RpcDataSource.d.cts.map +1 -1
  29. package/dist/data-sources/RpcDataSource.d.mts.map +1 -1
  30. package/dist/data-sources/RpcDataSource.mjs +3 -2
  31. package/dist/data-sources/RpcDataSource.mjs.map +1 -1
  32. package/dist/data-sources/SnapDataSource.cjs +3 -3
  33. package/dist/data-sources/SnapDataSource.cjs.map +1 -1
  34. package/dist/data-sources/SnapDataSource.mjs +3 -3
  35. package/dist/data-sources/SnapDataSource.mjs.map +1 -1
  36. package/dist/middlewares/ParallelMiddleware.cjs +9 -0
  37. package/dist/middlewares/ParallelMiddleware.cjs.map +1 -1
  38. package/dist/middlewares/ParallelMiddleware.d.cts.map +1 -1
  39. package/dist/middlewares/ParallelMiddleware.d.mts.map +1 -1
  40. package/dist/middlewares/ParallelMiddleware.mjs +9 -0
  41. package/dist/middlewares/ParallelMiddleware.mjs.map +1 -1
  42. package/dist/types.cjs.map +1 -1
  43. package/dist/types.d.cts +8 -10
  44. package/dist/types.d.cts.map +1 -1
  45. package/dist/types.d.mts +8 -10
  46. package/dist/types.d.mts.map +1 -1
  47. package/dist/types.mjs.map +1 -1
  48. package/dist/utils/processAccountActivityBalanceUpdates.cjs +54 -0
  49. package/dist/utils/processAccountActivityBalanceUpdates.cjs.map +1 -0
  50. package/dist/utils/processAccountActivityBalanceUpdates.d.cts +13 -0
  51. package/dist/utils/processAccountActivityBalanceUpdates.d.cts.map +1 -0
  52. package/dist/utils/processAccountActivityBalanceUpdates.d.mts +13 -0
  53. package/dist/utils/processAccountActivityBalanceUpdates.d.mts.map +1 -0
  54. package/dist/utils/processAccountActivityBalanceUpdates.mjs +47 -0
  55. package/dist/utils/processAccountActivityBalanceUpdates.mjs.map +1 -0
  56. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -9,13 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
11
 
12
+ - **BREAKING:** `AssetsController` messenger must now allow `AccountActivityService:balanceUpdated` so unified `assetsBalance` state receives real-time websocket balance updates when `AccountActivityService` owns the server subscription ([#9273](https://github.com/MetaMask/core/pull/9273))
12
13
  - Bump `@metamask/transaction-controller` from `^68.1.1` to `^68.2.0` ([#9253](https://github.com/MetaMask/core/pull/9253))
13
14
 
14
15
  ### Fixed
15
16
 
16
- - Fix stale token balances after transactions when switching accounts or when websocket subscriptions reconnect; `AssetsController` now fetches before re-subscribing on account switch, serializes overlapping refresh work, treats `getAssets({ forceUpdate: true })` as authoritative over recent websocket freshness guards, and prevents passive polling from overwriting websocket balances for 120 seconds ([#9265](https://github.com/MetaMask/core/pull/9265))
17
+ - Fix stale token balances after transactions when switching accounts or when websocket subscriptions reconnect; `AssetsController` now fetches before re-subscribing on account switch and serializes overlapping refresh work ([#9265](https://github.com/MetaMask/core/pull/9265))
17
18
  - `AccountsApiDataSource` bypasses the TanStack Query balance cache when `forceUpdate` is true so forced refreshes return up-to-date balances instead of 60-second cached values ([#9265](https://github.com/MetaMask/core/pull/9265))
18
- - `BackendWebsocketDataSource` re-subscribes when subscribed accounts change (case-insensitive EVM address matching), serializes subscribe/unsubscribe to prevent races on account switch, and registers optional channel callbacks for more reliable notification delivery ([#9265](https://github.com/MetaMask/core/pull/9265))
19
+ - `BackendWebsocketDataSource` re-subscribes when subscribed accounts change (case-insensitive EVM address matching), serializes subscribe/unsubscribe to prevent races on account switch, and registers channel callbacks as a fallback when server `subscriptionId` values do not match ([#9265](https://github.com/MetaMask/core/pull/9265))
20
+ - Remove the 120-second websocket balance freshness guard that blocked force-refresh and polling updates from correcting stale websocket balances ([#9273](https://github.com/MetaMask/core/pull/9273))
21
+ - Add `update` balance update mode so fetch and force-refresh pipelines patch balances without removing tokens omitted from partial API snapshots; `getAssets({ forceUpdate: true })`, `AccountsApiDataSource`, and `SnapDataSource` use this mode ([#9273](https://github.com/MetaMask/core/pull/9273))
22
+ - `BackendWebsocketDataSource` registers subscription handlers before the subscribe handshake so in-flight account-activity notifications are not dropped, cleans up subscription state on subscribe failure, and resolves balance updates from stored subscription state when notifications arrive with stale subscription IDs ([#9273](https://github.com/MetaMask/core/pull/9273))
19
23
 
20
24
  ## [9.1.0]
21
25
 
@@ -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_trace, _AssetsController_captureException, _AssetsController_firstInitFetchReported, _AssetsController_stateSizeReported, _AssetsController_emitTrace, _AssetsController_emitStateSizeTrace, _AssetsController_uiOpen, _AssetsController_keyringUnlocked, _AssetsController_controllerMutex, _AssetsController_accountRefreshMutex, _AssetsController_activeSubscriptions, _AssetsController_enabledChains, _AssetsController_lastKnownAccountIds, _AssetsController_wsBalanceFreshUntil, _AssetsController_getSelectedAccounts, _AssetsController_backendWebsocketDataSource, _AssetsController_accountsApiDataSource, _AssetsController_snapDataSource, _AssetsController_rpcDataSource, _AssetsController_stakedBalanceDataSource, _AssetsController_allBalanceDataSources_get, _AssetsController_priceDataSource, _AssetsController_detectionMiddleware, _AssetsController_customAssetGraduationMiddleware, _AssetsController_rpcFallbackMiddleware, _AssetsController_tokenDataSource, _AssetsController_unsubscribeBasicFunctionality, _AssetsController_queryApiClient, _AssetsController_onActiveChainsUpdated, _AssetsController_initializeNativeAssetsMap, _AssetsController_initializeState, _AssetsController_extractEnabledChains, _AssetsController_normalizeChainReference, _AssetsController_subscribeToEvents, _AssetsController_onUnapprovedTransactionAdded, _AssetsController_updateActive, _AssetsController_handleAccountTreeStateChange, _AssetsController_runAccountTreeRefresh, _AssetsController_registerActionHandlers, _AssetsController_handleActiveChainsUpdate, _AssetsController_executeMiddlewares, _AssetsController_getNativeAssetMap, _AssetsController_isNativeAsset, _AssetsController_getAssetType, _AssetsController_resolveNativeAssetIds, _AssetsController_getNativeAssetIdsForEnabledChains, _AssetsController_getNativeAssetIdsForAccount, _AssetsController_ensureNativeBalancesDefaultZero, _AssetsController_ensureDefaultTrackedAssetsSeeded, _AssetsController_filterBalancesRespectingWsFreshness, _AssetsController_markWsBalancesFresh, _AssetsController_clearWsBalanceFreshness, _AssetsController_updateState, _AssetsController_getAssetsFromState, _AssetsController_shouldHideNativeToken, _AssetsController_tokenStandardToAssetType, _AssetsController_start, _AssetsController_stop, _AssetsController_subscribeAssets, _AssetsController_subscribeAssetsBalance, _AssetsController_subscribeRpcCustomAssetsSupplement, _AssetsController_unsubscribeBySubscriptionKey, _AssetsController_subscribeStakedBalance, _AssetsController_buildChainToAccountsMap, _AssetsController_subscribeDataSource, _AssetsController_unsubscribeDataSource, _AssetsController_buildDataRequest, _AssetsController_getEnabledChainsForAccount, _AssetsController_handleAccountGroupChanged, _AssetsController_handleEnabledNetworksChanged, _AssetsController_handleNetworkAdded, _AssetsController_refreshAssetsAfterNetworkChange;
16
+ var _AssetsController_instances, _AssetsController_isEnabled, _AssetsController_isBasicFunctionality, _AssetsController_defaultUpdateInterval, _AssetsController_trace, _AssetsController_captureException, _AssetsController_firstInitFetchReported, _AssetsController_stateSizeReported, _AssetsController_emitTrace, _AssetsController_emitStateSizeTrace, _AssetsController_uiOpen, _AssetsController_keyringUnlocked, _AssetsController_controllerMutex, _AssetsController_accountRefreshMutex, _AssetsController_activeSubscriptions, _AssetsController_enabledChains, _AssetsController_lastKnownAccountIds, _AssetsController_getSelectedAccounts, _AssetsController_backendWebsocketDataSource, _AssetsController_accountsApiDataSource, _AssetsController_snapDataSource, _AssetsController_rpcDataSource, _AssetsController_stakedBalanceDataSource, _AssetsController_allBalanceDataSources_get, _AssetsController_priceDataSource, _AssetsController_detectionMiddleware, _AssetsController_customAssetGraduationMiddleware, _AssetsController_rpcFallbackMiddleware, _AssetsController_tokenDataSource, _AssetsController_unsubscribeBasicFunctionality, _AssetsController_queryApiClient, _AssetsController_onActiveChainsUpdated, _AssetsController_initializeNativeAssetsMap, _AssetsController_initializeState, _AssetsController_extractEnabledChains, _AssetsController_normalizeChainReference, _AssetsController_subscribeToEvents, _AssetsController_onUnapprovedTransactionAdded, _AssetsController_onAccountActivityBalanceUpdated, _AssetsController_updateActive, _AssetsController_handleAccountTreeStateChange, _AssetsController_runAccountTreeRefresh, _AssetsController_registerActionHandlers, _AssetsController_handleActiveChainsUpdate, _AssetsController_executeMiddlewares, _AssetsController_getNativeAssetMap, _AssetsController_isNativeAsset, _AssetsController_getAssetType, _AssetsController_resolveNativeAssetIds, _AssetsController_getNativeAssetIdsForEnabledChains, _AssetsController_getNativeAssetIdsForAccount, _AssetsController_getSlowPipelineChainIds, _AssetsController_ensureNativeBalancesDefaultZero, _AssetsController_ensureDefaultTrackedAssetsSeeded, _AssetsController_updateState, _AssetsController_getAssetsFromState, _AssetsController_shouldHideNativeToken, _AssetsController_tokenStandardToAssetType, _AssetsController_start, _AssetsController_stop, _AssetsController_subscribeAssets, _AssetsController_subscribeAssetsBalance, _AssetsController_subscribeRpcCustomAssetsSupplement, _AssetsController_unsubscribeBySubscriptionKey, _AssetsController_subscribeStakedBalance, _AssetsController_buildChainToAccountsMap, _AssetsController_subscribeDataSource, _AssetsController_unsubscribeDataSource, _AssetsController_buildDataRequest, _AssetsController_getEnabledChainsForAccount, _AssetsController_handleAccountGroupChanged, _AssetsController_handleEnabledNetworksChanged, _AssetsController_handleNetworkAdded, _AssetsController_refreshAssetsAfterNetworkChange;
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");
@@ -40,6 +40,7 @@ const RpcFallbackMiddleware_1 = require("./middlewares/RpcFallbackMiddleware.cjs
40
40
  const utils_2 = require("./utils/index.cjs");
41
41
  const constants_1 = require("./utils/constants.cjs");
42
42
  const customAssetsRpcSupplement_1 = require("./utils/customAssetsRpcSupplement.cjs");
43
+ const processAccountActivityBalanceUpdates_1 = require("./utils/processAccountActivityBalanceUpdates.cjs");
43
44
  const NATIVE_ASSETS_QUERY_KEY = ['nativeAssets'];
44
45
  // ============================================================================
45
46
  // CONTROLLER CONSTANTS
@@ -62,15 +63,6 @@ const MESSENGER_EXPOSED_METHODS = [
62
63
  ];
63
64
  /** Default polling interval hint for data sources (30 seconds) */
64
65
  const DEFAULT_POLLING_INTERVAL_MS = 30000;
65
- /** Sources whose passive polling must not overwrite recent websocket balances. */
66
- const POLLING_BALANCE_SOURCES = new Set([
67
- 'AccountsApiDataSource',
68
- 'RpcDataSource',
69
- 'SnapDataSource',
70
- 'StakedBalanceDataSource',
71
- ]);
72
- /** How long websocket balance updates block stale polling overwrites. */
73
- const WS_BALANCE_FRESHNESS_MS = 120000;
74
66
  // ============================================================================
75
67
  // TRACE NAMES — used in Sentry spans (search these strings in Discover)
76
68
  // ============================================================================
@@ -201,9 +193,6 @@ function normalizeResponse(response) {
201
193
  if (response.updateMode) {
202
194
  normalized.updateMode = response.updateMode;
203
195
  }
204
- if (response.sourceId) {
205
- normalized.sourceId = response.sourceId;
206
- }
207
196
  return normalized;
208
197
  }
209
198
  // ============================================================================
@@ -284,11 +273,6 @@ class AssetsController extends base_controller_1.BaseController {
284
273
  * re-subscriptions when the account set hasn't actually changed.
285
274
  */
286
275
  _AssetsController_lastKnownAccountIds.set(this, new Set());
287
- /**
288
- * Per `accountId:assetId`, timestamp until which websocket balance updates
289
- * should not be overwritten by polling/API fetches.
290
- */
291
- _AssetsController_wsBalanceFreshUntil.set(this, new Map());
292
276
  _AssetsController_backendWebsocketDataSource.set(this, void 0);
293
277
  _AssetsController_accountsApiDataSource.set(this, void 0);
294
278
  _AssetsController_snapDataSource.set(this, void 0);
@@ -437,9 +421,9 @@ class AssetsController extends base_controller_1.BaseController {
437
421
  // creation, RPC is slow on many chains). Results are committed to state
438
422
  // immediately so the UI can display balances without waiting for them.
439
423
  //
440
- // Both the fast and background pipelines use 'merge' mode because neither
441
- // alone represents the full set of data sources. Using 'full' in either
442
- // would wipe balances from the sources handled by the other pipeline.
424
+ // Fast/slow pipelines use 'update' so partial API snapshots cannot wipe
425
+ // tokens missing from the response (e.g. USDC when only native balance
426
+ // is returned). Balances present in the response are still refreshed.
443
427
  const fastSources = __classPrivateFieldGet(this, _AssetsController_isBasicFunctionality, "f").call(this)
444
428
  ? [
445
429
  (0, ParallelMiddleware_1.createParallelBalanceMiddleware)([
@@ -459,39 +443,32 @@ class AssetsController extends base_controller_1.BaseController {
459
443
  ]
460
444
  : [__classPrivateFieldGet(this, _AssetsController_stakedBalanceDataSource, "f"), __classPrivateFieldGet(this, _AssetsController_detectionMiddleware, "f")];
461
445
  const { response, durationByDataSource } = await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_executeMiddlewares).call(this, fastSources, request);
462
- // The fast pipeline only contains a subset of data sources (AccountsApi +
463
- // StakedBalance), so it must always merge to avoid wiping Snap/RPC
464
- // balances that the background pipeline hasn't yet replaced.
465
- await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, {
466
- ...response,
467
- updateMode: 'merge',
468
- sourceId: 'getAssets:forceUpdate',
469
- });
446
+ await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, { ...response, updateMode: 'update' });
470
447
  // Background pipeline: snap and RPC run in parallel after the fast path
471
448
  // commits to state. Their balances are merged together before detection.
472
449
  // Token + price enrichment matches the pre-split behavior: only when basic
473
450
  // functionality is on (RPC-only mode must not call token/price APIs).
474
- const slowSources = __classPrivateFieldGet(this, _AssetsController_isBasicFunctionality, "f").call(this)
475
- ? [__classPrivateFieldGet(this, _AssetsController_snapDataSource, "f"), __classPrivateFieldGet(this, _AssetsController_rpcDataSource, "f")]
476
- : [__classPrivateFieldGet(this, _AssetsController_rpcDataSource, "f")];
477
- __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_executeMiddlewares).call(this, [
478
- (0, ParallelMiddleware_1.createParallelBalanceMiddleware)(slowSources),
479
- __classPrivateFieldGet(this, _AssetsController_detectionMiddleware, "f"),
480
- ...(__classPrivateFieldGet(this, _AssetsController_isBasicFunctionality, "f").call(this)
481
- ? [
482
- (0, ParallelMiddleware_1.createParallelMiddleware)([
483
- __classPrivateFieldGet(this, _AssetsController_tokenDataSource, "f"),
484
- __classPrivateFieldGet(this, _AssetsController_priceDataSource, "f"),
485
- ]),
486
- ]
487
- : []),
488
- ], request)
489
- .then(({ response: slowResponse }) => __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, {
490
- ...slowResponse,
491
- updateMode: 'merge',
492
- sourceId: 'getAssets:forceUpdate',
493
- }))
494
- .catch((error) => log('Background pipeline failed', { error }));
451
+ const slowPipelineChainIds = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSlowPipelineChainIds).call(this, chainIds, response);
452
+ if (slowPipelineChainIds.length > 0) {
453
+ const slowSources = __classPrivateFieldGet(this, _AssetsController_isBasicFunctionality, "f").call(this)
454
+ ? [__classPrivateFieldGet(this, _AssetsController_snapDataSource, "f"), __classPrivateFieldGet(this, _AssetsController_rpcDataSource, "f")]
455
+ : [__classPrivateFieldGet(this, _AssetsController_rpcDataSource, "f")];
456
+ const slowRequest = { ...request, chainIds: slowPipelineChainIds };
457
+ __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_executeMiddlewares).call(this, [
458
+ (0, ParallelMiddleware_1.createParallelBalanceMiddleware)(slowSources),
459
+ __classPrivateFieldGet(this, _AssetsController_detectionMiddleware, "f"),
460
+ ...(__classPrivateFieldGet(this, _AssetsController_isBasicFunctionality, "f").call(this)
461
+ ? [
462
+ (0, ParallelMiddleware_1.createParallelMiddleware)([
463
+ __classPrivateFieldGet(this, _AssetsController_tokenDataSource, "f"),
464
+ __classPrivateFieldGet(this, _AssetsController_priceDataSource, "f"),
465
+ ]),
466
+ ]
467
+ : []),
468
+ ], slowRequest)
469
+ .then(({ response: slowResponse }) => __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, { ...slowResponse, updateMode: 'update' }))
470
+ .catch((error) => log('Background pipeline failed', { error }));
471
+ }
495
472
  const durationMs = performance.now() - startTime;
496
473
  // Emit trace for every full fetch (Assets Health dashboard)
497
474
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_emitTrace).call(this, TRACE_FULL_FETCH, {
@@ -883,7 +860,8 @@ class AssetsController extends base_controller_1.BaseController {
883
860
  // RPC already fetches custom assets on purpose, and Snap handles non-EVM
884
861
  // chains the rule does not apply to, so skip the middleware for those.
885
862
  const shouldGraduateCustomAssets = sourceId === 'AccountsApiDataSource' ||
886
- sourceId === 'BackendWebsocketDataSource';
863
+ sourceId === 'BackendWebsocketDataSource' ||
864
+ sourceId === 'AccountActivityService';
887
865
  const enrichmentSources = [
888
866
  ...(shouldGraduateCustomAssets
889
867
  ? [__classPrivateFieldGet(this, _AssetsController_customAssetGraduationMiddleware, "f")]
@@ -897,11 +875,7 @@ class AssetsController extends base_controller_1.BaseController {
897
875
  ]));
898
876
  }
899
877
  const { response: enrichedResponse } = await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_executeMiddlewares).call(this, enrichmentSources, pipelineRequest, response);
900
- await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, { ...enrichedResponse, sourceId });
901
- if (sourceId === 'BackendWebsocketDataSource' &&
902
- enrichedResponse.assetsBalance) {
903
- __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_markWsBalancesFresh).call(this, enrichedResponse.assetsBalance);
904
- }
878
+ await __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateState).call(this, enrichedResponse);
905
879
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_emitTrace).call(this, TRACE_UPDATE_PIPELINE, {
906
880
  source: sourceId,
907
881
  duration_ms: performance.now() - updateStart,
@@ -949,7 +923,7 @@ class AssetsController extends base_controller_1.BaseController {
949
923
  }
950
924
  }
951
925
  exports.AssetsController = AssetsController;
952
- _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctionality = new WeakMap(), _AssetsController_defaultUpdateInterval = new WeakMap(), _AssetsController_trace = new WeakMap(), _AssetsController_captureException = new WeakMap(), _AssetsController_firstInitFetchReported = new WeakMap(), _AssetsController_stateSizeReported = new WeakMap(), _AssetsController_uiOpen = new WeakMap(), _AssetsController_keyringUnlocked = new WeakMap(), _AssetsController_controllerMutex = new WeakMap(), _AssetsController_accountRefreshMutex = new WeakMap(), _AssetsController_activeSubscriptions = new WeakMap(), _AssetsController_enabledChains = new WeakMap(), _AssetsController_lastKnownAccountIds = new WeakMap(), _AssetsController_wsBalanceFreshUntil = new WeakMap(), _AssetsController_backendWebsocketDataSource = new WeakMap(), _AssetsController_accountsApiDataSource = new WeakMap(), _AssetsController_snapDataSource = new WeakMap(), _AssetsController_rpcDataSource = new WeakMap(), _AssetsController_stakedBalanceDataSource = new WeakMap(), _AssetsController_priceDataSource = new WeakMap(), _AssetsController_detectionMiddleware = new WeakMap(), _AssetsController_customAssetGraduationMiddleware = new WeakMap(), _AssetsController_rpcFallbackMiddleware = new WeakMap(), _AssetsController_tokenDataSource = new WeakMap(), _AssetsController_unsubscribeBasicFunctionality = new WeakMap(), _AssetsController_queryApiClient = new WeakMap(), _AssetsController_onActiveChainsUpdated = new WeakMap(), _AssetsController_instances = new WeakSet(), _AssetsController_emitTrace = function _AssetsController_emitTrace(name, data, tags = {
926
+ _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctionality = new WeakMap(), _AssetsController_defaultUpdateInterval = new WeakMap(), _AssetsController_trace = new WeakMap(), _AssetsController_captureException = new WeakMap(), _AssetsController_firstInitFetchReported = new WeakMap(), _AssetsController_stateSizeReported = new WeakMap(), _AssetsController_uiOpen = new WeakMap(), _AssetsController_keyringUnlocked = new WeakMap(), _AssetsController_controllerMutex = new WeakMap(), _AssetsController_accountRefreshMutex = new WeakMap(), _AssetsController_activeSubscriptions = new WeakMap(), _AssetsController_enabledChains = new WeakMap(), _AssetsController_lastKnownAccountIds = new WeakMap(), _AssetsController_backendWebsocketDataSource = new WeakMap(), _AssetsController_accountsApiDataSource = new WeakMap(), _AssetsController_snapDataSource = new WeakMap(), _AssetsController_rpcDataSource = new WeakMap(), _AssetsController_stakedBalanceDataSource = new WeakMap(), _AssetsController_priceDataSource = new WeakMap(), _AssetsController_detectionMiddleware = new WeakMap(), _AssetsController_customAssetGraduationMiddleware = new WeakMap(), _AssetsController_rpcFallbackMiddleware = new WeakMap(), _AssetsController_tokenDataSource = new WeakMap(), _AssetsController_unsubscribeBasicFunctionality = new WeakMap(), _AssetsController_queryApiClient = new WeakMap(), _AssetsController_onActiveChainsUpdated = new WeakMap(), _AssetsController_instances = new WeakSet(), _AssetsController_emitTrace = function _AssetsController_emitTrace(name, data, tags = {
953
927
  controller: 'AssetsController',
954
928
  }) {
955
929
  if (!__classPrivateFieldGet(this, _AssetsController_trace, "f")) {
@@ -1075,11 +1049,11 @@ _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctional
1075
1049
  // The base-controller `:stateChange` event is guaranteed to fire
1076
1050
  // when init() calls this.update(). #start() is idempotent so
1077
1051
  // repeated fires are safe.
1078
- this.messenger.subscribe('AccountTreeController:stateChange', () => {
1052
+ this.messenger.subscribe('AccountTreeController:stateChanged', () => {
1079
1053
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_handleAccountTreeStateChange).call(this);
1080
1054
  });
1081
1055
  // Subscribe to network enablement changes (only enabledNetworkMap)
1082
- this.messenger.subscribe('NetworkEnablementController:stateChange', ({ enabledNetworkMap }) => {
1056
+ this.messenger.subscribe('NetworkEnablementController:stateChanged', ({ enabledNetworkMap }) => {
1083
1057
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_handleEnabledNetworksChanged).call(this, enabledNetworkMap).catch(console.error);
1084
1058
  });
1085
1059
  // When a network is added or removed from NetworkController, refresh
@@ -1096,7 +1070,7 @@ _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctional
1096
1070
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_refreshAssetsAfterNetworkChange).call(this);
1097
1071
  });
1098
1072
  // Client + Keyring lifecycle: only run when UI is open AND keyring is unlocked
1099
- this.messenger.subscribe('ClientController:stateChange', (isUiOpen) => {
1073
+ this.messenger.subscribe('ClientController:stateChanged', (isUiOpen) => {
1100
1074
  __classPrivateFieldSet(this, _AssetsController_uiOpen, isUiOpen, "f");
1101
1075
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_updateActive).call(this);
1102
1076
  }, client_controller_1.clientControllerSelectors.selectIsUiOpen);
@@ -1113,6 +1087,12 @@ _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctional
1113
1087
  this.messenger.subscribe('TransactionController:unapprovedTransactionAdded', (transactionMeta) => {
1114
1088
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_onUnapprovedTransactionAdded).call(this, transactionMeta);
1115
1089
  });
1090
+ // Real-time post-tx balances from AccountActivityService (same WS payload as
1091
+ // TokenBalancesController; BackendWebsocketDataSource may not receive the
1092
+ // callback when AccountActivityService owns the server subscription).
1093
+ this.messenger.subscribe('AccountActivityService:balanceUpdated', (event) => {
1094
+ __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_onAccountActivityBalanceUpdated).call(this, event);
1095
+ });
1116
1096
  }, _AssetsController_onUnapprovedTransactionAdded = function _AssetsController_onUnapprovedTransactionAdded(transactionMeta) {
1117
1097
  const hexChainId = transactionMeta.chainId;
1118
1098
  if (!hexChainId) {
@@ -1135,6 +1115,26 @@ _AssetsController_isEnabled = new WeakMap(), _AssetsController_isBasicFunctional
1135
1115
  error,
1136
1116
  });
1137
1117
  });
1118
+ }, _AssetsController_onAccountActivityBalanceUpdated = function _AssetsController_onAccountActivityBalanceUpdated({ address, chain, updates, }) {
1119
+ const account = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSelectedAccounts).call(this).find((a) => a.address.startsWith('0x')
1120
+ ? a.address.toLowerCase() === address.toLowerCase()
1121
+ : a.address === address);
1122
+ if (!account) {
1123
+ return;
1124
+ }
1125
+ const chainId = chain;
1126
+ const response = (0, processAccountActivityBalanceUpdates_1.processAccountActivityBalanceUpdates)(updates, account.id, (assetId) => __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getAssetType).call(this, assetId));
1127
+ if (!response.assetsBalance) {
1128
+ return;
1129
+ }
1130
+ const request = {
1131
+ accountsWithSupportedChains: [{ account, supportedChains: [chainId] }],
1132
+ chainIds: [chainId],
1133
+ dataTypes: ['balance', 'metadata'],
1134
+ };
1135
+ this.handleAssetsUpdate(response, 'AccountActivityService', request).catch((error) => {
1136
+ log('Failed to apply AccountActivityService balance update', { error });
1137
+ });
1138
1138
  }, _AssetsController_updateActive = function _AssetsController_updateActive() {
1139
1139
  const shouldRun = __classPrivateFieldGet(this, _AssetsController_uiOpen, "f") && __classPrivateFieldGet(this, _AssetsController_keyringUnlocked, "f");
1140
1140
  if (shouldRun) {
@@ -1344,6 +1344,17 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1344
1344
  return __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_resolveNativeAssetIds).call(this, __classPrivateFieldGet(this, _AssetsController_enabledChains, "f"));
1345
1345
  }, _AssetsController_getNativeAssetIdsForAccount = function _AssetsController_getNativeAssetIdsForAccount(account) {
1346
1346
  return __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_resolveNativeAssetIds).call(this, __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getEnabledChainsForAccount).call(this, account));
1347
+ }, _AssetsController_getSlowPipelineChainIds = function _AssetsController_getSlowPipelineChainIds(chainIds, fastResponse) {
1348
+ const accountsApiChains = new Set(__classPrivateFieldGet(this, _AssetsController_accountsApiDataSource, "f").getActiveChainsSync());
1349
+ return chainIds.filter((chainId) => {
1350
+ if (fastResponse.errors?.[chainId]) {
1351
+ return true;
1352
+ }
1353
+ if (!accountsApiChains.has(chainId)) {
1354
+ return true;
1355
+ }
1356
+ return false;
1357
+ });
1347
1358
  }, _AssetsController_ensureNativeBalancesDefaultZero = function _AssetsController_ensureNativeBalancesDefaultZero() {
1348
1359
  const accounts = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSelectedAccounts).call(this);
1349
1360
  if (accounts.length === 0) {
@@ -1361,7 +1372,7 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1361
1372
  balances[accountId] = {};
1362
1373
  }
1363
1374
  for (const nativeAssetId of nativeAssetIds) {
1364
- if (!(nativeAssetId in balances[accountId])) {
1375
+ if (!Object.prototype.hasOwnProperty.call(balances[accountId], nativeAssetId)) {
1365
1376
  balances[accountId][nativeAssetId] = { amount: '0' };
1366
1377
  }
1367
1378
  }
@@ -1411,37 +1422,9 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1411
1422
  }
1412
1423
  }
1413
1424
  });
1414
- }, _AssetsController_filterBalancesRespectingWsFreshness = function _AssetsController_filterBalancesRespectingWsFreshness(accountId, accountBalances, sourceId) {
1415
- if (!sourceId || !POLLING_BALANCE_SOURCES.has(sourceId)) {
1416
- return accountBalances;
1417
- }
1418
- const now = Date.now();
1419
- const filtered = {};
1420
- for (const [assetId, balance] of Object.entries(accountBalances)) {
1421
- const freshUntil = __classPrivateFieldGet(this, _AssetsController_wsBalanceFreshUntil, "f").get(`${accountId}:${assetId}`);
1422
- if (freshUntil !== undefined && now < freshUntil) {
1423
- continue;
1424
- }
1425
- filtered[assetId] = balance;
1426
- }
1427
- return filtered;
1428
- }, _AssetsController_markWsBalancesFresh = function _AssetsController_markWsBalancesFresh(assetsBalance) {
1429
- const freshUntil = Date.now() + WS_BALANCE_FRESHNESS_MS;
1430
- for (const [accountId, balances] of Object.entries(assetsBalance)) {
1431
- for (const assetId of Object.keys(balances)) {
1432
- __classPrivateFieldGet(this, _AssetsController_wsBalanceFreshUntil, "f").set(`${accountId}:${assetId}`, freshUntil);
1433
- }
1434
- }
1435
- }, _AssetsController_clearWsBalanceFreshness = function _AssetsController_clearWsBalanceFreshness(assetsBalance) {
1436
- for (const [accountId, balances] of Object.entries(assetsBalance)) {
1437
- for (const assetId of Object.keys(balances)) {
1438
- __classPrivateFieldGet(this, _AssetsController_wsBalanceFreshUntil, "f").delete(`${accountId}:${assetId}`);
1439
- }
1440
- }
1441
1425
  }, _AssetsController_updateState = async function _AssetsController_updateState(response) {
1442
1426
  const normalizedResponse = normalizeResponse(response);
1443
1427
  const mode = normalizedResponse.updateMode ?? 'merge';
1444
- const assetsBalanceToApply = normalizedResponse.assetsBalance;
1445
1428
  const releaseLock = await __classPrivateFieldGet(this, _AssetsController_controllerMutex, "f").acquire();
1446
1429
  try {
1447
1430
  const previousState = this.state;
@@ -1457,29 +1440,43 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1457
1440
  const balances = state.assetsBalance;
1458
1441
  const prices = state.assetsPrice;
1459
1442
  if (normalizedResponse.assetsInfo) {
1460
- for (const [key, value] of Object.entries(normalizedResponse.assetsInfo)) {
1461
- if (!(0, lodash_1.isEqual)(previousState.assetsInfo[key], value)) {
1443
+ if (mode === 'update') {
1444
+ // Balance-only refresh: patch amounts without overwriting metadata,
1445
+ // but seed entries that are missing so RPC-only chains (e.g.
1446
+ // Avalanche) can render native balances on first fetch.
1447
+ for (const [key, value] of Object.entries(normalizedResponse.assetsInfo)) {
1448
+ if (metadata[key]) {
1449
+ continue;
1450
+ }
1451
+ metadata[key] = value;
1462
1452
  changedMetadata.push(key);
1463
1453
  }
1464
- const existing = metadata[key];
1465
- const incoming = value;
1466
- // When the API returns no symbol or name for this token, it has no
1467
- // real knowledge of it (e.g. a user-deployed token not indexed by
1468
- // the API). Preserve richer metadata already in state (e.g. from
1469
- // pendingMetadata set by addCustomAsset) so that the correct
1470
- // decimals/symbol/name/image are not overwritten with empty values.
1471
- if (existing && !incoming.symbol && !incoming.name) {
1472
- metadata[key] = {
1473
- ...existing,
1474
- ...incoming,
1475
- symbol: existing.symbol,
1476
- name: existing.name,
1477
- decimals: existing.decimals ?? incoming.decimals,
1478
- image: existing.image ?? incoming.image,
1479
- };
1480
- }
1481
- else {
1482
- metadata[key] = value;
1454
+ }
1455
+ else {
1456
+ for (const [key, value] of Object.entries(normalizedResponse.assetsInfo)) {
1457
+ if (!(0, lodash_1.isEqual)(previousState.assetsInfo[key], value)) {
1458
+ changedMetadata.push(key);
1459
+ }
1460
+ const existing = metadata[key];
1461
+ const incoming = value;
1462
+ // When the API returns no symbol or name for this token, it has no
1463
+ // real knowledge of it (e.g. a user-deployed token not indexed by
1464
+ // the API). Preserve richer metadata already in state (e.g. from
1465
+ // pendingMetadata set by addCustomAsset) so that the correct
1466
+ // decimals/symbol/name/image are not overwritten with empty values.
1467
+ if (existing && !incoming.symbol && !incoming.name) {
1468
+ metadata[key] = {
1469
+ ...existing,
1470
+ ...incoming,
1471
+ symbol: existing.symbol,
1472
+ name: existing.name,
1473
+ decimals: existing.decimals ?? incoming.decimals,
1474
+ image: existing.image ?? incoming.image,
1475
+ };
1476
+ }
1477
+ else {
1478
+ metadata[key] = value;
1479
+ }
1483
1480
  }
1484
1481
  }
1485
1482
  }
@@ -1502,21 +1499,21 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1502
1499
  }
1503
1500
  }
1504
1501
  }
1505
- if (assetsBalanceToApply) {
1506
- for (const [accountId, accountBalances] of Object.entries(assetsBalanceToApply)) {
1507
- const filteredAccountBalances = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_filterBalancesRespectingWsFreshness).call(this, accountId, accountBalances, normalizedResponse.sourceId);
1502
+ if (normalizedResponse.assetsBalance) {
1503
+ for (const [accountId, accountBalances] of Object.entries(normalizedResponse.assetsBalance)) {
1508
1504
  const previousBalances = previousState.assetsBalance[accountId] ?? {};
1509
1505
  const customAssetIds = state.customAssets[accountId] ?? [];
1510
1506
  // Full: response is authoritative for the chains it covered;
1511
1507
  // balances for chains not in the response are preserved from
1512
1508
  // previous state so unsupported chains (e.g. Ink on AccountsAPI)
1513
1509
  // are never inadvertently reset to zero.
1514
- // Merge: response overlays previous balances.
1515
- const effective = mode === 'merge'
1516
- ? { ...previousBalances, ...filteredAccountBalances }
1510
+ // Merge / update: response overlays previous balances (update never
1511
+ // removes tokens missing from a partial snapshot).
1512
+ const effective = mode === 'merge' || mode === 'update'
1513
+ ? { ...previousBalances, ...accountBalances }
1517
1514
  : (() => {
1518
1515
  // Determine which chain namespaces this response covers.
1519
- const coveredChains = new Set(Object.keys(filteredAccountBalances).map((assetId) => assetId.split('/')[0]));
1516
+ const coveredChains = new Set(Object.keys(accountBalances).map((assetId) => assetId.split('/')[0]));
1520
1517
  // Start from previous balances, dropping only entries for
1521
1518
  // chains this response is authoritative over.
1522
1519
  const next = {};
@@ -1526,10 +1523,10 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1526
1523
  }
1527
1524
  }
1528
1525
  // Apply the response (authoritative for covered chains).
1529
- Object.assign(next, filteredAccountBalances);
1526
+ Object.assign(next, accountBalances);
1530
1527
  // Preserve custom assets that the response omitted.
1531
1528
  for (const customId of customAssetIds) {
1532
- if (!(customId in next)) {
1529
+ if (!Object.prototype.hasOwnProperty.call(next, customId)) {
1533
1530
  const prev = previousBalances[customId];
1534
1531
  next[customId] =
1535
1532
  prev ?? { amount: '0' };
@@ -1537,13 +1534,25 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1537
1534
  }
1538
1535
  return next;
1539
1536
  })();
1537
+ // Chains present in this partial response (e.g. Accounts API returned
1538
+ // ERC-20 zeros but omitted native after a max send).
1539
+ const coveredChainsInResponse = new Set(Object.keys(accountBalances).map((assetId) => assetId.split('/')[0]));
1540
1540
  // Ensure native tokens have an entry (0 if missing) for chains this account supports
1541
1541
  const account = __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getSelectedAccounts).call(this).find((a) => a.id === accountId);
1542
1542
  const nativeAssetIdsForAccount = account
1543
1543
  ? __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getNativeAssetIdsForAccount).call(this, account)
1544
1544
  : __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_getNativeAssetIdsForEnabledChains).call(this);
1545
1545
  for (const nativeAssetId of nativeAssetIdsForAccount) {
1546
- if (!(nativeAssetId in effective)) {
1546
+ const nativeChain = nativeAssetId.split('/')[0];
1547
+ if (!Object.prototype.hasOwnProperty.call(effective, nativeAssetId)) {
1548
+ effective[nativeAssetId] = { amount: '0' };
1549
+ }
1550
+ else if (mode === 'update' &&
1551
+ coveredChainsInResponse.has(nativeChain) &&
1552
+ !Object.prototype.hasOwnProperty.call(accountBalances, nativeAssetId)) {
1553
+ // Update-mode overlay keeps previous native when the response
1554
+ // omits it. If the API returned any balance on this chain,
1555
+ // treat omitted native as zero instead of a stale pre-tx amount.
1547
1556
  effective[nativeAssetId] = { amount: '0' };
1548
1557
  }
1549
1558
  }
@@ -1575,10 +1584,21 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1575
1584
  balances[accountId] = effective;
1576
1585
  }
1577
1586
  }
1578
- // Update prices in state
1587
+ // Update prices in state. Update mode only seeds missing entries so
1588
+ // balance-only refreshes do not clobber existing price data.
1579
1589
  if (normalizedResponse.assetsPrice) {
1580
- for (const [key, value] of Object.entries(normalizedResponse.assetsPrice)) {
1581
- prices[key] = value;
1590
+ if (mode === 'update') {
1591
+ for (const [key, value] of Object.entries(normalizedResponse.assetsPrice)) {
1592
+ if (prices[key]) {
1593
+ continue;
1594
+ }
1595
+ prices[key] = value;
1596
+ }
1597
+ }
1598
+ else {
1599
+ for (const [key, value] of Object.entries(normalizedResponse.assetsPrice)) {
1600
+ prices[key] = value;
1601
+ }
1582
1602
  }
1583
1603
  }
1584
1604
  });
@@ -1638,12 +1658,6 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1638
1658
  });
1639
1659
  }
1640
1660
  }
1641
- // Authoritative fetch on account switch — drop WS freshness locks so API
1642
- // balances (e.g. receiver +1 USDC) replace stale values from a prior send.
1643
- if (normalizedResponse.sourceId === 'getAssets:forceUpdate' &&
1644
- assetsBalanceToApply) {
1645
- __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_clearWsBalanceFreshness).call(this, assetsBalanceToApply);
1646
- }
1647
1661
  }
1648
1662
  finally {
1649
1663
  releaseLock();
@@ -1827,7 +1841,13 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1827
1841
  const seenIds = new Set();
1828
1842
  const accountsForSource = assignedChains
1829
1843
  .flatMap((chainId) => chainToAccounts.get(chainId) ?? [])
1830
- .filter((account) => !seenIds.has(account.id) && (seenIds.add(account.id), true));
1844
+ .filter((account) => {
1845
+ if (seenIds.has(account.id)) {
1846
+ return false;
1847
+ }
1848
+ seenIds.add(account.id);
1849
+ return true;
1850
+ });
1831
1851
  if (accountsForSource.length > 0) {
1832
1852
  __classPrivateFieldGet(this, _AssetsController_instances, "m", _AssetsController_subscribeDataSource).call(this, source, accountsForSource, assignedChains);
1833
1853
  }