@metamask/assets-controllers 109.4.1 → 110.0.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 (143) hide show
  1. package/CHANGELOG.md +41 -1
  2. package/LICENSE +1 -0
  3. package/dist/AssetsContractController.cjs +1 -0
  4. package/dist/AssetsContractController.cjs.map +1 -1
  5. package/dist/AssetsContractController.d.cts +1 -0
  6. package/dist/AssetsContractController.d.cts.map +1 -1
  7. package/dist/AssetsContractController.d.mts +1 -0
  8. package/dist/AssetsContractController.d.mts.map +1 -1
  9. package/dist/AssetsContractController.mjs +1 -0
  10. package/dist/AssetsContractController.mjs.map +1 -1
  11. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.cjs +7 -0
  12. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.cjs.map +1 -0
  13. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.cts +28 -0
  14. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.cts.map +1 -0
  15. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.mts +28 -0
  16. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.mts.map +1 -0
  17. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.mjs +6 -0
  18. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.mjs.map +1 -0
  19. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.cjs +128 -0
  20. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.cjs.map +1 -0
  21. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.cts +89 -0
  22. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.cts.map +1 -0
  23. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.mts +89 -0
  24. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.mts.map +1 -0
  25. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.mjs +123 -0
  26. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.mjs.map +1 -0
  27. package/dist/DeFiPositionsController/build-defi-balances-query.cjs +73 -0
  28. package/dist/DeFiPositionsController/build-defi-balances-query.cjs.map +1 -0
  29. package/dist/DeFiPositionsController/build-defi-balances-query.d.cts +34 -0
  30. package/dist/DeFiPositionsController/build-defi-balances-query.d.cts.map +1 -0
  31. package/dist/DeFiPositionsController/build-defi-balances-query.d.mts +34 -0
  32. package/dist/DeFiPositionsController/build-defi-balances-query.d.mts.map +1 -0
  33. package/dist/DeFiPositionsController/build-defi-balances-query.mjs +69 -0
  34. package/dist/DeFiPositionsController/build-defi-balances-query.mjs.map +1 -0
  35. package/dist/DeFiPositionsController/group-defi-positions-v6.cjs +235 -0
  36. package/dist/DeFiPositionsController/group-defi-positions-v6.cjs.map +1 -0
  37. package/dist/DeFiPositionsController/group-defi-positions-v6.d.cts +117 -0
  38. package/dist/DeFiPositionsController/group-defi-positions-v6.d.cts.map +1 -0
  39. package/dist/DeFiPositionsController/group-defi-positions-v6.d.mts +117 -0
  40. package/dist/DeFiPositionsController/group-defi-positions-v6.d.mts.map +1 -0
  41. package/dist/DeFiPositionsController/group-defi-positions-v6.mjs +231 -0
  42. package/dist/DeFiPositionsController/group-defi-positions-v6.mjs.map +1 -0
  43. package/dist/DeFiPositionsController/merge-positions-for-accounts.cjs +71 -0
  44. package/dist/DeFiPositionsController/merge-positions-for-accounts.cjs.map +1 -0
  45. package/dist/DeFiPositionsController/merge-positions-for-accounts.d.cts +16 -0
  46. package/dist/DeFiPositionsController/merge-positions-for-accounts.d.cts.map +1 -0
  47. package/dist/DeFiPositionsController/merge-positions-for-accounts.d.mts +16 -0
  48. package/dist/DeFiPositionsController/merge-positions-for-accounts.d.mts.map +1 -0
  49. package/dist/DeFiPositionsController/merge-positions-for-accounts.mjs +67 -0
  50. package/dist/DeFiPositionsController/merge-positions-for-accounts.mjs.map +1 -0
  51. package/dist/MultichainAssetsController/MultichainAssetsController.cjs +52 -29
  52. package/dist/MultichainAssetsController/MultichainAssetsController.cjs.map +1 -1
  53. package/dist/MultichainAssetsController/MultichainAssetsController.d.cts +3 -17
  54. package/dist/MultichainAssetsController/MultichainAssetsController.d.cts.map +1 -1
  55. package/dist/MultichainAssetsController/MultichainAssetsController.d.mts +3 -17
  56. package/dist/MultichainAssetsController/MultichainAssetsController.d.mts.map +1 -1
  57. package/dist/MultichainAssetsController/MultichainAssetsController.mjs +52 -29
  58. package/dist/MultichainAssetsController/MultichainAssetsController.mjs.map +1 -1
  59. package/dist/MultichainBalancesController/MultichainBalancesController.cjs +16 -62
  60. package/dist/MultichainBalancesController/MultichainBalancesController.cjs.map +1 -1
  61. package/dist/MultichainBalancesController/MultichainBalancesController.d.cts +5 -3
  62. package/dist/MultichainBalancesController/MultichainBalancesController.d.cts.map +1 -1
  63. package/dist/MultichainBalancesController/MultichainBalancesController.d.mts +5 -3
  64. package/dist/MultichainBalancesController/MultichainBalancesController.d.mts.map +1 -1
  65. package/dist/MultichainBalancesController/MultichainBalancesController.mjs +16 -62
  66. package/dist/MultichainBalancesController/MultichainBalancesController.mjs.map +1 -1
  67. package/dist/MultichainBalancesController/index.cjs.map +1 -1
  68. package/dist/MultichainBalancesController/index.d.cts +1 -1
  69. package/dist/MultichainBalancesController/index.d.cts.map +1 -1
  70. package/dist/MultichainBalancesController/index.d.mts +1 -1
  71. package/dist/MultichainBalancesController/index.d.mts.map +1 -1
  72. package/dist/MultichainBalancesController/index.mjs.map +1 -1
  73. package/dist/TokenDetectionController.cjs +34 -3
  74. package/dist/TokenDetectionController.cjs.map +1 -1
  75. package/dist/TokenDetectionController.d.cts +3 -1
  76. package/dist/TokenDetectionController.d.cts.map +1 -1
  77. package/dist/TokenDetectionController.d.mts +3 -1
  78. package/dist/TokenDetectionController.d.mts.map +1 -1
  79. package/dist/TokenDetectionController.mjs +34 -3
  80. package/dist/TokenDetectionController.mjs.map +1 -1
  81. package/dist/assetsUtil.cjs +1 -0
  82. package/dist/assetsUtil.cjs.map +1 -1
  83. package/dist/assetsUtil.d.cts +2 -1
  84. package/dist/assetsUtil.d.cts.map +1 -1
  85. package/dist/assetsUtil.d.mts +2 -1
  86. package/dist/assetsUtil.d.mts.map +1 -1
  87. package/dist/assetsUtil.mjs +1 -0
  88. package/dist/assetsUtil.mjs.map +1 -1
  89. package/dist/constants.cjs +2 -1
  90. package/dist/constants.cjs.map +1 -1
  91. package/dist/constants.d.cts.map +1 -1
  92. package/dist/constants.d.mts.map +1 -1
  93. package/dist/constants.mjs +2 -1
  94. package/dist/constants.mjs.map +1 -1
  95. package/dist/index.cjs +10 -1
  96. package/dist/index.cjs.map +1 -1
  97. package/dist/index.d.cts +7 -1
  98. package/dist/index.d.cts.map +1 -1
  99. package/dist/index.d.mts +7 -1
  100. package/dist/index.d.mts.map +1 -1
  101. package/dist/index.mjs +4 -1
  102. package/dist/index.mjs.map +1 -1
  103. package/dist/selectors/token-selectors.cjs +0 -3
  104. package/dist/selectors/token-selectors.cjs.map +1 -1
  105. package/dist/selectors/token-selectors.d.cts +25 -10
  106. package/dist/selectors/token-selectors.d.cts.map +1 -1
  107. package/dist/selectors/token-selectors.d.mts +25 -10
  108. package/dist/selectors/token-selectors.d.mts.map +1 -1
  109. package/dist/selectors/token-selectors.mjs +0 -3
  110. package/dist/selectors/token-selectors.mjs.map +1 -1
  111. package/dist/token-prices-service/codefi-v2.cjs +42 -22
  112. package/dist/token-prices-service/codefi-v2.cjs.map +1 -1
  113. package/dist/token-prices-service/codefi-v2.d.cts +25 -4
  114. package/dist/token-prices-service/codefi-v2.d.cts.map +1 -1
  115. package/dist/token-prices-service/codefi-v2.d.mts +25 -4
  116. package/dist/token-prices-service/codefi-v2.d.mts.map +1 -1
  117. package/dist/token-prices-service/codefi-v2.mjs +40 -21
  118. package/dist/token-prices-service/codefi-v2.mjs.map +1 -1
  119. package/dist/token-prices-service/index.cjs +2 -1
  120. package/dist/token-prices-service/index.cjs.map +1 -1
  121. package/dist/token-prices-service/index.d.cts +1 -1
  122. package/dist/token-prices-service/index.d.cts.map +1 -1
  123. package/dist/token-prices-service/index.d.mts +1 -1
  124. package/dist/token-prices-service/index.d.mts.map +1 -1
  125. package/dist/token-prices-service/index.mjs +1 -1
  126. package/dist/token-prices-service/index.mjs.map +1 -1
  127. package/dist/token-service.cjs +87 -8
  128. package/dist/token-service.cjs.map +1 -1
  129. package/dist/token-service.d.cts +4 -0
  130. package/dist/token-service.d.cts.map +1 -1
  131. package/dist/token-service.d.mts +4 -0
  132. package/dist/token-service.d.mts.map +1 -1
  133. package/dist/token-service.mjs +85 -7
  134. package/dist/token-service.mjs.map +1 -1
  135. package/package.json +11 -9
  136. package/dist/MultichainBalancesController/account-asset-info.cjs +0 -118
  137. package/dist/MultichainBalancesController/account-asset-info.cjs.map +0 -1
  138. package/dist/MultichainBalancesController/account-asset-info.d.cts +0 -82
  139. package/dist/MultichainBalancesController/account-asset-info.d.cts.map +0 -1
  140. package/dist/MultichainBalancesController/account-asset-info.d.mts +0 -82
  141. package/dist/MultichainBalancesController/account-asset-info.d.mts.map +0 -1
  142. package/dist/MultichainBalancesController/account-asset-info.mjs +0 -110
  143. package/dist/MultichainBalancesController/account-asset-info.mjs.map +0 -1
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _DeFiPositionsControllerV2_apiClient, _DeFiPositionsControllerV2_isEnabled, _DeFiPositionsControllerV2_getVsCurrency;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DeFiPositionsControllerV2 = exports.getDefaultDeFiPositionsControllerV2State = void 0;
16
+ const base_controller_1 = require("@metamask/base-controller");
17
+ const build_defi_balances_query_1 = require("./build-defi-balances-query.cjs");
18
+ const group_defi_positions_v6_1 = require("./group-defi-positions-v6.cjs");
19
+ const controllerName = 'DeFiPositionsControllerV2';
20
+ const MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'];
21
+ const controllerMetadata = {
22
+ allDeFiPositionsV2: {
23
+ includeInStateLogs: false,
24
+ persist: true,
25
+ includeInDebugSnapshot: false,
26
+ usedInUi: true,
27
+ },
28
+ };
29
+ const getDefaultDeFiPositionsControllerV2State = () => {
30
+ return {
31
+ allDeFiPositionsV2: {},
32
+ };
33
+ };
34
+ exports.getDefaultDeFiPositionsControllerV2State = getDefaultDeFiPositionsControllerV2State;
35
+ /**
36
+ * Controller that fetches DeFi positions for the selected account group from
37
+ * the Accounts API (v6 multiaccount balances) and stores them in the shape the
38
+ * client consumes directly.
39
+ *
40
+ * Deduplication and freshness are handled by the shared TanStack Query cache on
41
+ * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
42
+ * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
43
+ */
44
+ class DeFiPositionsControllerV2 extends base_controller_1.BaseController {
45
+ /**
46
+ * @param options - Constructor options.
47
+ * @param options.messenger - The controller messenger.
48
+ * @param options.apiClient - Accounts API client used to fetch balances/positions. Auth is handled by the client.
49
+ * @param options.isEnabled - Returns whether fetching is enabled (default: () => false).
50
+ * @param options.getVsCurrency - Returns the fiat currency for prices (default: () => 'usd').
51
+ * @param options.state - Initial controller state.
52
+ */
53
+ constructor({ messenger, apiClient, isEnabled, getVsCurrency, state, }) {
54
+ super({
55
+ name: controllerName,
56
+ metadata: controllerMetadata,
57
+ messenger,
58
+ state: {
59
+ ...(0, exports.getDefaultDeFiPositionsControllerV2State)(),
60
+ ...state,
61
+ },
62
+ });
63
+ _DeFiPositionsControllerV2_apiClient.set(this, void 0);
64
+ _DeFiPositionsControllerV2_isEnabled.set(this, void 0);
65
+ _DeFiPositionsControllerV2_getVsCurrency.set(this, void 0);
66
+ __classPrivateFieldSet(this, _DeFiPositionsControllerV2_apiClient, apiClient, "f");
67
+ __classPrivateFieldSet(this, _DeFiPositionsControllerV2_isEnabled, isEnabled, "f");
68
+ __classPrivateFieldSet(this, _DeFiPositionsControllerV2_getVsCurrency, getVsCurrency, "f");
69
+ this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
70
+ }
71
+ /**
72
+ * Fetches DeFi positions for the selected account group. Each account key in
73
+ * a ready response replaces that account's state (other accounts stay).
74
+ * Accounts still indexing (`processingDefiPositions`) are skipped so prior
75
+ * state is kept for them. No-ops when disabled or when the group has no
76
+ * supported accounts. Caching / spam prevention is handled by the apiClient
77
+ * TanStack Query cache (keyed by accounts + query options including
78
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
79
+ * pull-to-refresh).
80
+ *
81
+ * @param options - Optional fetch modifiers.
82
+ * @param options.forceRefresh - When true, bypass the apiClient cache and
83
+ * fetch immediately.
84
+ */
85
+ async fetchDeFiPositions(options) {
86
+ if (!__classPrivateFieldGet(this, _DeFiPositionsControllerV2_isEnabled, "f").call(this)) {
87
+ return;
88
+ }
89
+ const selectedAccounts = this.messenger.call('AccountTreeController:getAccountsFromSelectedAccountGroup');
90
+ const { networks, internalAccountIdByCaip } = (0, build_defi_balances_query_1.buildDeFiBalancesQuery)(selectedAccounts);
91
+ if (internalAccountIdByCaip.size === 0 || networks.length === 0) {
92
+ return;
93
+ }
94
+ const accountIds = [...internalAccountIdByCaip.keys()];
95
+ const vsCurrency = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_getVsCurrency, "f").call(this).toLowerCase();
96
+ try {
97
+ const response = await __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.fetchV6MultiAccountBalances(accountIds, {
98
+ networks,
99
+ includeDeFiBalances: true,
100
+ forceFetchDeFiPositions: true,
101
+ includePrices: true,
102
+ vsCurrency,
103
+ }, {
104
+ // staleTime: 0 makes TanStack treat the cache as stale for this call.
105
+ ...(options?.forceRefresh ? { staleTime: 0 } : {}),
106
+ });
107
+ // Skip accounts still indexing — their balances are not a valid snapshot.
108
+ const readyAccounts = response.accounts.filter((account) => !account.processingDefiPositions);
109
+ if (readyAccounts.length === 0) {
110
+ return;
111
+ }
112
+ const positionsByAccount = (0, group_defi_positions_v6_1.groupDeFiPositionsV6)({ ...response, accounts: readyAccounts }, internalAccountIdByCaip);
113
+ // Last valid response wins per ready account; processing / other accounts
114
+ // stay untouched.
115
+ this.update((state) => {
116
+ for (const [accountId, positions] of Object.entries(positionsByAccount)) {
117
+ state.allDeFiPositionsV2[accountId] = positions;
118
+ }
119
+ });
120
+ }
121
+ catch (error) {
122
+ console.error('Failed to fetch DeFi positions', error);
123
+ }
124
+ }
125
+ }
126
+ exports.DeFiPositionsControllerV2 = DeFiPositionsControllerV2;
127
+ _DeFiPositionsControllerV2_apiClient = new WeakMap(), _DeFiPositionsControllerV2_isEnabled = new WeakMap(), _DeFiPositionsControllerV2_getVsCurrency = new WeakMap();
128
+ //# sourceMappingURL=DeFiPositionsControllerV2.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeFiPositionsControllerV2.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,+DAA2D;AAS3D,+EAAqE;AAGrE,2EAAiE;AAEjE,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD,MAAM,yB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CAAC;oBACF,KAAK,CAAC,kBAAkB,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;gBAClD,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;QACzD,CAAC;IACH,CAAC;CACF;AAlID,8DAkIC","sourcesContent":["import type { AccountTreeControllerGetAccountsFromSelectedAccountGroupAction } from '@metamask/account-tree-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangedEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type { ApiPlatformClient } from '@metamask/core-backend';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { buildDeFiBalancesQuery } from './build-defi-balances-query';\nimport type { DeFiPositionsControllerV2MethodActions } from './DeFiPositionsControllerV2-method-action-types';\nimport type { DeFiPositionsByAccount } from './group-defi-positions-v6';\nimport { groupDeFiPositionsV6 } from './group-defi-positions-v6';\n\nconst controllerName = 'DeFiPositionsControllerV2';\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'] as const;\n\nexport type DeFiPositionsControllerV2State = {\n /**\n * DeFi positions keyed by internal MetaMask account ID (`InternalAccount.id`,\n * the same key AssetsController uses). Each account maps to a flat list of\n * protocol groups shown in the DeFi tab, each carrying its own `chainId` for\n * filtering plus the details-page sections embedded inside it. This is\n * exactly the shape the client consumes, so no further transformation is\n * needed on read.\n *\n * Named `allDeFiPositionsV2` (rather than `allDeFiPositions`) so it can live\n * alongside the legacy `DeFiPositionsController` in clients that flatten every\n * controller's state into a single object (e.g. the extension background),\n * without colliding on the shared `allDeFiPositions` key.\n */\n allDeFiPositionsV2: DeFiPositionsByAccount;\n};\n\nconst controllerMetadata: StateMetadata<DeFiPositionsControllerV2State> = {\n allDeFiPositionsV2: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\nexport const getDefaultDeFiPositionsControllerV2State =\n (): DeFiPositionsControllerV2State => {\n return {\n allDeFiPositionsV2: {},\n };\n };\n\nexport type DeFiPositionsControllerV2GetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DeFiPositionsControllerV2State\n>;\n\nexport type DeFiPositionsControllerV2Actions =\n | DeFiPositionsControllerV2GetStateAction\n | DeFiPositionsControllerV2MethodActions;\n\nexport type DeFiPositionsControllerV2StateChangedEvent =\n ControllerStateChangedEvent<\n typeof controllerName,\n DeFiPositionsControllerV2State\n >;\n\nexport type DeFiPositionsControllerV2Events =\n DeFiPositionsControllerV2StateChangedEvent;\n\n/**\n * The external actions available to the {@link DeFiPositionsControllerV2}.\n */\nexport type AllowedActions =\n AccountTreeControllerGetAccountsFromSelectedAccountGroupAction;\n\n/**\n * The external events available to the {@link DeFiPositionsControllerV2}.\n *\n * None yet — clients must call `fetchDeFiPositions` (and optionally\n * `{ forceRefresh: true }`) on their own triggers. Likely future subscriptions:\n * `AccountTreeController:selectedAccountGroupChange`,\n * `TransactionController:transactionConfirmed`, and `KeyringController:lock`.\n */\nexport type AllowedEvents = never;\n\nexport type DeFiPositionsControllerV2Messenger = Messenger<\n typeof controllerName,\n DeFiPositionsControllerV2Actions | AllowedActions,\n DeFiPositionsControllerV2Events | AllowedEvents\n>;\n\n/**\n * Controller that fetches DeFi positions for the selected account group from\n * the Accounts API (v6 multiaccount balances) and stores them in the shape the\n * client consumes directly.\n *\n * Deduplication and freshness are handled by the shared TanStack Query cache on\n * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass\n * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.\n */\nexport class DeFiPositionsControllerV2 extends BaseController<\n typeof controllerName,\n DeFiPositionsControllerV2State,\n DeFiPositionsControllerV2Messenger\n> {\n readonly #apiClient: ApiPlatformClient;\n\n readonly #isEnabled: () => boolean;\n\n readonly #getVsCurrency: () => string;\n\n /**\n * @param options - Constructor options.\n * @param options.messenger - The controller messenger.\n * @param options.apiClient - Accounts API client used to fetch balances/positions. Auth is handled by the client.\n * @param options.isEnabled - Returns whether fetching is enabled (default: () => false).\n * @param options.getVsCurrency - Returns the fiat currency for prices (default: () => 'usd').\n * @param options.state - Initial controller state.\n */\n constructor({\n messenger,\n apiClient,\n isEnabled,\n getVsCurrency,\n state,\n }: {\n messenger: DeFiPositionsControllerV2Messenger;\n apiClient: ApiPlatformClient;\n isEnabled: () => boolean;\n getVsCurrency: () => string;\n state?: Partial<DeFiPositionsControllerV2State>;\n }) {\n super({\n name: controllerName,\n metadata: controllerMetadata,\n messenger,\n state: {\n ...getDefaultDeFiPositionsControllerV2State(),\n ...state,\n },\n });\n\n this.#apiClient = apiClient;\n this.#isEnabled = isEnabled;\n this.#getVsCurrency = getVsCurrency;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Fetches DeFi positions for the selected account group. Each account key in\n * a ready response replaces that account's state (other accounts stay).\n * Accounts still indexing (`processingDefiPositions`) are skipped so prior\n * state is kept for them. No-ops when disabled or when the group has no\n * supported accounts. Caching / spam prevention is handled by the apiClient\n * TanStack Query cache (keyed by accounts + query options including\n * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.\n * pull-to-refresh).\n *\n * @param options - Optional fetch modifiers.\n * @param options.forceRefresh - When true, bypass the apiClient cache and\n * fetch immediately.\n */\n async fetchDeFiPositions(options?: {\n forceRefresh?: boolean;\n }): Promise<void> {\n if (!this.#isEnabled()) {\n return;\n }\n\n const selectedAccounts = this.messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n );\n\n const { networks, internalAccountIdByCaip } =\n buildDeFiBalancesQuery(selectedAccounts);\n\n if (internalAccountIdByCaip.size === 0 || networks.length === 0) {\n return;\n }\n\n const accountIds = [...internalAccountIdByCaip.keys()];\n const vsCurrency = this.#getVsCurrency().toLowerCase();\n\n try {\n const response =\n await this.#apiClient.accounts.fetchV6MultiAccountBalances(\n accountIds,\n {\n networks,\n includeDeFiBalances: true,\n forceFetchDeFiPositions: true,\n includePrices: true,\n vsCurrency,\n },\n {\n // staleTime: 0 makes TanStack treat the cache as stale for this call.\n ...(options?.forceRefresh ? { staleTime: 0 } : {}),\n },\n );\n\n // Skip accounts still indexing — their balances are not a valid snapshot.\n const readyAccounts = response.accounts.filter(\n (account) => !account.processingDefiPositions,\n );\n if (readyAccounts.length === 0) {\n return;\n }\n\n const positionsByAccount = groupDeFiPositionsV6(\n { ...response, accounts: readyAccounts },\n internalAccountIdByCaip,\n );\n\n // Last valid response wins per ready account; processing / other accounts\n // stay untouched.\n this.update((state) => {\n for (const [accountId, positions] of Object.entries(\n positionsByAccount,\n )) {\n state.allDeFiPositionsV2[accountId] = positions;\n }\n });\n } catch (error) {\n console.error('Failed to fetch DeFi positions', error);\n }\n }\n}\n"]}
@@ -0,0 +1,89 @@
1
+ import type { AccountTreeControllerGetAccountsFromSelectedAccountGroupAction } from "@metamask/account-tree-controller";
2
+ import { BaseController } from "@metamask/base-controller";
3
+ import type { ControllerGetStateAction, ControllerStateChangedEvent } from "@metamask/base-controller";
4
+ import type { ApiPlatformClient } from "@metamask/core-backend";
5
+ import type { Messenger } from "@metamask/messenger";
6
+ import type { DeFiPositionsControllerV2MethodActions } from "./DeFiPositionsControllerV2-method-action-types.cjs";
7
+ import type { DeFiPositionsByAccount } from "./group-defi-positions-v6.cjs";
8
+ declare const controllerName = "DeFiPositionsControllerV2";
9
+ export type DeFiPositionsControllerV2State = {
10
+ /**
11
+ * DeFi positions keyed by internal MetaMask account ID (`InternalAccount.id`,
12
+ * the same key AssetsController uses). Each account maps to a flat list of
13
+ * protocol groups shown in the DeFi tab, each carrying its own `chainId` for
14
+ * filtering plus the details-page sections embedded inside it. This is
15
+ * exactly the shape the client consumes, so no further transformation is
16
+ * needed on read.
17
+ *
18
+ * Named `allDeFiPositionsV2` (rather than `allDeFiPositions`) so it can live
19
+ * alongside the legacy `DeFiPositionsController` in clients that flatten every
20
+ * controller's state into a single object (e.g. the extension background),
21
+ * without colliding on the shared `allDeFiPositions` key.
22
+ */
23
+ allDeFiPositionsV2: DeFiPositionsByAccount;
24
+ };
25
+ export declare const getDefaultDeFiPositionsControllerV2State: () => DeFiPositionsControllerV2State;
26
+ export type DeFiPositionsControllerV2GetStateAction = ControllerGetStateAction<typeof controllerName, DeFiPositionsControllerV2State>;
27
+ export type DeFiPositionsControllerV2Actions = DeFiPositionsControllerV2GetStateAction | DeFiPositionsControllerV2MethodActions;
28
+ export type DeFiPositionsControllerV2StateChangedEvent = ControllerStateChangedEvent<typeof controllerName, DeFiPositionsControllerV2State>;
29
+ export type DeFiPositionsControllerV2Events = DeFiPositionsControllerV2StateChangedEvent;
30
+ /**
31
+ * The external actions available to the {@link DeFiPositionsControllerV2}.
32
+ */
33
+ export type AllowedActions = AccountTreeControllerGetAccountsFromSelectedAccountGroupAction;
34
+ /**
35
+ * The external events available to the {@link DeFiPositionsControllerV2}.
36
+ *
37
+ * None yet — clients must call `fetchDeFiPositions` (and optionally
38
+ * `{ forceRefresh: true }`) on their own triggers. Likely future subscriptions:
39
+ * `AccountTreeController:selectedAccountGroupChange`,
40
+ * `TransactionController:transactionConfirmed`, and `KeyringController:lock`.
41
+ */
42
+ export type AllowedEvents = never;
43
+ export type DeFiPositionsControllerV2Messenger = Messenger<typeof controllerName, DeFiPositionsControllerV2Actions | AllowedActions, DeFiPositionsControllerV2Events | AllowedEvents>;
44
+ /**
45
+ * Controller that fetches DeFi positions for the selected account group from
46
+ * the Accounts API (v6 multiaccount balances) and stores them in the shape the
47
+ * client consumes directly.
48
+ *
49
+ * Deduplication and freshness are handled by the shared TanStack Query cache on
50
+ * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
51
+ * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
52
+ */
53
+ export declare class DeFiPositionsControllerV2 extends BaseController<typeof controllerName, DeFiPositionsControllerV2State, DeFiPositionsControllerV2Messenger> {
54
+ #private;
55
+ /**
56
+ * @param options - Constructor options.
57
+ * @param options.messenger - The controller messenger.
58
+ * @param options.apiClient - Accounts API client used to fetch balances/positions. Auth is handled by the client.
59
+ * @param options.isEnabled - Returns whether fetching is enabled (default: () => false).
60
+ * @param options.getVsCurrency - Returns the fiat currency for prices (default: () => 'usd').
61
+ * @param options.state - Initial controller state.
62
+ */
63
+ constructor({ messenger, apiClient, isEnabled, getVsCurrency, state, }: {
64
+ messenger: DeFiPositionsControllerV2Messenger;
65
+ apiClient: ApiPlatformClient;
66
+ isEnabled: () => boolean;
67
+ getVsCurrency: () => string;
68
+ state?: Partial<DeFiPositionsControllerV2State>;
69
+ });
70
+ /**
71
+ * Fetches DeFi positions for the selected account group. Each account key in
72
+ * a ready response replaces that account's state (other accounts stay).
73
+ * Accounts still indexing (`processingDefiPositions`) are skipped so prior
74
+ * state is kept for them. No-ops when disabled or when the group has no
75
+ * supported accounts. Caching / spam prevention is handled by the apiClient
76
+ * TanStack Query cache (keyed by accounts + query options including
77
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
78
+ * pull-to-refresh).
79
+ *
80
+ * @param options - Optional fetch modifiers.
81
+ * @param options.forceRefresh - When true, bypass the apiClient cache and
82
+ * fetch immediately.
83
+ */
84
+ fetchDeFiPositions(options?: {
85
+ forceRefresh?: boolean;
86
+ }): Promise<void>;
87
+ }
88
+ export {};
89
+ //# sourceMappingURL=DeFiPositionsControllerV2.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeFiPositionsControllerV2.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8DAA8D,EAAE,0CAA0C;AACxH,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,2BAA2B,EAE5B,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,+BAA+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,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAqBD;;;;;;;;;;;;;OAaG;IACG,kBAAkB,CAAC,OAAO,CAAC,EAAE;QACjC,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB,GAAG,OAAO,CAAC,IAAI,CAAC;CA8DlB"}
@@ -0,0 +1,89 @@
1
+ import type { AccountTreeControllerGetAccountsFromSelectedAccountGroupAction } from "@metamask/account-tree-controller";
2
+ import { BaseController } from "@metamask/base-controller";
3
+ import type { ControllerGetStateAction, ControllerStateChangedEvent } from "@metamask/base-controller";
4
+ import type { ApiPlatformClient } from "@metamask/core-backend";
5
+ import type { Messenger } from "@metamask/messenger";
6
+ import type { DeFiPositionsControllerV2MethodActions } from "./DeFiPositionsControllerV2-method-action-types.mjs";
7
+ import type { DeFiPositionsByAccount } from "./group-defi-positions-v6.mjs";
8
+ declare const controllerName = "DeFiPositionsControllerV2";
9
+ export type DeFiPositionsControllerV2State = {
10
+ /**
11
+ * DeFi positions keyed by internal MetaMask account ID (`InternalAccount.id`,
12
+ * the same key AssetsController uses). Each account maps to a flat list of
13
+ * protocol groups shown in the DeFi tab, each carrying its own `chainId` for
14
+ * filtering plus the details-page sections embedded inside it. This is
15
+ * exactly the shape the client consumes, so no further transformation is
16
+ * needed on read.
17
+ *
18
+ * Named `allDeFiPositionsV2` (rather than `allDeFiPositions`) so it can live
19
+ * alongside the legacy `DeFiPositionsController` in clients that flatten every
20
+ * controller's state into a single object (e.g. the extension background),
21
+ * without colliding on the shared `allDeFiPositions` key.
22
+ */
23
+ allDeFiPositionsV2: DeFiPositionsByAccount;
24
+ };
25
+ export declare const getDefaultDeFiPositionsControllerV2State: () => DeFiPositionsControllerV2State;
26
+ export type DeFiPositionsControllerV2GetStateAction = ControllerGetStateAction<typeof controllerName, DeFiPositionsControllerV2State>;
27
+ export type DeFiPositionsControllerV2Actions = DeFiPositionsControllerV2GetStateAction | DeFiPositionsControllerV2MethodActions;
28
+ export type DeFiPositionsControllerV2StateChangedEvent = ControllerStateChangedEvent<typeof controllerName, DeFiPositionsControllerV2State>;
29
+ export type DeFiPositionsControllerV2Events = DeFiPositionsControllerV2StateChangedEvent;
30
+ /**
31
+ * The external actions available to the {@link DeFiPositionsControllerV2}.
32
+ */
33
+ export type AllowedActions = AccountTreeControllerGetAccountsFromSelectedAccountGroupAction;
34
+ /**
35
+ * The external events available to the {@link DeFiPositionsControllerV2}.
36
+ *
37
+ * None yet — clients must call `fetchDeFiPositions` (and optionally
38
+ * `{ forceRefresh: true }`) on their own triggers. Likely future subscriptions:
39
+ * `AccountTreeController:selectedAccountGroupChange`,
40
+ * `TransactionController:transactionConfirmed`, and `KeyringController:lock`.
41
+ */
42
+ export type AllowedEvents = never;
43
+ export type DeFiPositionsControllerV2Messenger = Messenger<typeof controllerName, DeFiPositionsControllerV2Actions | AllowedActions, DeFiPositionsControllerV2Events | AllowedEvents>;
44
+ /**
45
+ * Controller that fetches DeFi positions for the selected account group from
46
+ * the Accounts API (v6 multiaccount balances) and stores them in the shape the
47
+ * client consumes directly.
48
+ *
49
+ * Deduplication and freshness are handled by the shared TanStack Query cache on
50
+ * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
51
+ * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
52
+ */
53
+ export declare class DeFiPositionsControllerV2 extends BaseController<typeof controllerName, DeFiPositionsControllerV2State, DeFiPositionsControllerV2Messenger> {
54
+ #private;
55
+ /**
56
+ * @param options - Constructor options.
57
+ * @param options.messenger - The controller messenger.
58
+ * @param options.apiClient - Accounts API client used to fetch balances/positions. Auth is handled by the client.
59
+ * @param options.isEnabled - Returns whether fetching is enabled (default: () => false).
60
+ * @param options.getVsCurrency - Returns the fiat currency for prices (default: () => 'usd').
61
+ * @param options.state - Initial controller state.
62
+ */
63
+ constructor({ messenger, apiClient, isEnabled, getVsCurrency, state, }: {
64
+ messenger: DeFiPositionsControllerV2Messenger;
65
+ apiClient: ApiPlatformClient;
66
+ isEnabled: () => boolean;
67
+ getVsCurrency: () => string;
68
+ state?: Partial<DeFiPositionsControllerV2State>;
69
+ });
70
+ /**
71
+ * Fetches DeFi positions for the selected account group. Each account key in
72
+ * a ready response replaces that account's state (other accounts stay).
73
+ * Accounts still indexing (`processingDefiPositions`) are skipped so prior
74
+ * state is kept for them. No-ops when disabled or when the group has no
75
+ * supported accounts. Caching / spam prevention is handled by the apiClient
76
+ * TanStack Query cache (keyed by accounts + query options including
77
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
78
+ * pull-to-refresh).
79
+ *
80
+ * @param options - Optional fetch modifiers.
81
+ * @param options.forceRefresh - When true, bypass the apiClient cache and
82
+ * fetch immediately.
83
+ */
84
+ fetchDeFiPositions(options?: {
85
+ forceRefresh?: boolean;
86
+ }): Promise<void>;
87
+ }
88
+ export {};
89
+ //# sourceMappingURL=DeFiPositionsControllerV2.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeFiPositionsControllerV2.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8DAA8D,EAAE,0CAA0C;AACxH,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,2BAA2B,EAE5B,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,+BAA+B;AAChE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EAAE,sCAAsC,EAAE,4DAAwD;AAC9G,OAAO,KAAK,EAAE,sBAAsB,EAAE,sCAAkC;AAGxE,QAAA,MAAM,cAAc,8BAA8B,CAAC;AAInD,MAAM,MAAM,8BAA8B,GAAG;IAC3C;;;;;;;;;;;;OAYG;IACH,kBAAkB,EAAE,sBAAsB,CAAC;CAC5C,CAAC;AAWF,eAAO,MAAM,wCAAwC,QAC/C,8BAIH,CAAC;AAEJ,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,cAAc,EACrB,8BAA8B,CAC/B,CAAC;AAEF,MAAM,MAAM,gCAAgC,GACxC,uCAAuC,GACvC,sCAAsC,CAAC;AAE3C,MAAM,MAAM,0CAA0C,GACpD,2BAA2B,CACzB,OAAO,cAAc,EACrB,8BAA8B,CAC/B,CAAC;AAEJ,MAAM,MAAM,+BAA+B,GACzC,0CAA0C,CAAC;AAE7C;;GAEG;AACH,MAAM,MAAM,cAAc,GACxB,8DAA8D,CAAC;AAEjE;;;;;;;GAOG;AACH,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC;AAElC,MAAM,MAAM,kCAAkC,GAAG,SAAS,CACxD,OAAO,cAAc,EACrB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,CAChD,CAAC;AAEF;;;;;;;;GAQG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAOC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,aAAa,EACb,KAAK,GACN,EAAE;QACD,SAAS,EAAE,kCAAkC,CAAC;QAC9C,SAAS,EAAE,iBAAiB,CAAC;QAC7B,SAAS,EAAE,MAAM,OAAO,CAAC;QACzB,aAAa,EAAE,MAAM,MAAM,CAAC;QAC5B,KAAK,CAAC,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAqBD;;;;;;;;;;;;;OAaG;IACG,kBAAkB,CAAC,OAAO,CAAC,EAAE;QACjC,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB,GAAG,OAAO,CAAC,IAAI,CAAC;CA8DlB"}
@@ -0,0 +1,123 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _DeFiPositionsControllerV2_apiClient, _DeFiPositionsControllerV2_isEnabled, _DeFiPositionsControllerV2_getVsCurrency;
13
+ import { BaseController } from "@metamask/base-controller";
14
+ import { buildDeFiBalancesQuery } from "./build-defi-balances-query.mjs";
15
+ import { groupDeFiPositionsV6 } from "./group-defi-positions-v6.mjs";
16
+ const controllerName = 'DeFiPositionsControllerV2';
17
+ const MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'];
18
+ const controllerMetadata = {
19
+ allDeFiPositionsV2: {
20
+ includeInStateLogs: false,
21
+ persist: true,
22
+ includeInDebugSnapshot: false,
23
+ usedInUi: true,
24
+ },
25
+ };
26
+ export const getDefaultDeFiPositionsControllerV2State = () => {
27
+ return {
28
+ allDeFiPositionsV2: {},
29
+ };
30
+ };
31
+ /**
32
+ * Controller that fetches DeFi positions for the selected account group from
33
+ * the Accounts API (v6 multiaccount balances) and stores them in the shape the
34
+ * client consumes directly.
35
+ *
36
+ * Deduplication and freshness are handled by the shared TanStack Query cache on
37
+ * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
38
+ * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
39
+ */
40
+ export class DeFiPositionsControllerV2 extends BaseController {
41
+ /**
42
+ * @param options - Constructor options.
43
+ * @param options.messenger - The controller messenger.
44
+ * @param options.apiClient - Accounts API client used to fetch balances/positions. Auth is handled by the client.
45
+ * @param options.isEnabled - Returns whether fetching is enabled (default: () => false).
46
+ * @param options.getVsCurrency - Returns the fiat currency for prices (default: () => 'usd').
47
+ * @param options.state - Initial controller state.
48
+ */
49
+ constructor({ messenger, apiClient, isEnabled, getVsCurrency, state, }) {
50
+ super({
51
+ name: controllerName,
52
+ metadata: controllerMetadata,
53
+ messenger,
54
+ state: {
55
+ ...getDefaultDeFiPositionsControllerV2State(),
56
+ ...state,
57
+ },
58
+ });
59
+ _DeFiPositionsControllerV2_apiClient.set(this, void 0);
60
+ _DeFiPositionsControllerV2_isEnabled.set(this, void 0);
61
+ _DeFiPositionsControllerV2_getVsCurrency.set(this, void 0);
62
+ __classPrivateFieldSet(this, _DeFiPositionsControllerV2_apiClient, apiClient, "f");
63
+ __classPrivateFieldSet(this, _DeFiPositionsControllerV2_isEnabled, isEnabled, "f");
64
+ __classPrivateFieldSet(this, _DeFiPositionsControllerV2_getVsCurrency, getVsCurrency, "f");
65
+ this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
66
+ }
67
+ /**
68
+ * Fetches DeFi positions for the selected account group. Each account key in
69
+ * a ready response replaces that account's state (other accounts stay).
70
+ * Accounts still indexing (`processingDefiPositions`) are skipped so prior
71
+ * state is kept for them. No-ops when disabled or when the group has no
72
+ * supported accounts. Caching / spam prevention is handled by the apiClient
73
+ * TanStack Query cache (keyed by accounts + query options including
74
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
75
+ * pull-to-refresh).
76
+ *
77
+ * @param options - Optional fetch modifiers.
78
+ * @param options.forceRefresh - When true, bypass the apiClient cache and
79
+ * fetch immediately.
80
+ */
81
+ async fetchDeFiPositions(options) {
82
+ if (!__classPrivateFieldGet(this, _DeFiPositionsControllerV2_isEnabled, "f").call(this)) {
83
+ return;
84
+ }
85
+ const selectedAccounts = this.messenger.call('AccountTreeController:getAccountsFromSelectedAccountGroup');
86
+ const { networks, internalAccountIdByCaip } = buildDeFiBalancesQuery(selectedAccounts);
87
+ if (internalAccountIdByCaip.size === 0 || networks.length === 0) {
88
+ return;
89
+ }
90
+ const accountIds = [...internalAccountIdByCaip.keys()];
91
+ const vsCurrency = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_getVsCurrency, "f").call(this).toLowerCase();
92
+ try {
93
+ const response = await __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.fetchV6MultiAccountBalances(accountIds, {
94
+ networks,
95
+ includeDeFiBalances: true,
96
+ forceFetchDeFiPositions: true,
97
+ includePrices: true,
98
+ vsCurrency,
99
+ }, {
100
+ // staleTime: 0 makes TanStack treat the cache as stale for this call.
101
+ ...(options?.forceRefresh ? { staleTime: 0 } : {}),
102
+ });
103
+ // Skip accounts still indexing — their balances are not a valid snapshot.
104
+ const readyAccounts = response.accounts.filter((account) => !account.processingDefiPositions);
105
+ if (readyAccounts.length === 0) {
106
+ return;
107
+ }
108
+ const positionsByAccount = groupDeFiPositionsV6({ ...response, accounts: readyAccounts }, internalAccountIdByCaip);
109
+ // Last valid response wins per ready account; processing / other accounts
110
+ // stay untouched.
111
+ this.update((state) => {
112
+ for (const [accountId, positions] of Object.entries(positionsByAccount)) {
113
+ state.allDeFiPositionsV2[accountId] = positions;
114
+ }
115
+ });
116
+ }
117
+ catch (error) {
118
+ console.error('Failed to fetch DeFi positions', error);
119
+ }
120
+ }
121
+ }
122
+ _DeFiPositionsControllerV2_apiClient = new WeakMap(), _DeFiPositionsControllerV2_isEnabled = new WeakMap(), _DeFiPositionsControllerV2_getVsCurrency = new WeakMap();
123
+ //# sourceMappingURL=DeFiPositionsControllerV2.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeFiPositionsControllerV2.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAS3D,OAAO,EAAE,sBAAsB,EAAE,wCAAoC;AAGr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,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CACjD,kBAAkB,CACnB,EAAE,CAAC;oBACF,KAAK,CAAC,kBAAkB,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;gBAClD,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;QACzD,CAAC;IACH,CAAC;CACF","sourcesContent":["import type { AccountTreeControllerGetAccountsFromSelectedAccountGroupAction } from '@metamask/account-tree-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangedEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type { ApiPlatformClient } from '@metamask/core-backend';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { buildDeFiBalancesQuery } from './build-defi-balances-query';\nimport type { DeFiPositionsControllerV2MethodActions } from './DeFiPositionsControllerV2-method-action-types';\nimport type { DeFiPositionsByAccount } from './group-defi-positions-v6';\nimport { groupDeFiPositionsV6 } from './group-defi-positions-v6';\n\nconst controllerName = 'DeFiPositionsControllerV2';\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'] as const;\n\nexport type DeFiPositionsControllerV2State = {\n /**\n * DeFi positions keyed by internal MetaMask account ID (`InternalAccount.id`,\n * the same key AssetsController uses). Each account maps to a flat list of\n * protocol groups shown in the DeFi tab, each carrying its own `chainId` for\n * filtering plus the details-page sections embedded inside it. This is\n * exactly the shape the client consumes, so no further transformation is\n * needed on read.\n *\n * Named `allDeFiPositionsV2` (rather than `allDeFiPositions`) so it can live\n * alongside the legacy `DeFiPositionsController` in clients that flatten every\n * controller's state into a single object (e.g. the extension background),\n * without colliding on the shared `allDeFiPositions` key.\n */\n allDeFiPositionsV2: DeFiPositionsByAccount;\n};\n\nconst controllerMetadata: StateMetadata<DeFiPositionsControllerV2State> = {\n allDeFiPositionsV2: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\nexport const getDefaultDeFiPositionsControllerV2State =\n (): DeFiPositionsControllerV2State => {\n return {\n allDeFiPositionsV2: {},\n };\n };\n\nexport type DeFiPositionsControllerV2GetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DeFiPositionsControllerV2State\n>;\n\nexport type DeFiPositionsControllerV2Actions =\n | DeFiPositionsControllerV2GetStateAction\n | DeFiPositionsControllerV2MethodActions;\n\nexport type DeFiPositionsControllerV2StateChangedEvent =\n ControllerStateChangedEvent<\n typeof controllerName,\n DeFiPositionsControllerV2State\n >;\n\nexport type DeFiPositionsControllerV2Events =\n DeFiPositionsControllerV2StateChangedEvent;\n\n/**\n * The external actions available to the {@link DeFiPositionsControllerV2}.\n */\nexport type AllowedActions =\n AccountTreeControllerGetAccountsFromSelectedAccountGroupAction;\n\n/**\n * The external events available to the {@link DeFiPositionsControllerV2}.\n *\n * None yet — clients must call `fetchDeFiPositions` (and optionally\n * `{ forceRefresh: true }`) on their own triggers. Likely future subscriptions:\n * `AccountTreeController:selectedAccountGroupChange`,\n * `TransactionController:transactionConfirmed`, and `KeyringController:lock`.\n */\nexport type AllowedEvents = never;\n\nexport type DeFiPositionsControllerV2Messenger = Messenger<\n typeof controllerName,\n DeFiPositionsControllerV2Actions | AllowedActions,\n DeFiPositionsControllerV2Events | AllowedEvents\n>;\n\n/**\n * Controller that fetches DeFi positions for the selected account group from\n * the Accounts API (v6 multiaccount balances) and stores them in the shape the\n * client consumes directly.\n *\n * Deduplication and freshness are handled by the shared TanStack Query cache on\n * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass\n * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.\n */\nexport class DeFiPositionsControllerV2 extends BaseController<\n typeof controllerName,\n DeFiPositionsControllerV2State,\n DeFiPositionsControllerV2Messenger\n> {\n readonly #apiClient: ApiPlatformClient;\n\n readonly #isEnabled: () => boolean;\n\n readonly #getVsCurrency: () => string;\n\n /**\n * @param options - Constructor options.\n * @param options.messenger - The controller messenger.\n * @param options.apiClient - Accounts API client used to fetch balances/positions. Auth is handled by the client.\n * @param options.isEnabled - Returns whether fetching is enabled (default: () => false).\n * @param options.getVsCurrency - Returns the fiat currency for prices (default: () => 'usd').\n * @param options.state - Initial controller state.\n */\n constructor({\n messenger,\n apiClient,\n isEnabled,\n getVsCurrency,\n state,\n }: {\n messenger: DeFiPositionsControllerV2Messenger;\n apiClient: ApiPlatformClient;\n isEnabled: () => boolean;\n getVsCurrency: () => string;\n state?: Partial<DeFiPositionsControllerV2State>;\n }) {\n super({\n name: controllerName,\n metadata: controllerMetadata,\n messenger,\n state: {\n ...getDefaultDeFiPositionsControllerV2State(),\n ...state,\n },\n });\n\n this.#apiClient = apiClient;\n this.#isEnabled = isEnabled;\n this.#getVsCurrency = getVsCurrency;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Fetches DeFi positions for the selected account group. Each account key in\n * a ready response replaces that account's state (other accounts stay).\n * Accounts still indexing (`processingDefiPositions`) are skipped so prior\n * state is kept for them. No-ops when disabled or when the group has no\n * supported accounts. Caching / spam prevention is handled by the apiClient\n * TanStack Query cache (keyed by accounts + query options including\n * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.\n * pull-to-refresh).\n *\n * @param options - Optional fetch modifiers.\n * @param options.forceRefresh - When true, bypass the apiClient cache and\n * fetch immediately.\n */\n async fetchDeFiPositions(options?: {\n forceRefresh?: boolean;\n }): Promise<void> {\n if (!this.#isEnabled()) {\n return;\n }\n\n const selectedAccounts = this.messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n );\n\n const { networks, internalAccountIdByCaip } =\n buildDeFiBalancesQuery(selectedAccounts);\n\n if (internalAccountIdByCaip.size === 0 || networks.length === 0) {\n return;\n }\n\n const accountIds = [...internalAccountIdByCaip.keys()];\n const vsCurrency = this.#getVsCurrency().toLowerCase();\n\n try {\n const response =\n await this.#apiClient.accounts.fetchV6MultiAccountBalances(\n accountIds,\n {\n networks,\n includeDeFiBalances: true,\n forceFetchDeFiPositions: true,\n includePrices: true,\n vsCurrency,\n },\n {\n // staleTime: 0 makes TanStack treat the cache as stale for this call.\n ...(options?.forceRefresh ? { staleTime: 0 } : {}),\n },\n );\n\n // Skip accounts still indexing — their balances are not a valid snapshot.\n const readyAccounts = response.accounts.filter(\n (account) => !account.processingDefiPositions,\n );\n if (readyAccounts.length === 0) {\n return;\n }\n\n const positionsByAccount = groupDeFiPositionsV6(\n { ...response, accounts: readyAccounts },\n internalAccountIdByCaip,\n );\n\n // Last valid response wins per ready account; processing / other accounts\n // stay untouched.\n this.update((state) => {\n for (const [accountId, positions] of Object.entries(\n positionsByAccount,\n )) {\n state.allDeFiPositionsV2[accountId] = positions;\n }\n });\n } catch (error) {\n console.error('Failed to fetch DeFi positions', error);\n }\n }\n}\n"]}
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildDeFiBalancesQuery = exports.DEFI_SUPPORTED_NETWORKS = void 0;
4
+ const keyring_api_1 = require("@metamask/keyring-api");
5
+ const utils_1 = require("@metamask/utils");
6
+ /**
7
+ * Networks the DeFi balances (v6 multiaccount) endpoint supports.
8
+ * Cross-section of the supported chains from:
9
+ * https://developers.zerion.io/supported-blockchains
10
+ * https://accounts.api.cx.metamask.io/v2/supportedNetworks
11
+ */
12
+ exports.DEFI_SUPPORTED_NETWORKS = [
13
+ 'eip155:1',
14
+ 'eip155:137',
15
+ 'eip155:56',
16
+ 'eip155:1329',
17
+ 'eip155:43114',
18
+ 'eip155:59144',
19
+ 'eip155:8453',
20
+ 'eip155:10',
21
+ 'eip155:42161',
22
+ 'eip155:143',
23
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
24
+ 'eip155:999',
25
+ 'eip155:5042',
26
+ ];
27
+ const SOLANA_MAINNET_CAIP_CHAIN_ID = keyring_api_1.SolScope.Mainnet;
28
+ /**
29
+ * Builds an EVM CAIP-10 account ID that spans every EVM chain (reference `0`).
30
+ * Addresses are lowercased because EVM addresses are case-insensitive.
31
+ *
32
+ * @param address - The EVM account address.
33
+ * @returns The CAIP-10 account ID for the address.
34
+ */
35
+ function toEvmCaipAccountId(address) {
36
+ return (0, utils_1.toCaipAccountId)(utils_1.KnownCaipNamespace.Eip155, '0', address.toLowerCase());
37
+ }
38
+ /**
39
+ * Builds the account IDs and networks to request DeFi positions for, from the
40
+ * accounts in the selected account group.
41
+ *
42
+ * Picks the group's EVM account (queried across all supported EVM chains) and
43
+ * its Solana account (queried on supported Solana chains). Enabled-network
44
+ * filtering is intentionally omitted here: positions are stored per chain, so
45
+ * the client can filter by enabled networks when reading state.
46
+ *
47
+ * @param internalAccounts - Accounts belonging to the selected account group.
48
+ * @returns Networks and a CAIP→internal account ID map for the v6 multiaccount
49
+ * balances request. Map keys are the CAIP account IDs to query.
50
+ */
51
+ function buildDeFiBalancesQuery(internalAccounts) {
52
+ const evmNetworks = exports.DEFI_SUPPORTED_NETWORKS.filter((network) => network.startsWith(`${utils_1.KnownCaipNamespace.Eip155}:`));
53
+ const solanaNetworks = exports.DEFI_SUPPORTED_NETWORKS.filter((network) => network.startsWith(`${utils_1.KnownCaipNamespace.Solana}:`));
54
+ const networks = [];
55
+ const internalAccountIdByCaip = new Map();
56
+ const evmAccount = internalAccounts.find((account) => (0, keyring_api_1.isEvmAccountType)(account.type));
57
+ if (evmAccount && evmNetworks.length > 0) {
58
+ internalAccountIdByCaip.set(toEvmCaipAccountId(evmAccount.address), evmAccount.id);
59
+ networks.push(...evmNetworks);
60
+ }
61
+ const solanaAccount = internalAccounts.find((account) => account.type === keyring_api_1.SolAccountType.DataAccount);
62
+ if (solanaAccount && solanaNetworks.length > 0) {
63
+ const [, solanaReference] = SOLANA_MAINNET_CAIP_CHAIN_ID.split(':');
64
+ internalAccountIdByCaip.set((0, utils_1.toCaipAccountId)(utils_1.KnownCaipNamespace.Solana, solanaReference, solanaAccount.address), solanaAccount.id);
65
+ networks.push(...solanaNetworks);
66
+ }
67
+ return {
68
+ networks: [...new Set(networks)],
69
+ internalAccountIdByCaip,
70
+ };
71
+ }
72
+ exports.buildDeFiBalancesQuery = buildDeFiBalancesQuery;
73
+ //# sourceMappingURL=build-defi-balances-query.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-defi-balances-query.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/build-defi-balances-query.ts"],"names":[],"mappings":";;;AAAA,uDAI+B;AAG/B,2CAAsE;AAEtE;;;;;GAKG;AACU,QAAA,uBAAuB,GAA2B;IAC7D,UAAU;IACV,YAAY;IACZ,WAAW;IACX,aAAa;IACb,cAAc;IACd,cAAc;IACd,aAAa;IACb,WAAW;IACX,cAAc;IACd,YAAY;IACZ,yCAAyC;IACzC,YAAY;IACZ,aAAa;CACd,CAAC;AAEF,MAAM,4BAA4B,GAAgB,sBAAQ,CAAC,OAAO,CAAC;AAanE;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,OAAe;IACzC,OAAO,IAAA,uBAAe,EAAC,0BAAkB,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;AAChF,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,sBAAsB,CACpC,gBAAmC;IAEnC,MAAM,WAAW,GAAG,+BAAuB,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAC7D,OAAO,CAAC,UAAU,CAAC,GAAG,0BAAkB,CAAC,MAAM,GAAG,CAAC,CACpD,CAAC;IACF,MAAM,cAAc,GAAG,+BAAuB,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAChE,OAAO,CAAC,UAAU,CAAC,GAAG,0BAAkB,CAAC,MAAM,GAAG,CAAC,CACpD,CAAC;IAEF,MAAM,QAAQ,GAAkB,EAAE,CAAC;IACnC,MAAM,uBAAuB,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE1D,MAAM,UAAU,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CACnD,IAAA,8BAAgB,EAAC,OAAO,CAAC,IAAI,CAAC,CAC/B,CAAC;IACF,IAAI,UAAU,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzC,uBAAuB,CAAC,GAAG,CACzB,kBAAkB,CAAC,UAAU,CAAC,OAAO,CAAC,EACtC,UAAU,CAAC,EAAE,CACd,CAAC;QACF,QAAQ,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;IAChC,CAAC;IAED,MAAM,aAAa,GAAG,gBAAgB,CAAC,IAAI,CACzC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,4BAAc,CAAC,WAAW,CACzD,CAAC;IACF,IAAI,aAAa,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,CAAC,EAAE,eAAe,CAAC,GAAG,4BAA4B,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACpE,uBAAuB,CAAC,GAAG,CACzB,IAAA,uBAAe,EACb,0BAAkB,CAAC,MAAM,EACzB,eAAe,EACf,aAAa,CAAC,OAAO,CACtB,EACD,aAAa,CAAC,EAAE,CACjB,CAAC;QACF,QAAQ,CAAC,IAAI,CAAC,GAAG,cAAc,CAAC,CAAC;IACnC,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAkB;QACjD,uBAAuB;KACxB,CAAC;AACJ,CAAC;AA5CD,wDA4CC","sourcesContent":["import {\n isEvmAccountType,\n SolAccountType,\n SolScope,\n} from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { CaipAccountId, CaipChainId } from '@metamask/utils';\nimport { KnownCaipNamespace, toCaipAccountId } from '@metamask/utils';\n\n/**\n * Networks the DeFi balances (v6 multiaccount) endpoint supports.\n * Cross-section of the supported chains from:\n * https://developers.zerion.io/supported-blockchains\n * https://accounts.api.cx.metamask.io/v2/supportedNetworks\n */\nexport const DEFI_SUPPORTED_NETWORKS: readonly CaipChainId[] = [\n 'eip155:1',\n 'eip155:137',\n 'eip155:56',\n 'eip155:1329',\n 'eip155:43114',\n 'eip155:59144',\n 'eip155:8453',\n 'eip155:10',\n 'eip155:42161',\n 'eip155:143',\n 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',\n 'eip155:999',\n 'eip155:5042',\n];\n\nconst SOLANA_MAINNET_CAIP_CHAIN_ID: CaipChainId = SolScope.Mainnet;\n\nexport type DeFiBalancesQuery = {\n /** CAIP-2 networks to query, deduped across accounts. */\n networks: CaipChainId[];\n /**\n * Request CAIP-10 account IDs → internal MetaMask account IDs\n * (`InternalAccount.id`). EVM keys use the all-chains reference and a\n * lowercased address; Solana keys keep address case.\n */\n internalAccountIdByCaip: Map<string, string>;\n};\n\n/**\n * Builds an EVM CAIP-10 account ID that spans every EVM chain (reference `0`).\n * Addresses are lowercased because EVM addresses are case-insensitive.\n *\n * @param address - The EVM account address.\n * @returns The CAIP-10 account ID for the address.\n */\nfunction toEvmCaipAccountId(address: string): CaipAccountId {\n return toCaipAccountId(KnownCaipNamespace.Eip155, '0', address.toLowerCase());\n}\n\n/**\n * Builds the account IDs and networks to request DeFi positions for, from the\n * accounts in the selected account group.\n *\n * Picks the group's EVM account (queried across all supported EVM chains) and\n * its Solana account (queried on supported Solana chains). Enabled-network\n * filtering is intentionally omitted here: positions are stored per chain, so\n * the client can filter by enabled networks when reading state.\n *\n * @param internalAccounts - Accounts belonging to the selected account group.\n * @returns Networks and a CAIP→internal account ID map for the v6 multiaccount\n * balances request. Map keys are the CAIP account IDs to query.\n */\nexport function buildDeFiBalancesQuery(\n internalAccounts: InternalAccount[],\n): DeFiBalancesQuery {\n const evmNetworks = DEFI_SUPPORTED_NETWORKS.filter((network) =>\n network.startsWith(`${KnownCaipNamespace.Eip155}:`),\n );\n const solanaNetworks = DEFI_SUPPORTED_NETWORKS.filter((network) =>\n network.startsWith(`${KnownCaipNamespace.Solana}:`),\n );\n\n const networks: CaipChainId[] = [];\n const internalAccountIdByCaip = new Map<string, string>();\n\n const evmAccount = internalAccounts.find((account) =>\n isEvmAccountType(account.type),\n );\n if (evmAccount && evmNetworks.length > 0) {\n internalAccountIdByCaip.set(\n toEvmCaipAccountId(evmAccount.address),\n evmAccount.id,\n );\n networks.push(...evmNetworks);\n }\n\n const solanaAccount = internalAccounts.find(\n (account) => account.type === SolAccountType.DataAccount,\n );\n if (solanaAccount && solanaNetworks.length > 0) {\n const [, solanaReference] = SOLANA_MAINNET_CAIP_CHAIN_ID.split(':');\n internalAccountIdByCaip.set(\n toCaipAccountId(\n KnownCaipNamespace.Solana,\n solanaReference,\n solanaAccount.address,\n ),\n solanaAccount.id,\n );\n networks.push(...solanaNetworks);\n }\n\n return {\n networks: [...new Set(networks)] as CaipChainId[],\n internalAccountIdByCaip,\n };\n}\n"]}
@@ -0,0 +1,34 @@
1
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
2
+ import type { CaipChainId } from "@metamask/utils";
3
+ /**
4
+ * Networks the DeFi balances (v6 multiaccount) endpoint supports.
5
+ * Cross-section of the supported chains from:
6
+ * https://developers.zerion.io/supported-blockchains
7
+ * https://accounts.api.cx.metamask.io/v2/supportedNetworks
8
+ */
9
+ export declare const DEFI_SUPPORTED_NETWORKS: readonly CaipChainId[];
10
+ export type DeFiBalancesQuery = {
11
+ /** CAIP-2 networks to query, deduped across accounts. */
12
+ networks: CaipChainId[];
13
+ /**
14
+ * Request CAIP-10 account IDs → internal MetaMask account IDs
15
+ * (`InternalAccount.id`). EVM keys use the all-chains reference and a
16
+ * lowercased address; Solana keys keep address case.
17
+ */
18
+ internalAccountIdByCaip: Map<string, string>;
19
+ };
20
+ /**
21
+ * Builds the account IDs and networks to request DeFi positions for, from the
22
+ * accounts in the selected account group.
23
+ *
24
+ * Picks the group's EVM account (queried across all supported EVM chains) and
25
+ * its Solana account (queried on supported Solana chains). Enabled-network
26
+ * filtering is intentionally omitted here: positions are stored per chain, so
27
+ * the client can filter by enabled networks when reading state.
28
+ *
29
+ * @param internalAccounts - Accounts belonging to the selected account group.
30
+ * @returns Networks and a CAIP→internal account ID map for the v6 multiaccount
31
+ * balances request. Map keys are the CAIP account IDs to query.
32
+ */
33
+ export declare function buildDeFiBalancesQuery(internalAccounts: InternalAccount[]): DeFiBalancesQuery;
34
+ //# sourceMappingURL=build-defi-balances-query.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-defi-balances-query.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/build-defi-balances-query.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAiB,WAAW,EAAE,wBAAwB;AAGlE;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,EAAE,SAAS,WAAW,EAczD,CAAC;AAIF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,yDAAyD;IACzD,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB;;;;OAIG;IACH,uBAAuB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9C,CAAC;AAaF;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CACpC,gBAAgB,EAAE,eAAe,EAAE,GAClC,iBAAiB,CA0CnB"}