@metamask-previews/assets-controllers 110.1.0-preview-7b60eca → 110.1.0-preview-54ee0c6b9

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 (24) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.cjs.map +1 -1
  3. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.cts +18 -8
  4. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.cts.map +1 -1
  5. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.mts +18 -8
  6. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.mts.map +1 -1
  7. package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.mjs.map +1 -1
  8. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.cjs +124 -36
  9. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.cjs.map +1 -1
  10. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.cts +36 -9
  11. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.cts.map +1 -1
  12. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.mts +36 -9
  13. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.mts.map +1 -1
  14. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.mjs +123 -35
  15. package/dist/DeFiPositionsController/DeFiPositionsControllerV2.mjs.map +1 -1
  16. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.cjs +62 -0
  17. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.cjs.map +1 -0
  18. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.d.cts +27 -0
  19. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.d.cts.map +1 -0
  20. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.d.mts +27 -0
  21. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.d.mts.map +1 -0
  22. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.mjs +58 -0
  23. package/dist/DeFiPositionsController/defi-controller-v2-feature-flag.mjs.map +1 -0
  24. package/package.json +2 -1
package/CHANGELOG.md CHANGED
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
11
 
12
+ - `DeFiPositionsControllerV2` reads processing-poll `maxAttempts` / `pollInterval` from the `defiControllerV2` remote feature flag via `RemoteFeatureFlagController:getState` (clients must delegate that action); missing or invalid values fall back to 3 attempts and a 3000ms poll interval
13
+ - `DeFiPositionsControllerV2.fetchDeFiPositions` reports the number of fetch attempts to Sentry (via `messenger.captureException`, error name `DeFiPositionsV2FetchAttempts`) when positions become ready after more than one attempt, so processing-poll limits can be tuned from production data
14
+ - `DeFiPositionsControllerV2.fetchDeFiPositions` now polls while any selected account has `processingDefiPositions: true`, updating state only when every account is ready, invalidating the balances cache between attempts, sharing one in-flight promise per selected-account + `vsCurrency` key (so fast switches can join an earlier matching poll), and stopping on request failure or the max attempt limit ([#9711](https://github.com/MetaMask/core/pull/9711))
12
15
  - Bump `@metamask/network-controller` from `^35.0.0` to `^35.0.1` ([#9758](https://github.com/MetaMask/core/pull/9758))
13
16
  - Bump `@metamask/phishing-controller` from `^17.3.0` to `^17.3.1` ([#9746](https://github.com/MetaMask/core/pull/9746))
14
17
 
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { DeFiPositionsControllerV2 } from './DeFiPositionsControllerV2.js';\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 */\nexport type DeFiPositionsControllerV2FetchDeFiPositionsAction = {\n type: `DeFiPositionsControllerV2:fetchDeFiPositions`;\n handler: DeFiPositionsControllerV2['fetchDeFiPositions'];\n};\n\n/**\n * Union of all DeFiPositionsControllerV2 action types.\n */\nexport type DeFiPositionsControllerV2MethodActions =\n DeFiPositionsControllerV2FetchDeFiPositionsAction;\n"]}
1
+ {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { DeFiPositionsControllerV2 } from './DeFiPositionsControllerV2.js';\n\n/**\n * Fetches DeFi positions for the selected account group. State is updated only\n * when every selected account in the response is ready (none report\n * `processingDefiPositions`); each account key in that response replaces that\n * account's state (other accounts stay). While any account is still indexing,\n * prior state is kept and the method polls (invalidating the balances cache\n * between attempts) until all selected accounts are ready, the attempt limit\n * is reached, or a request fails. Concurrent calls for the same selected\n * accounts and `vsCurrency` share one in-flight promise; calls for a different\n * selection or fiat currency start a new fetch and leave prior polls running\n * so a later switch back can join them. When a successful ready response\n * required more than one attempt, reports the attempt count to Sentry via\n * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so\n * poll limits can be tuned. 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 on the\n * first attempt (e.g. pull-to-refresh).\n *\n * @param options - Optional fetch modifiers.\n * @param options.forceRefresh - When true, bypass the apiClient cache on the\n * first attempt and fetch immediately.\n * @returns Resolves when the fetch (and any processing polls) finish.\n */\nexport type DeFiPositionsControllerV2FetchDeFiPositionsAction = {\n type: `DeFiPositionsControllerV2:fetchDeFiPositions`;\n handler: DeFiPositionsControllerV2['fetchDeFiPositions'];\n};\n\n/**\n * Union of all DeFiPositionsControllerV2 action types.\n */\nexport type DeFiPositionsControllerV2MethodActions =\n DeFiPositionsControllerV2FetchDeFiPositionsAction;\n"]}
@@ -4,18 +4,28 @@
4
4
  */
5
5
  import type { DeFiPositionsControllerV2 } from "./DeFiPositionsControllerV2.cjs";
6
6
  /**
7
- * Fetches DeFi positions for the selected account group. Each account key in
8
- * a ready response replaces that account's state (other accounts stay).
9
- * Accounts still indexing (`processingDefiPositions`) are skipped so prior
10
- * state is kept for them. No-ops when disabled or when the group has no
7
+ * Fetches DeFi positions for the selected account group. State is updated only
8
+ * when every selected account in the response is ready (none report
9
+ * `processingDefiPositions`); each account key in that response replaces that
10
+ * account's state (other accounts stay). While any account is still indexing,
11
+ * prior state is kept and the method polls (invalidating the balances cache
12
+ * between attempts) until all selected accounts are ready, the attempt limit
13
+ * is reached, or a request fails. Concurrent calls for the same selected
14
+ * accounts and `vsCurrency` share one in-flight promise; calls for a different
15
+ * selection or fiat currency start a new fetch and leave prior polls running
16
+ * so a later switch back can join them. When a successful ready response
17
+ * required more than one attempt, reports the attempt count to Sentry via
18
+ * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so
19
+ * poll limits can be tuned. No-ops when disabled or when the group has no
11
20
  * supported accounts. Caching / spam prevention is handled by the apiClient
12
21
  * TanStack Query cache (keyed by accounts + query options including
13
- * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
14
- * pull-to-refresh).
22
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache on the
23
+ * first attempt (e.g. pull-to-refresh).
15
24
  *
16
25
  * @param options - Optional fetch modifiers.
17
- * @param options.forceRefresh - When true, bypass the apiClient cache and
18
- * fetch immediately.
26
+ * @param options.forceRefresh - When true, bypass the apiClient cache on the
27
+ * first attempt and fetch immediately.
28
+ * @returns Resolves when the fetch (and any processing polls) finish.
19
29
  */
20
30
  export type DeFiPositionsControllerV2FetchDeFiPositionsAction = {
21
31
  type: `DeFiPositionsControllerV2:fetchDeFiPositions`;
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,wCAAuC;AAEhF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,iDAAiD,GAAG;IAC9D,IAAI,EAAE,8CAA8C,CAAC;IACrD,OAAO,EAAE,yBAAyB,CAAC,oBAAoB,CAAC,CAAC;CAC1D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAChD,iDAAiD,CAAC"}
1
+ {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,wCAAuC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,iDAAiD,GAAG;IAC9D,IAAI,EAAE,8CAA8C,CAAC;IACrD,OAAO,EAAE,yBAAyB,CAAC,oBAAoB,CAAC,CAAC;CAC1D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAChD,iDAAiD,CAAC"}
@@ -4,18 +4,28 @@
4
4
  */
5
5
  import type { DeFiPositionsControllerV2 } from "./DeFiPositionsControllerV2.mjs";
6
6
  /**
7
- * Fetches DeFi positions for the selected account group. Each account key in
8
- * a ready response replaces that account's state (other accounts stay).
9
- * Accounts still indexing (`processingDefiPositions`) are skipped so prior
10
- * state is kept for them. No-ops when disabled or when the group has no
7
+ * Fetches DeFi positions for the selected account group. State is updated only
8
+ * when every selected account in the response is ready (none report
9
+ * `processingDefiPositions`); each account key in that response replaces that
10
+ * account's state (other accounts stay). While any account is still indexing,
11
+ * prior state is kept and the method polls (invalidating the balances cache
12
+ * between attempts) until all selected accounts are ready, the attempt limit
13
+ * is reached, or a request fails. Concurrent calls for the same selected
14
+ * accounts and `vsCurrency` share one in-flight promise; calls for a different
15
+ * selection or fiat currency start a new fetch and leave prior polls running
16
+ * so a later switch back can join them. When a successful ready response
17
+ * required more than one attempt, reports the attempt count to Sentry via
18
+ * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so
19
+ * poll limits can be tuned. No-ops when disabled or when the group has no
11
20
  * supported accounts. Caching / spam prevention is handled by the apiClient
12
21
  * TanStack Query cache (keyed by accounts + query options including
13
- * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
14
- * pull-to-refresh).
22
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache on the
23
+ * first attempt (e.g. pull-to-refresh).
15
24
  *
16
25
  * @param options - Optional fetch modifiers.
17
- * @param options.forceRefresh - When true, bypass the apiClient cache and
18
- * fetch immediately.
26
+ * @param options.forceRefresh - When true, bypass the apiClient cache on the
27
+ * first attempt and fetch immediately.
28
+ * @returns Resolves when the fetch (and any processing polls) finish.
19
29
  */
20
30
  export type DeFiPositionsControllerV2FetchDeFiPositionsAction = {
21
31
  type: `DeFiPositionsControllerV2:fetchDeFiPositions`;
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,wCAAuC;AAEhF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,iDAAiD,GAAG;IAC9D,IAAI,EAAE,8CAA8C,CAAC;IACrD,OAAO,EAAE,yBAAyB,CAAC,oBAAoB,CAAC,CAAC;CAC1D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAChD,iDAAiD,CAAC"}
1
+ {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,wCAAuC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,iDAAiD,GAAG;IAC9D,IAAI,EAAE,8CAA8C,CAAC;IACrD,OAAO,EAAE,yBAAyB,CAAC,oBAAoB,CAAC,CAAC;CAC1D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAChD,iDAAiD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { DeFiPositionsControllerV2 } from './DeFiPositionsControllerV2.js';\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 */\nexport type DeFiPositionsControllerV2FetchDeFiPositionsAction = {\n type: `DeFiPositionsControllerV2:fetchDeFiPositions`;\n handler: DeFiPositionsControllerV2['fetchDeFiPositions'];\n};\n\n/**\n * Union of all DeFiPositionsControllerV2 action types.\n */\nexport type DeFiPositionsControllerV2MethodActions =\n DeFiPositionsControllerV2FetchDeFiPositionsAction;\n"]}
1
+ {"version":3,"file":"DeFiPositionsControllerV2-method-action-types.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { DeFiPositionsControllerV2 } from './DeFiPositionsControllerV2.js';\n\n/**\n * Fetches DeFi positions for the selected account group. State is updated only\n * when every selected account in the response is ready (none report\n * `processingDefiPositions`); each account key in that response replaces that\n * account's state (other accounts stay). While any account is still indexing,\n * prior state is kept and the method polls (invalidating the balances cache\n * between attempts) until all selected accounts are ready, the attempt limit\n * is reached, or a request fails. Concurrent calls for the same selected\n * accounts and `vsCurrency` share one in-flight promise; calls for a different\n * selection or fiat currency start a new fetch and leave prior polls running\n * so a later switch back can join them. When a successful ready response\n * required more than one attempt, reports the attempt count to Sentry via\n * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so\n * poll limits can be tuned. 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 on the\n * first attempt (e.g. pull-to-refresh).\n *\n * @param options - Optional fetch modifiers.\n * @param options.forceRefresh - When true, bypass the apiClient cache on the\n * first attempt and fetch immediately.\n * @returns Resolves when the fetch (and any processing polls) finish.\n */\nexport type DeFiPositionsControllerV2FetchDeFiPositionsAction = {\n type: `DeFiPositionsControllerV2:fetchDeFiPositions`;\n handler: DeFiPositionsControllerV2['fetchDeFiPositions'];\n};\n\n/**\n * Union of all DeFiPositionsControllerV2 action types.\n */\nexport type DeFiPositionsControllerV2MethodActions =\n DeFiPositionsControllerV2FetchDeFiPositionsAction;\n"]}
@@ -10,14 +10,22 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
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
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _DeFiPositionsControllerV2_apiClient, _DeFiPositionsControllerV2_isEnabled, _DeFiPositionsControllerV2_getVsCurrency;
13
+ var _DeFiPositionsControllerV2_instances, _DeFiPositionsControllerV2_apiClient, _DeFiPositionsControllerV2_isEnabled, _DeFiPositionsControllerV2_getVsCurrency, _DeFiPositionsControllerV2_inFlightFetches, _DeFiPositionsControllerV2_fetchDeFiPositions;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.DeFiPositionsControllerV2 = exports.getDefaultDeFiPositionsControllerV2State = void 0;
16
16
  const base_controller_1 = require("@metamask/base-controller");
17
17
  const build_defi_balances_query_js_1 = require("./build-defi-balances-query.cjs");
18
+ const defi_controller_v2_feature_flag_js_1 = require("./defi-controller-v2-feature-flag.cjs");
18
19
  const group_defi_positions_v6_js_1 = require("./group-defi-positions-v6.cjs");
19
20
  const controllerName = 'DeFiPositionsControllerV2';
20
21
  const MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'];
22
+ /**
23
+ * @param ms - Milliseconds to wait.
24
+ * @returns A promise that resolves after `ms`.
25
+ */
26
+ function delay(ms) {
27
+ return new Promise((resolve) => setTimeout(resolve, ms));
28
+ }
21
29
  const controllerMetadata = {
22
30
  allDeFiPositionsV2: {
23
31
  includeInStateLogs: false,
@@ -40,6 +48,22 @@ exports.getDefaultDeFiPositionsControllerV2State = getDefaultDeFiPositionsContro
40
48
  * Deduplication and freshness are handled by the shared TanStack Query cache on
41
49
  * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
42
50
  * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
51
+ *
52
+ * When the API reports `processingDefiPositions` for any account, this
53
+ * controller polls until indexing finishes or the attempt limit is reached,
54
+ * and only then processes and writes state — mixed ready/processing responses
55
+ * leave prior state untouched. Concurrent calls for the same selected accounts
56
+ * and `vsCurrency` share one in-flight promise so the UI can treat the pending
57
+ * promise as a loading signal. Calls for a different selection or fiat currency
58
+ * start their own fetch and leave any prior poll running, so switching back can
59
+ * join an in-flight fetch for that group and currency.
60
+ *
61
+ * Processing-poll `maxAttempts` / `pollInterval` are read via
62
+ * {@link getProcessingPollConfig} from the `defiControllerV2` remote feature
63
+ * flag (`RemoteFeatureFlagController:getState`), falling back to
64
+ * `DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS` /
65
+ * `DEFAULT_PROCESSING_POLL_INTERVAL_MS` when unset or invalid. Clients
66
+ * must delegate that action to this controller's messenger.
43
67
  */
44
68
  class DeFiPositionsControllerV2 extends base_controller_1.BaseController {
45
69
  /**
@@ -60,69 +84,133 @@ class DeFiPositionsControllerV2 extends base_controller_1.BaseController {
60
84
  ...state,
61
85
  },
62
86
  });
87
+ _DeFiPositionsControllerV2_instances.add(this);
63
88
  _DeFiPositionsControllerV2_apiClient.set(this, void 0);
64
89
  _DeFiPositionsControllerV2_isEnabled.set(this, void 0);
65
90
  _DeFiPositionsControllerV2_getVsCurrency.set(this, void 0);
91
+ /**
92
+ * In-flight fetches keyed by selected DeFi-queryable account IDs plus
93
+ * `vsCurrency`. Concurrent callers for the same selection and currency share
94
+ * a promise; different selections or fiat currencies keep independent
95
+ * fetches so fast switching can join an earlier matching poll.
96
+ */
97
+ _DeFiPositionsControllerV2_inFlightFetches.set(this, new Map());
66
98
  __classPrivateFieldSet(this, _DeFiPositionsControllerV2_apiClient, apiClient, "f");
67
99
  __classPrivateFieldSet(this, _DeFiPositionsControllerV2_isEnabled, isEnabled, "f");
68
100
  __classPrivateFieldSet(this, _DeFiPositionsControllerV2_getVsCurrency, getVsCurrency, "f");
69
101
  this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
70
102
  }
71
103
  /**
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
104
+ * Fetches DeFi positions for the selected account group. State is updated only
105
+ * when every selected account in the response is ready (none report
106
+ * `processingDefiPositions`); each account key in that response replaces that
107
+ * account's state (other accounts stay). While any account is still indexing,
108
+ * prior state is kept and the method polls (invalidating the balances cache
109
+ * between attempts) until all selected accounts are ready, the attempt limit
110
+ * is reached, or a request fails. Concurrent calls for the same selected
111
+ * accounts and `vsCurrency` share one in-flight promise; calls for a different
112
+ * selection or fiat currency start a new fetch and leave prior polls running
113
+ * so a later switch back can join them. When a successful ready response
114
+ * required more than one attempt, reports the attempt count to Sentry via
115
+ * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so
116
+ * poll limits can be tuned. No-ops when disabled or when the group has no
76
117
  * supported accounts. Caching / spam prevention is handled by the apiClient
77
118
  * TanStack Query cache (keyed by accounts + query options including
78
- * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
79
- * pull-to-refresh).
119
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache on the
120
+ * first attempt (e.g. pull-to-refresh).
80
121
  *
81
122
  * @param options - Optional fetch modifiers.
82
- * @param options.forceRefresh - When true, bypass the apiClient cache and
83
- * fetch immediately.
123
+ * @param options.forceRefresh - When true, bypass the apiClient cache on the
124
+ * first attempt and fetch immediately.
125
+ * @returns Resolves when the fetch (and any processing polls) finish.
84
126
  */
85
127
  async fetchDeFiPositions(options) {
86
128
  if (!__classPrivateFieldGet(this, _DeFiPositionsControllerV2_isEnabled, "f").call(this)) {
87
129
  return;
88
130
  }
89
131
  const selectedAccounts = this.messenger.call('AccountTreeController:getAccountsFromSelectedAccountGroup');
90
- const { networks, internalAccountIdByCaip } = (0, build_defi_balances_query_js_1.buildDeFiBalancesQuery)(selectedAccounts);
91
- if (internalAccountIdByCaip.size === 0 || networks.length === 0) {
132
+ const query = (0, build_defi_balances_query_js_1.buildDeFiBalancesQuery)(selectedAccounts);
133
+ const vsCurrency = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_getVsCurrency, "f").call(this).toLowerCase();
134
+ // Include vsCurrency so a fiat change does not join a poll priced in the
135
+ // previous currency (TanStack also keys the balances cache on vsCurrency).
136
+ const inFlightKey = [
137
+ ...[...query.internalAccountIdByCaip.keys()].sort(),
138
+ vsCurrency,
139
+ ].join('\0');
140
+ const existing = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_inFlightFetches, "f").get(inFlightKey);
141
+ if (existing) {
142
+ await existing;
92
143
  return;
93
144
  }
94
- const accountIds = [...internalAccountIdByCaip.keys()];
95
- const vsCurrency = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_getVsCurrency, "f").call(this).toLowerCase();
145
+ // Same-key callers join this promise instead of replacing it, so cleanup
146
+ // can always delete without checking identity.
147
+ const fetchPromise = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_instances, "m", _DeFiPositionsControllerV2_fetchDeFiPositions).call(this, options, query, vsCurrency).finally(() => {
148
+ __classPrivateFieldGet(this, _DeFiPositionsControllerV2_inFlightFetches, "f").delete(inFlightKey);
149
+ });
150
+ __classPrivateFieldGet(this, _DeFiPositionsControllerV2_inFlightFetches, "f").set(inFlightKey, fetchPromise);
151
+ await fetchPromise;
152
+ }
153
+ }
154
+ exports.DeFiPositionsControllerV2 = DeFiPositionsControllerV2;
155
+ _DeFiPositionsControllerV2_apiClient = new WeakMap(), _DeFiPositionsControllerV2_isEnabled = new WeakMap(), _DeFiPositionsControllerV2_getVsCurrency = new WeakMap(), _DeFiPositionsControllerV2_inFlightFetches = new WeakMap(), _DeFiPositionsControllerV2_instances = new WeakSet(), _DeFiPositionsControllerV2_fetchDeFiPositions = async function _DeFiPositionsControllerV2_fetchDeFiPositions(options, { networks, internalAccountIdByCaip }, vsCurrency) {
156
+ if (internalAccountIdByCaip.size === 0 || networks.length === 0) {
157
+ return;
158
+ }
159
+ const accountIds = [...internalAccountIdByCaip.keys()];
160
+ const queryOptions = {
161
+ networks,
162
+ includeDeFiBalances: true,
163
+ forceFetchDeFiPositions: true,
164
+ includePrices: true,
165
+ vsCurrency,
166
+ };
167
+ // Resolve once per fetch so a mid-poll remote-flag change cannot stretch
168
+ // or shrink this poll sequence inconsistently.
169
+ const { maxAttempts, pollInterval } = (0, defi_controller_v2_feature_flag_js_1.getProcessingPollConfig)(this.messenger);
170
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
171
+ // First attempt respects forceRefresh; later polls always bypass cache
172
+ // so we do not spin on a stale processing snapshot.
173
+ const fetchOptions = {
174
+ ...(options?.forceRefresh || attempt > 0 ? { staleTime: 0 } : {}),
175
+ };
96
176
  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) {
177
+ const response = await __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.fetchV6MultiAccountBalances(accountIds, queryOptions, fetchOptions);
178
+ const stillProcessing = response.accounts.some((account) => account.processingDefiPositions);
179
+ // Only process and write when every account is ready so a partial
180
+ // response cannot clear or overwrite positions for accounts that are
181
+ // still indexing.
182
+ if (!stillProcessing) {
183
+ const positionsByAccount = (0, group_defi_positions_v6_js_1.groupDeFiPositionsV6)(response, internalAccountIdByCaip);
184
+ this.update((state) => {
185
+ for (const [accountId, positions] of Object.entries(positionsByAccount)) {
186
+ state.allDeFiPositionsV2[accountId] = positions;
187
+ }
188
+ });
189
+ // Report how many attempts were needed (only when polling was
190
+ // required) so we can tune DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS /
191
+ // INTERVAL from Sentry without flooding on first-try successes.
192
+ const attemptsTaken = attempt + 1;
193
+ if (attemptsTaken > 1) {
194
+ const sentryError = new Error(`DeFiPositionsControllerV2: positions ready after ${attemptsTaken} attempt(s)`);
195
+ sentryError.name = 'DeFiPositionsV2FetchAttempts';
196
+ this.messenger.captureException?.(sentryError);
197
+ }
110
198
  return;
111
199
  }
112
- const positionsByAccount = (0, group_defi_positions_v6_js_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
- }
200
+ const { queryKey } = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.getV6MultiAccountBalancesQueryOptions(accountIds, queryOptions, fetchOptions);
201
+ await __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.queryClient.invalidateQueries({
202
+ queryKey,
119
203
  });
204
+ const isLastAttempt = attempt >= maxAttempts - 1;
205
+ if (isLastAttempt) {
206
+ return;
207
+ }
208
+ await delay(pollInterval);
120
209
  }
121
210
  catch (error) {
122
211
  console.error('Failed to fetch DeFi positions', error);
212
+ return;
123
213
  }
124
214
  }
125
- }
126
- exports.DeFiPositionsControllerV2 = DeFiPositionsControllerV2;
127
- _DeFiPositionsControllerV2_apiClient = new WeakMap(), _DeFiPositionsControllerV2_isEnabled = new WeakMap(), _DeFiPositionsControllerV2_getVsCurrency = new WeakMap();
215
+ };
128
216
  //# sourceMappingURL=DeFiPositionsControllerV2.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2.cjs","sourceRoot":"","sources":["../../src/DeFiP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kB;YAClB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,KAAK,MAAM,CAAC,SAAS,EAAE,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;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.js';\nimport type { DeFiPositionsControllerV2MethodActions } from './DeFiPositionsControllerV2-method-action-types.js';\nimport type { DeFiPositionsByAccount } from './group-defi-positions-v6.js';\nimport { groupDeFiPositionsV6 } from './group-defi-positions-v6.js';\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"]}
1
+ {"version":3,"file":"DeFiPositionsControllerV2.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,+DAA2D;AAU3D,kFAAwE;AAExE,8FAA+E;AAG/E,8EAAoE;AAEpE,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD,MAAM,yBAAyB,GAAG,CAAC,oBAAoB,CAAU,CAAC;AAElE;;;GAGG;AACH,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAmBD,MAAM,kBAAkB,GAAkD;IACxE,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,KAAK;QACz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AC;oBACtB,MAAM,WAAW,GAAG,IAAI,KAAK,CAC3B,oDAAoD,aAAa,aAAa,CAC/E,CAAC;oBACF,WAAW,CAAC,IAAI,GAAG,8BAA8B,CAAC;oBAClD,IAAI,CAAC,SAAS,CAAC,gBAAgB,EAAE,CAAC,WAAW,CAAC,CAAC;gBACjD,CAAC;gBACD,OAAO;YACT,CAAC;YAED,MAAM,EAAE,QAAQ,EAAE,GAChB,uBAAA,IAAI,4CAAW,CAAC,QAAQ,CAAC,qCAAqC,CAC5D,UAAU,EACV,YAAY,EACZ,YAAY,CACb,CAAC;YACJ,MAAM,uBAAA,IAAI,4CAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,iBAAiB,CAAC;gBAC3D,QAAQ;aACT,CAAC,CAAC;YAEH,MAAM,aAAa,GAAG,OAAO,IAAI,WAAW,GAAG,CAAC,CAAC;YACjD,IAAI,aAAa,EAAE,CAAC;gBAClB,OAAO;YACT,CAAC;YAED,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC;QAC5B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;YACvD,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC","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';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\n\nimport { buildDeFiBalancesQuery } from './build-defi-balances-query.js';\nimport type { DeFiBalancesQuery } from './build-defi-balances-query.js';\nimport { getProcessingPollConfig } from './defi-controller-v2-feature-flag.js';\nimport type { DeFiPositionsControllerV2MethodActions } from './DeFiPositionsControllerV2-method-action-types.js';\nimport type { DeFiPositionsByAccount } from './group-defi-positions-v6.js';\nimport { groupDeFiPositionsV6 } from './group-defi-positions-v6.js';\n\nconst controllerName = 'DeFiPositionsControllerV2';\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'] as const;\n\n/**\n * @param ms - Milliseconds to wait.\n * @returns A promise that resolves after `ms`.\n */\nfunction delay(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\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 | RemoteFeatureFlagControllerGetStateAction;\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 *\n * When the API reports `processingDefiPositions` for any account, this\n * controller polls until indexing finishes or the attempt limit is reached,\n * and only then processes and writes state — mixed ready/processing responses\n * leave prior state untouched. Concurrent calls for the same selected accounts\n * and `vsCurrency` share one in-flight promise so the UI can treat the pending\n * promise as a loading signal. Calls for a different selection or fiat currency\n * start their own fetch and leave any prior poll running, so switching back can\n * join an in-flight fetch for that group and currency.\n *\n * Processing-poll `maxAttempts` / `pollInterval` are read via\n * {@link getProcessingPollConfig} from the `defiControllerV2` remote feature\n * flag (`RemoteFeatureFlagController:getState`), falling back to\n * `DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS` /\n * `DEFAULT_PROCESSING_POLL_INTERVAL_MS` when unset or invalid. Clients\n * must delegate that action to this controller's messenger.\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 * In-flight fetches keyed by selected DeFi-queryable account IDs plus\n * `vsCurrency`. Concurrent callers for the same selection and currency share\n * a promise; different selections or fiat currencies keep independent\n * fetches so fast switching can join an earlier matching poll.\n */\n readonly #inFlightFetches = new Map<string, Promise<void>>();\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. State is updated only\n * when every selected account in the response is ready (none report\n * `processingDefiPositions`); each account key in that response replaces that\n * account's state (other accounts stay). While any account is still indexing,\n * prior state is kept and the method polls (invalidating the balances cache\n * between attempts) until all selected accounts are ready, the attempt limit\n * is reached, or a request fails. Concurrent calls for the same selected\n * accounts and `vsCurrency` share one in-flight promise; calls for a different\n * selection or fiat currency start a new fetch and leave prior polls running\n * so a later switch back can join them. When a successful ready response\n * required more than one attempt, reports the attempt count to Sentry via\n * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so\n * poll limits can be tuned. 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 on the\n * first attempt (e.g. pull-to-refresh).\n *\n * @param options - Optional fetch modifiers.\n * @param options.forceRefresh - When true, bypass the apiClient cache on the\n * first attempt and fetch immediately.\n * @returns Resolves when the fetch (and any processing polls) finish.\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 const query = buildDeFiBalancesQuery(selectedAccounts);\n const vsCurrency = this.#getVsCurrency().toLowerCase();\n // Include vsCurrency so a fiat change does not join a poll priced in the\n // previous currency (TanStack also keys the balances cache on vsCurrency).\n const inFlightKey = [\n ...[...query.internalAccountIdByCaip.keys()].sort(),\n vsCurrency,\n ].join('\\0');\n\n const existing = this.#inFlightFetches.get(inFlightKey);\n if (existing) {\n await existing;\n return;\n }\n\n // Same-key callers join this promise instead of replacing it, so cleanup\n // can always delete without checking identity.\n const fetchPromise = this.#fetchDeFiPositions(\n options,\n query,\n vsCurrency,\n ).finally(() => {\n this.#inFlightFetches.delete(inFlightKey);\n });\n this.#inFlightFetches.set(inFlightKey, fetchPromise);\n\n await fetchPromise;\n }\n\n async #fetchDeFiPositions(\n options: { forceRefresh?: boolean } | undefined,\n { networks, internalAccountIdByCaip }: DeFiBalancesQuery,\n vsCurrency: string,\n ): Promise<void> {\n if (internalAccountIdByCaip.size === 0 || networks.length === 0) {\n return;\n }\n\n const accountIds = [...internalAccountIdByCaip.keys()];\n const queryOptions = {\n networks,\n includeDeFiBalances: true,\n forceFetchDeFiPositions: true,\n includePrices: true,\n vsCurrency,\n };\n\n // Resolve once per fetch so a mid-poll remote-flag change cannot stretch\n // or shrink this poll sequence inconsistently.\n const { maxAttempts, pollInterval } = getProcessingPollConfig(\n this.messenger,\n );\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n // First attempt respects forceRefresh; later polls always bypass cache\n // so we do not spin on a stale processing snapshot.\n const fetchOptions = {\n ...(options?.forceRefresh || attempt > 0 ? { staleTime: 0 } : {}),\n };\n\n try {\n const response =\n await this.#apiClient.accounts.fetchV6MultiAccountBalances(\n accountIds,\n queryOptions,\n fetchOptions,\n );\n\n const stillProcessing = response.accounts.some(\n (account) => account.processingDefiPositions,\n );\n\n // Only process and write when every account is ready so a partial\n // response cannot clear or overwrite positions for accounts that are\n // still indexing.\n if (!stillProcessing) {\n const positionsByAccount = groupDeFiPositionsV6(\n response,\n internalAccountIdByCaip,\n );\n\n this.update((state) => {\n for (const [accountId, positions] of Object.entries(\n positionsByAccount,\n )) {\n state.allDeFiPositionsV2[accountId] = positions;\n }\n });\n\n // Report how many attempts were needed (only when polling was\n // required) so we can tune DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS /\n // INTERVAL from Sentry without flooding on first-try successes.\n const attemptsTaken = attempt + 1;\n if (attemptsTaken > 1) {\n const sentryError = new Error(\n `DeFiPositionsControllerV2: positions ready after ${attemptsTaken} attempt(s)`,\n );\n sentryError.name = 'DeFiPositionsV2FetchAttempts';\n this.messenger.captureException?.(sentryError);\n }\n return;\n }\n\n const { queryKey } =\n this.#apiClient.accounts.getV6MultiAccountBalancesQueryOptions(\n accountIds,\n queryOptions,\n fetchOptions,\n );\n await this.#apiClient.accounts.queryClient.invalidateQueries({\n queryKey,\n });\n\n const isLastAttempt = attempt >= maxAttempts - 1;\n if (isLastAttempt) {\n return;\n }\n\n await delay(pollInterval);\n } catch (error) {\n console.error('Failed to fetch DeFi positions', error);\n return;\n }\n }\n }\n}\n"]}
@@ -3,6 +3,7 @@ import { BaseController } from "@metamask/base-controller";
3
3
  import type { ControllerGetStateAction, ControllerStateChangedEvent } from "@metamask/base-controller";
4
4
  import type { ApiPlatformClient } from "@metamask/core-backend";
5
5
  import type { Messenger } from "@metamask/messenger";
6
+ import type { RemoteFeatureFlagControllerGetStateAction } from "@metamask/remote-feature-flag-controller";
6
7
  import type { DeFiPositionsControllerV2MethodActions } from "./DeFiPositionsControllerV2-method-action-types.cjs";
7
8
  import type { DeFiPositionsByAccount } from "./group-defi-positions-v6.cjs";
8
9
  declare const controllerName = "DeFiPositionsControllerV2";
@@ -30,7 +31,7 @@ export type DeFiPositionsControllerV2Events = DeFiPositionsControllerV2StateChan
30
31
  /**
31
32
  * The external actions available to the {@link DeFiPositionsControllerV2}.
32
33
  */
33
- export type AllowedActions = AccountTreeControllerGetAccountsFromSelectedAccountGroupAction;
34
+ export type AllowedActions = AccountTreeControllerGetAccountsFromSelectedAccountGroupAction | RemoteFeatureFlagControllerGetStateAction;
34
35
  /**
35
36
  * The external events available to the {@link DeFiPositionsControllerV2}.
36
37
  *
@@ -49,6 +50,22 @@ export type DeFiPositionsControllerV2Messenger = Messenger<typeof controllerName
49
50
  * Deduplication and freshness are handled by the shared TanStack Query cache on
50
51
  * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
51
52
  * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
53
+ *
54
+ * When the API reports `processingDefiPositions` for any account, this
55
+ * controller polls until indexing finishes or the attempt limit is reached,
56
+ * and only then processes and writes state — mixed ready/processing responses
57
+ * leave prior state untouched. Concurrent calls for the same selected accounts
58
+ * and `vsCurrency` share one in-flight promise so the UI can treat the pending
59
+ * promise as a loading signal. Calls for a different selection or fiat currency
60
+ * start their own fetch and leave any prior poll running, so switching back can
61
+ * join an in-flight fetch for that group and currency.
62
+ *
63
+ * Processing-poll `maxAttempts` / `pollInterval` are read via
64
+ * {@link getProcessingPollConfig} from the `defiControllerV2` remote feature
65
+ * flag (`RemoteFeatureFlagController:getState`), falling back to
66
+ * `DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS` /
67
+ * `DEFAULT_PROCESSING_POLL_INTERVAL_MS` when unset or invalid. Clients
68
+ * must delegate that action to this controller's messenger.
52
69
  */
53
70
  export declare class DeFiPositionsControllerV2 extends BaseController<typeof controllerName, DeFiPositionsControllerV2State, DeFiPositionsControllerV2Messenger> {
54
71
  #private;
@@ -68,18 +85,28 @@ export declare class DeFiPositionsControllerV2 extends BaseController<typeof con
68
85
  state?: Partial<DeFiPositionsControllerV2State>;
69
86
  });
70
87
  /**
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
88
+ * Fetches DeFi positions for the selected account group. State is updated only
89
+ * when every selected account in the response is ready (none report
90
+ * `processingDefiPositions`); each account key in that response replaces that
91
+ * account's state (other accounts stay). While any account is still indexing,
92
+ * prior state is kept and the method polls (invalidating the balances cache
93
+ * between attempts) until all selected accounts are ready, the attempt limit
94
+ * is reached, or a request fails. Concurrent calls for the same selected
95
+ * accounts and `vsCurrency` share one in-flight promise; calls for a different
96
+ * selection or fiat currency start a new fetch and leave prior polls running
97
+ * so a later switch back can join them. When a successful ready response
98
+ * required more than one attempt, reports the attempt count to Sentry via
99
+ * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so
100
+ * poll limits can be tuned. No-ops when disabled or when the group has no
75
101
  * supported accounts. Caching / spam prevention is handled by the apiClient
76
102
  * TanStack Query cache (keyed by accounts + query options including
77
- * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
78
- * pull-to-refresh).
103
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache on the
104
+ * first attempt (e.g. pull-to-refresh).
79
105
  *
80
106
  * @param options - Optional fetch modifiers.
81
- * @param options.forceRefresh - When true, bypass the apiClient cache and
82
- * fetch immediately.
107
+ * @param options.forceRefresh - When true, bypass the apiClient cache on the
108
+ * first attempt and fetch immediately.
109
+ * @returns Resolves when the fetch (and any processing polls) finish.
83
110
  */
84
111
  fetchDeFiPositions(options?: {
85
112
  forceRefresh?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2.d.cts","sourceRoot":"","sources":["../../src/DeFiPosition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,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"}
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+B;AAChE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,yCAAyC,EAAE,iDAAiD;AAK1G,OAAO,KAAK,EAAE,sCAAsC,EAAE,4DAA2D;AACjH,OAAO,KAAK,EAAE,sBAAsB,EAAE,sCAAqC;AAG3E,QAAA,MAAM,cAAc,8BAA8B,CAAC;AAYnD,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,GACtB,8DAA8D,GAC9D,yCAAyC,CAAC;AAE9C;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAeC;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,kBAAkB,CAAC,OAAO,CAAC,EAAE;QACjC,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB,GAAG,OAAO,CAAC,IAAI,CAAC;CAqIlB"}
@@ -3,6 +3,7 @@ import { BaseController } from "@metamask/base-controller";
3
3
  import type { ControllerGetStateAction, ControllerStateChangedEvent } from "@metamask/base-controller";
4
4
  import type { ApiPlatformClient } from "@metamask/core-backend";
5
5
  import type { Messenger } from "@metamask/messenger";
6
+ import type { RemoteFeatureFlagControllerGetStateAction } from "@metamask/remote-feature-flag-controller";
6
7
  import type { DeFiPositionsControllerV2MethodActions } from "./DeFiPositionsControllerV2-method-action-types.mjs";
7
8
  import type { DeFiPositionsByAccount } from "./group-defi-positions-v6.mjs";
8
9
  declare const controllerName = "DeFiPositionsControllerV2";
@@ -30,7 +31,7 @@ export type DeFiPositionsControllerV2Events = DeFiPositionsControllerV2StateChan
30
31
  /**
31
32
  * The external actions available to the {@link DeFiPositionsControllerV2}.
32
33
  */
33
- export type AllowedActions = AccountTreeControllerGetAccountsFromSelectedAccountGroupAction;
34
+ export type AllowedActions = AccountTreeControllerGetAccountsFromSelectedAccountGroupAction | RemoteFeatureFlagControllerGetStateAction;
34
35
  /**
35
36
  * The external events available to the {@link DeFiPositionsControllerV2}.
36
37
  *
@@ -49,6 +50,22 @@ export type DeFiPositionsControllerV2Messenger = Messenger<typeof controllerName
49
50
  * Deduplication and freshness are handled by the shared TanStack Query cache on
50
51
  * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
51
52
  * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
53
+ *
54
+ * When the API reports `processingDefiPositions` for any account, this
55
+ * controller polls until indexing finishes or the attempt limit is reached,
56
+ * and only then processes and writes state — mixed ready/processing responses
57
+ * leave prior state untouched. Concurrent calls for the same selected accounts
58
+ * and `vsCurrency` share one in-flight promise so the UI can treat the pending
59
+ * promise as a loading signal. Calls for a different selection or fiat currency
60
+ * start their own fetch and leave any prior poll running, so switching back can
61
+ * join an in-flight fetch for that group and currency.
62
+ *
63
+ * Processing-poll `maxAttempts` / `pollInterval` are read via
64
+ * {@link getProcessingPollConfig} from the `defiControllerV2` remote feature
65
+ * flag (`RemoteFeatureFlagController:getState`), falling back to
66
+ * `DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS` /
67
+ * `DEFAULT_PROCESSING_POLL_INTERVAL_MS` when unset or invalid. Clients
68
+ * must delegate that action to this controller's messenger.
52
69
  */
53
70
  export declare class DeFiPositionsControllerV2 extends BaseController<typeof controllerName, DeFiPositionsControllerV2State, DeFiPositionsControllerV2Messenger> {
54
71
  #private;
@@ -68,18 +85,28 @@ export declare class DeFiPositionsControllerV2 extends BaseController<typeof con
68
85
  state?: Partial<DeFiPositionsControllerV2State>;
69
86
  });
70
87
  /**
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
88
+ * Fetches DeFi positions for the selected account group. State is updated only
89
+ * when every selected account in the response is ready (none report
90
+ * `processingDefiPositions`); each account key in that response replaces that
91
+ * account's state (other accounts stay). While any account is still indexing,
92
+ * prior state is kept and the method polls (invalidating the balances cache
93
+ * between attempts) until all selected accounts are ready, the attempt limit
94
+ * is reached, or a request fails. Concurrent calls for the same selected
95
+ * accounts and `vsCurrency` share one in-flight promise; calls for a different
96
+ * selection or fiat currency start a new fetch and leave prior polls running
97
+ * so a later switch back can join them. When a successful ready response
98
+ * required more than one attempt, reports the attempt count to Sentry via
99
+ * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so
100
+ * poll limits can be tuned. No-ops when disabled or when the group has no
75
101
  * supported accounts. Caching / spam prevention is handled by the apiClient
76
102
  * TanStack Query cache (keyed by accounts + query options including
77
- * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
78
- * pull-to-refresh).
103
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache on the
104
+ * first attempt (e.g. pull-to-refresh).
79
105
  *
80
106
  * @param options - Optional fetch modifiers.
81
- * @param options.forceRefresh - When true, bypass the apiClient cache and
82
- * fetch immediately.
107
+ * @param options.forceRefresh - When true, bypass the apiClient cache on the
108
+ * first attempt and fetch immediately.
109
+ * @returns Resolves when the fetch (and any processing polls) finish.
83
110
  */
84
111
  fetchDeFiPositions(options?: {
85
112
  forceRefresh?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"DeFiPositionsControllerV2.d.mts","sourceRoot":"","sources":["../../src/DeFiPositions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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"}
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;AACrD,OAAO,KAAK,EAAE,yCAAyC,EAAE,iDAAiD;AAK1G,OAAO,KAAK,EAAE,sCAAsC,EAAE,4DAA2D;AACjH,OAAO,KAAK,EAAE,sBAAsB,EAAE,sCAAqC;AAG3E,QAAA,MAAM,cAAc,8BAA8B,CAAC;AAYnD,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,GACtB,8DAA8D,GAC9D,yCAAyC,CAAC;AAE9C;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAeC;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,kBAAkB,CAAC,OAAO,CAAC,EAAE;QACjC,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB,GAAG,OAAO,CAAC,IAAI,CAAC;CAqIlB"}
@@ -9,12 +9,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _DeFiPositionsControllerV2_apiClient, _DeFiPositionsControllerV2_isEnabled, _DeFiPositionsControllerV2_getVsCurrency;
12
+ var _DeFiPositionsControllerV2_instances, _DeFiPositionsControllerV2_apiClient, _DeFiPositionsControllerV2_isEnabled, _DeFiPositionsControllerV2_getVsCurrency, _DeFiPositionsControllerV2_inFlightFetches, _DeFiPositionsControllerV2_fetchDeFiPositions;
13
13
  import { BaseController } from "@metamask/base-controller";
14
14
  import { buildDeFiBalancesQuery } from "./build-defi-balances-query.mjs";
15
+ import { getProcessingPollConfig } from "./defi-controller-v2-feature-flag.mjs";
15
16
  import { groupDeFiPositionsV6 } from "./group-defi-positions-v6.mjs";
16
17
  const controllerName = 'DeFiPositionsControllerV2';
17
18
  const MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'];
19
+ /**
20
+ * @param ms - Milliseconds to wait.
21
+ * @returns A promise that resolves after `ms`.
22
+ */
23
+ function delay(ms) {
24
+ return new Promise((resolve) => setTimeout(resolve, ms));
25
+ }
18
26
  const controllerMetadata = {
19
27
  allDeFiPositionsV2: {
20
28
  includeInStateLogs: false,
@@ -36,6 +44,22 @@ export const getDefaultDeFiPositionsControllerV2State = () => {
36
44
  * Deduplication and freshness are handled by the shared TanStack Query cache on
37
45
  * {@link ApiPlatformClient} (balances default `staleTime` is 1 minute). Pass
38
46
  * `{ forceRefresh: true }` to bypass that cache for pull-to-refresh.
47
+ *
48
+ * When the API reports `processingDefiPositions` for any account, this
49
+ * controller polls until indexing finishes or the attempt limit is reached,
50
+ * and only then processes and writes state — mixed ready/processing responses
51
+ * leave prior state untouched. Concurrent calls for the same selected accounts
52
+ * and `vsCurrency` share one in-flight promise so the UI can treat the pending
53
+ * promise as a loading signal. Calls for a different selection or fiat currency
54
+ * start their own fetch and leave any prior poll running, so switching back can
55
+ * join an in-flight fetch for that group and currency.
56
+ *
57
+ * Processing-poll `maxAttempts` / `pollInterval` are read via
58
+ * {@link getProcessingPollConfig} from the `defiControllerV2` remote feature
59
+ * flag (`RemoteFeatureFlagController:getState`), falling back to
60
+ * `DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS` /
61
+ * `DEFAULT_PROCESSING_POLL_INTERVAL_MS` when unset or invalid. Clients
62
+ * must delegate that action to this controller's messenger.
39
63
  */
40
64
  export class DeFiPositionsControllerV2 extends BaseController {
41
65
  /**
@@ -56,68 +80,132 @@ export class DeFiPositionsControllerV2 extends BaseController {
56
80
  ...state,
57
81
  },
58
82
  });
83
+ _DeFiPositionsControllerV2_instances.add(this);
59
84
  _DeFiPositionsControllerV2_apiClient.set(this, void 0);
60
85
  _DeFiPositionsControllerV2_isEnabled.set(this, void 0);
61
86
  _DeFiPositionsControllerV2_getVsCurrency.set(this, void 0);
87
+ /**
88
+ * In-flight fetches keyed by selected DeFi-queryable account IDs plus
89
+ * `vsCurrency`. Concurrent callers for the same selection and currency share
90
+ * a promise; different selections or fiat currencies keep independent
91
+ * fetches so fast switching can join an earlier matching poll.
92
+ */
93
+ _DeFiPositionsControllerV2_inFlightFetches.set(this, new Map());
62
94
  __classPrivateFieldSet(this, _DeFiPositionsControllerV2_apiClient, apiClient, "f");
63
95
  __classPrivateFieldSet(this, _DeFiPositionsControllerV2_isEnabled, isEnabled, "f");
64
96
  __classPrivateFieldSet(this, _DeFiPositionsControllerV2_getVsCurrency, getVsCurrency, "f");
65
97
  this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
66
98
  }
67
99
  /**
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
100
+ * Fetches DeFi positions for the selected account group. State is updated only
101
+ * when every selected account in the response is ready (none report
102
+ * `processingDefiPositions`); each account key in that response replaces that
103
+ * account's state (other accounts stay). While any account is still indexing,
104
+ * prior state is kept and the method polls (invalidating the balances cache
105
+ * between attempts) until all selected accounts are ready, the attempt limit
106
+ * is reached, or a request fails. Concurrent calls for the same selected
107
+ * accounts and `vsCurrency` share one in-flight promise; calls for a different
108
+ * selection or fiat currency start a new fetch and leave prior polls running
109
+ * so a later switch back can join them. When a successful ready response
110
+ * required more than one attempt, reports the attempt count to Sentry via
111
+ * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so
112
+ * poll limits can be tuned. No-ops when disabled or when the group has no
72
113
  * supported accounts. Caching / spam prevention is handled by the apiClient
73
114
  * TanStack Query cache (keyed by accounts + query options including
74
- * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache (e.g.
75
- * pull-to-refresh).
115
+ * `vsCurrency`). Pass `{ forceRefresh: true }` to bypass the cache on the
116
+ * first attempt (e.g. pull-to-refresh).
76
117
  *
77
118
  * @param options - Optional fetch modifiers.
78
- * @param options.forceRefresh - When true, bypass the apiClient cache and
79
- * fetch immediately.
119
+ * @param options.forceRefresh - When true, bypass the apiClient cache on the
120
+ * first attempt and fetch immediately.
121
+ * @returns Resolves when the fetch (and any processing polls) finish.
80
122
  */
81
123
  async fetchDeFiPositions(options) {
82
124
  if (!__classPrivateFieldGet(this, _DeFiPositionsControllerV2_isEnabled, "f").call(this)) {
83
125
  return;
84
126
  }
85
127
  const selectedAccounts = this.messenger.call('AccountTreeController:getAccountsFromSelectedAccountGroup');
86
- const { networks, internalAccountIdByCaip } = buildDeFiBalancesQuery(selectedAccounts);
87
- if (internalAccountIdByCaip.size === 0 || networks.length === 0) {
128
+ const query = buildDeFiBalancesQuery(selectedAccounts);
129
+ const vsCurrency = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_getVsCurrency, "f").call(this).toLowerCase();
130
+ // Include vsCurrency so a fiat change does not join a poll priced in the
131
+ // previous currency (TanStack also keys the balances cache on vsCurrency).
132
+ const inFlightKey = [
133
+ ...[...query.internalAccountIdByCaip.keys()].sort(),
134
+ vsCurrency,
135
+ ].join('\0');
136
+ const existing = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_inFlightFetches, "f").get(inFlightKey);
137
+ if (existing) {
138
+ await existing;
88
139
  return;
89
140
  }
90
- const accountIds = [...internalAccountIdByCaip.keys()];
91
- const vsCurrency = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_getVsCurrency, "f").call(this).toLowerCase();
141
+ // Same-key callers join this promise instead of replacing it, so cleanup
142
+ // can always delete without checking identity.
143
+ const fetchPromise = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_instances, "m", _DeFiPositionsControllerV2_fetchDeFiPositions).call(this, options, query, vsCurrency).finally(() => {
144
+ __classPrivateFieldGet(this, _DeFiPositionsControllerV2_inFlightFetches, "f").delete(inFlightKey);
145
+ });
146
+ __classPrivateFieldGet(this, _DeFiPositionsControllerV2_inFlightFetches, "f").set(inFlightKey, fetchPromise);
147
+ await fetchPromise;
148
+ }
149
+ }
150
+ _DeFiPositionsControllerV2_apiClient = new WeakMap(), _DeFiPositionsControllerV2_isEnabled = new WeakMap(), _DeFiPositionsControllerV2_getVsCurrency = new WeakMap(), _DeFiPositionsControllerV2_inFlightFetches = new WeakMap(), _DeFiPositionsControllerV2_instances = new WeakSet(), _DeFiPositionsControllerV2_fetchDeFiPositions = async function _DeFiPositionsControllerV2_fetchDeFiPositions(options, { networks, internalAccountIdByCaip }, vsCurrency) {
151
+ if (internalAccountIdByCaip.size === 0 || networks.length === 0) {
152
+ return;
153
+ }
154
+ const accountIds = [...internalAccountIdByCaip.keys()];
155
+ const queryOptions = {
156
+ networks,
157
+ includeDeFiBalances: true,
158
+ forceFetchDeFiPositions: true,
159
+ includePrices: true,
160
+ vsCurrency,
161
+ };
162
+ // Resolve once per fetch so a mid-poll remote-flag change cannot stretch
163
+ // or shrink this poll sequence inconsistently.
164
+ const { maxAttempts, pollInterval } = getProcessingPollConfig(this.messenger);
165
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
166
+ // First attempt respects forceRefresh; later polls always bypass cache
167
+ // so we do not spin on a stale processing snapshot.
168
+ const fetchOptions = {
169
+ ...(options?.forceRefresh || attempt > 0 ? { staleTime: 0 } : {}),
170
+ };
92
171
  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) {
172
+ const response = await __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.fetchV6MultiAccountBalances(accountIds, queryOptions, fetchOptions);
173
+ const stillProcessing = response.accounts.some((account) => account.processingDefiPositions);
174
+ // Only process and write when every account is ready so a partial
175
+ // response cannot clear or overwrite positions for accounts that are
176
+ // still indexing.
177
+ if (!stillProcessing) {
178
+ const positionsByAccount = groupDeFiPositionsV6(response, internalAccountIdByCaip);
179
+ this.update((state) => {
180
+ for (const [accountId, positions] of Object.entries(positionsByAccount)) {
181
+ state.allDeFiPositionsV2[accountId] = positions;
182
+ }
183
+ });
184
+ // Report how many attempts were needed (only when polling was
185
+ // required) so we can tune DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS /
186
+ // INTERVAL from Sentry without flooding on first-try successes.
187
+ const attemptsTaken = attempt + 1;
188
+ if (attemptsTaken > 1) {
189
+ const sentryError = new Error(`DeFiPositionsControllerV2: positions ready after ${attemptsTaken} attempt(s)`);
190
+ sentryError.name = 'DeFiPositionsV2FetchAttempts';
191
+ this.messenger.captureException?.(sentryError);
192
+ }
106
193
  return;
107
194
  }
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
- }
195
+ const { queryKey } = __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.getV6MultiAccountBalancesQueryOptions(accountIds, queryOptions, fetchOptions);
196
+ await __classPrivateFieldGet(this, _DeFiPositionsControllerV2_apiClient, "f").accounts.queryClient.invalidateQueries({
197
+ queryKey,
115
198
  });
199
+ const isLastAttempt = attempt >= maxAttempts - 1;
200
+ if (isLastAttempt) {
201
+ return;
202
+ }
203
+ await delay(pollInterval);
116
204
  }
117
205
  catch (error) {
118
206
  console.error('Failed to fetch DeFi positions', error);
207
+ return;
119
208
  }
120
209
  }
121
- }
122
- _DeFiPositionsControllerV2_apiClient = new WeakMap(), _DeFiPositionsControllerV2_isEnabled = new WeakMap(), _DeFiPositionsControllerV2_getVsCurrency = new WeakMap();
210
+ };
123
211
  //# sourceMappingURL=DeFiPositionsControllerV2.mjs.map
@@ -1 +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,wCAAuC;AAGxE,OAAO,EAAE,oBAAoB,EAAE,sCAAqC;AAEpE,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD,MAAM,yBAAyB,GAAG,CAAC,oBAAoB,CAAU,CAAC;AAmBlE,MAAM,kBAAkB,GAAkD;IACxE,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,wCAAwC,GACnD,GAAmC,EAAE;IACnC,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC,CAAC;AA0CJ;;;;;;;;GAQG;AACH,MAAM,OAA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,CAAC,QAAQ,CAAC,2BAA2B,CACxD,UAAU,EACV;gBACE,QAAQ;gBACR,mBAAmB,EAAE,IAAI;gBACzB,uBAAuB,EAAE,IAAI;gBAC7B,aAAa,EAAE,IAAI;gBACnB,UAAU;aACX,EACD;gBACE,sEAAsE;gBACtE,GAAG,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACnD,CACF,CAAC;YAEJ,0EAA0E;YAC1E,MAAM,aAAa,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAC5C,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,uBAAuB,CAC9C,CAAC;YACF,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC/B,OAAO;YACT,CAAC;YAED,MAAM,kBAAkB,GAAG,oBAAoB,CAC7C,EAAE,GAAG,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,EACxC,uBAAuB,CACxB,CAAC;YAEF,0EAA0E;YAC1E,kBAAkB;YAClB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,KAAK,MAAM,CAAC,SAAS,EAAE,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.js';\nimport type { DeFiPositionsControllerV2MethodActions } from './DeFiPositionsControllerV2-method-action-types.js';\nimport type { DeFiPositionsByAccount } from './group-defi-positions-v6.js';\nimport { groupDeFiPositionsV6 } from './group-defi-positions-v6.js';\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"]}
1
+ {"version":3,"file":"DeFiPositionsControllerV2.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsControllerV2.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAU3D,OAAO,EAAE,sBAAsB,EAAE,wCAAuC;AAExE,OAAO,EAAE,uBAAuB,EAAE,8CAA6C;AAG/E,OAAO,EAAE,oBAAoB,EAAE,sCAAqC;AAEpE,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD,MAAM,yBAAyB,GAAG,CAAC,oBAAoB,CAAU,CAAC;AAElE;;;GAGG;AACH,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAmBD,MAAM,kBAAkB,GAAkD;IACxE,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,wCAAwC,GACnD,GAAmC,EAAE;IACnC,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC,CAAC;AA2CJ;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,yBAA0B,SAAQ,cAI9C;IAeC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,aAAa,EACb,KAAK,GAON;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,kBAAkB;YAC5B,SAAS;YACT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,GAAG,CAAC,CAAC;YACjD,IAAI,aAAa,EAAE,CAAC;gBAClB,OAAO;YACT,CAAC;YAED,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC;QAC5B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;YACvD,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC","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';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\n\nimport { buildDeFiBalancesQuery } from './build-defi-balances-query.js';\nimport type { DeFiBalancesQuery } from './build-defi-balances-query.js';\nimport { getProcessingPollConfig } from './defi-controller-v2-feature-flag.js';\nimport type { DeFiPositionsControllerV2MethodActions } from './DeFiPositionsControllerV2-method-action-types.js';\nimport type { DeFiPositionsByAccount } from './group-defi-positions-v6.js';\nimport { groupDeFiPositionsV6 } from './group-defi-positions-v6.js';\n\nconst controllerName = 'DeFiPositionsControllerV2';\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchDeFiPositions'] as const;\n\n/**\n * @param ms - Milliseconds to wait.\n * @returns A promise that resolves after `ms`.\n */\nfunction delay(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\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 | RemoteFeatureFlagControllerGetStateAction;\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 *\n * When the API reports `processingDefiPositions` for any account, this\n * controller polls until indexing finishes or the attempt limit is reached,\n * and only then processes and writes state — mixed ready/processing responses\n * leave prior state untouched. Concurrent calls for the same selected accounts\n * and `vsCurrency` share one in-flight promise so the UI can treat the pending\n * promise as a loading signal. Calls for a different selection or fiat currency\n * start their own fetch and leave any prior poll running, so switching back can\n * join an in-flight fetch for that group and currency.\n *\n * Processing-poll `maxAttempts` / `pollInterval` are read via\n * {@link getProcessingPollConfig} from the `defiControllerV2` remote feature\n * flag (`RemoteFeatureFlagController:getState`), falling back to\n * `DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS` /\n * `DEFAULT_PROCESSING_POLL_INTERVAL_MS` when unset or invalid. Clients\n * must delegate that action to this controller's messenger.\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 * In-flight fetches keyed by selected DeFi-queryable account IDs plus\n * `vsCurrency`. Concurrent callers for the same selection and currency share\n * a promise; different selections or fiat currencies keep independent\n * fetches so fast switching can join an earlier matching poll.\n */\n readonly #inFlightFetches = new Map<string, Promise<void>>();\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. State is updated only\n * when every selected account in the response is ready (none report\n * `processingDefiPositions`); each account key in that response replaces that\n * account's state (other accounts stay). While any account is still indexing,\n * prior state is kept and the method polls (invalidating the balances cache\n * between attempts) until all selected accounts are ready, the attempt limit\n * is reached, or a request fails. Concurrent calls for the same selected\n * accounts and `vsCurrency` share one in-flight promise; calls for a different\n * selection or fiat currency start a new fetch and leave prior polls running\n * so a later switch back can join them. When a successful ready response\n * required more than one attempt, reports the attempt count to Sentry via\n * `messenger.captureException` (error name `DeFiPositionsV2FetchAttempts`) so\n * poll limits can be tuned. 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 on the\n * first attempt (e.g. pull-to-refresh).\n *\n * @param options - Optional fetch modifiers.\n * @param options.forceRefresh - When true, bypass the apiClient cache on the\n * first attempt and fetch immediately.\n * @returns Resolves when the fetch (and any processing polls) finish.\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 const query = buildDeFiBalancesQuery(selectedAccounts);\n const vsCurrency = this.#getVsCurrency().toLowerCase();\n // Include vsCurrency so a fiat change does not join a poll priced in the\n // previous currency (TanStack also keys the balances cache on vsCurrency).\n const inFlightKey = [\n ...[...query.internalAccountIdByCaip.keys()].sort(),\n vsCurrency,\n ].join('\\0');\n\n const existing = this.#inFlightFetches.get(inFlightKey);\n if (existing) {\n await existing;\n return;\n }\n\n // Same-key callers join this promise instead of replacing it, so cleanup\n // can always delete without checking identity.\n const fetchPromise = this.#fetchDeFiPositions(\n options,\n query,\n vsCurrency,\n ).finally(() => {\n this.#inFlightFetches.delete(inFlightKey);\n });\n this.#inFlightFetches.set(inFlightKey, fetchPromise);\n\n await fetchPromise;\n }\n\n async #fetchDeFiPositions(\n options: { forceRefresh?: boolean } | undefined,\n { networks, internalAccountIdByCaip }: DeFiBalancesQuery,\n vsCurrency: string,\n ): Promise<void> {\n if (internalAccountIdByCaip.size === 0 || networks.length === 0) {\n return;\n }\n\n const accountIds = [...internalAccountIdByCaip.keys()];\n const queryOptions = {\n networks,\n includeDeFiBalances: true,\n forceFetchDeFiPositions: true,\n includePrices: true,\n vsCurrency,\n };\n\n // Resolve once per fetch so a mid-poll remote-flag change cannot stretch\n // or shrink this poll sequence inconsistently.\n const { maxAttempts, pollInterval } = getProcessingPollConfig(\n this.messenger,\n );\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n // First attempt respects forceRefresh; later polls always bypass cache\n // so we do not spin on a stale processing snapshot.\n const fetchOptions = {\n ...(options?.forceRefresh || attempt > 0 ? { staleTime: 0 } : {}),\n };\n\n try {\n const response =\n await this.#apiClient.accounts.fetchV6MultiAccountBalances(\n accountIds,\n queryOptions,\n fetchOptions,\n );\n\n const stillProcessing = response.accounts.some(\n (account) => account.processingDefiPositions,\n );\n\n // Only process and write when every account is ready so a partial\n // response cannot clear or overwrite positions for accounts that are\n // still indexing.\n if (!stillProcessing) {\n const positionsByAccount = groupDeFiPositionsV6(\n response,\n internalAccountIdByCaip,\n );\n\n this.update((state) => {\n for (const [accountId, positions] of Object.entries(\n positionsByAccount,\n )) {\n state.allDeFiPositionsV2[accountId] = positions;\n }\n });\n\n // Report how many attempts were needed (only when polling was\n // required) so we can tune DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS /\n // INTERVAL from Sentry without flooding on first-try successes.\n const attemptsTaken = attempt + 1;\n if (attemptsTaken > 1) {\n const sentryError = new Error(\n `DeFiPositionsControllerV2: positions ready after ${attemptsTaken} attempt(s)`,\n );\n sentryError.name = 'DeFiPositionsV2FetchAttempts';\n this.messenger.captureException?.(sentryError);\n }\n return;\n }\n\n const { queryKey } =\n this.#apiClient.accounts.getV6MultiAccountBalancesQueryOptions(\n accountIds,\n queryOptions,\n fetchOptions,\n );\n await this.#apiClient.accounts.queryClient.invalidateQueries({\n queryKey,\n });\n\n const isLastAttempt = attempt >= maxAttempts - 1;\n if (isLastAttempt) {\n return;\n }\n\n await delay(pollInterval);\n } catch (error) {\n console.error('Failed to fetch DeFi positions', error);\n return;\n }\n }\n }\n}\n"]}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getProcessingPollConfig = void 0;
4
+ /**
5
+ * Remote feature flag key for DeFi Positions Controller V2 (camelCase, as
6
+ * stored by RemoteFeatureFlagController after client-config resolution).
7
+ */
8
+ const DEFI_CONTROLLER_V2_FEATURE_FLAG = 'defiControllerV2';
9
+ /** Delay between polls while Accounts API reports DeFi indexing in progress. */
10
+ const DEFAULT_PROCESSING_POLL_INTERVAL_MS = 3000;
11
+ /**
12
+ * Maximum fetch attempts (including the first) while any account still has
13
+ * `processingDefiPositions: true`. After this, the call resolves without
14
+ * updating state, so prior positions are kept for every selected account.
15
+ */
16
+ const DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS = 3;
17
+ /**
18
+ * @param config - Optional remote poll overrides.
19
+ * @returns Resolved positive integer max attempts and poll interval ms.
20
+ */
21
+ function resolveProcessingPollConfig(config) {
22
+ const maxAttempts = typeof config?.maxAttempts === 'number' &&
23
+ Number.isFinite(config.maxAttempts) &&
24
+ config.maxAttempts > 0
25
+ ? Math.floor(config.maxAttempts)
26
+ : DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS;
27
+ const pollInterval = typeof config?.pollInterval === 'number' &&
28
+ Number.isFinite(config.pollInterval) &&
29
+ config.pollInterval > 0
30
+ ? config.pollInterval
31
+ : DEFAULT_PROCESSING_POLL_INTERVAL_MS;
32
+ return { maxAttempts, pollInterval };
33
+ }
34
+ /**
35
+ * Narrows a remote feature-flag JSON value to the DeFi V2 poll config fields.
36
+ *
37
+ * @param flag - Raw flag value from RemoteFeatureFlagController state.
38
+ * @returns Poll config fields when present, otherwise `undefined`.
39
+ */
40
+ function parseDeFiControllerV2FeatureFlag(flag) {
41
+ if (!flag || typeof flag !== 'object' || Array.isArray(flag)) {
42
+ return undefined;
43
+ }
44
+ const { maxAttempts, pollInterval } = flag;
45
+ return { maxAttempts, pollInterval };
46
+ }
47
+ /**
48
+ * Reads `defiControllerV2` from RemoteFeatureFlagController and returns
49
+ * resolved processing-poll limits. Missing or invalid flag values fall back to
50
+ * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /
51
+ * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.
52
+ *
53
+ * @param messenger - Messenger that can call
54
+ * `RemoteFeatureFlagController:getState`.
55
+ * @returns Positive integer max attempts and poll interval ms.
56
+ */
57
+ function getProcessingPollConfig(messenger) {
58
+ const { remoteFeatureFlags } = messenger.call('RemoteFeatureFlagController:getState');
59
+ return resolveProcessingPollConfig(parseDeFiControllerV2FeatureFlag(remoteFeatureFlags?.[DEFI_CONTROLLER_V2_FEATURE_FLAG]));
60
+ }
61
+ exports.getProcessingPollConfig = getProcessingPollConfig;
62
+ //# sourceMappingURL=defi-controller-v2-feature-flag.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defi-controller-v2-feature-flag.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/defi-controller-v2-feature-flag.ts"],"names":[],"mappings":";;;AAGA;;;GAGG;AACH,MAAM,+BAA+B,GAAG,kBAAkB,CAAC;AAE3D,gFAAgF;AAChF,MAAM,mCAAmC,GAAG,IAAK,CAAC;AAElD;;;;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State } from '@metamask/remote-feature-flag-controller';\nimport type { Json } from '@metamask/utils';\n\n/**\n * Remote feature flag key for DeFi Positions Controller V2 (camelCase, as\n * stored by RemoteFeatureFlagController after client-config resolution).\n */\nconst DEFI_CONTROLLER_V2_FEATURE_FLAG = 'defiControllerV2';\n\n/** Delay between polls while Accounts API reports DeFi indexing in progress. */\nconst DEFAULT_PROCESSING_POLL_INTERVAL_MS = 3_000;\n\n/**\n * Maximum fetch attempts (including the first) while any account still has\n * `processingDefiPositions: true`. After this, the call resolves without\n * updating state, so prior positions are kept for every selected account.\n */\nconst DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS = 3;\n\n/**\n * Resolved `defiControllerV2` remote feature flag shape used for processing\n * poll overrides. `enabled` is read by clients for gating; the controller\n * only consumes `maxAttempts` / `pollInterval`.\n */\ntype DeFiControllerV2FeatureFlag = {\n enabled?: boolean;\n maxAttempts?: number;\n pollInterval?: number;\n};\n\n/**\n * Optional processing-poll overrides from {@link DeFiControllerV2FeatureFlag}.\n * Missing or non-positive values fall back to\n * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /\n * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.\n */\ntype DeFiPositionsControllerV2ProcessingPollConfig = {\n maxAttempts?: number;\n pollInterval?: number;\n};\n\n/**\n * Resolved positive integer processing-poll limits.\n */\ntype ResolvedProcessingPollConfig = {\n maxAttempts: number;\n pollInterval: number;\n};\n\n/**\n * Messenger surface needed to read the DeFi V2 remote feature flag.\n */\ntype GetProcessingPollConfigMessenger = {\n call: (\n actionType: 'RemoteFeatureFlagController:getState',\n ) => RemoteFeatureFlagControllerState;\n};\n\n/**\n * @param config - Optional remote poll overrides.\n * @returns Resolved positive integer max attempts and poll interval ms.\n */\nfunction resolveProcessingPollConfig(\n config?: DeFiPositionsControllerV2ProcessingPollConfig | null,\n): ResolvedProcessingPollConfig {\n const maxAttempts =\n typeof config?.maxAttempts === 'number' &&\n Number.isFinite(config.maxAttempts) &&\n config.maxAttempts > 0\n ? Math.floor(config.maxAttempts)\n : DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS;\n const pollInterval =\n typeof config?.pollInterval === 'number' &&\n Number.isFinite(config.pollInterval) &&\n config.pollInterval > 0\n ? config.pollInterval\n : DEFAULT_PROCESSING_POLL_INTERVAL_MS;\n\n return { maxAttempts, pollInterval };\n}\n\n/**\n * Narrows a remote feature-flag JSON value to the DeFi V2 poll config fields.\n *\n * @param flag - Raw flag value from RemoteFeatureFlagController state.\n * @returns Poll config fields when present, otherwise `undefined`.\n */\nfunction parseDeFiControllerV2FeatureFlag(\n flag: Json | undefined,\n): DeFiPositionsControllerV2ProcessingPollConfig | undefined {\n if (!flag || typeof flag !== 'object' || Array.isArray(flag)) {\n return undefined;\n }\n\n const { maxAttempts, pollInterval } = flag as DeFiControllerV2FeatureFlag;\n return { maxAttempts, pollInterval };\n}\n\n/**\n * Reads `defiControllerV2` from RemoteFeatureFlagController and returns\n * resolved processing-poll limits. Missing or invalid flag values fall back to\n * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /\n * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.\n *\n * @param messenger - Messenger that can call\n * `RemoteFeatureFlagController:getState`.\n * @returns Positive integer max attempts and poll interval ms.\n */\nexport function getProcessingPollConfig(\n messenger: GetProcessingPollConfigMessenger,\n): ResolvedProcessingPollConfig {\n const { remoteFeatureFlags } = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n return resolveProcessingPollConfig(\n parseDeFiControllerV2FeatureFlag(\n remoteFeatureFlags?.[DEFI_CONTROLLER_V2_FEATURE_FLAG],\n ),\n );\n}\n"]}
@@ -0,0 +1,27 @@
1
+ import type { RemoteFeatureFlagControllerState } from "@metamask/remote-feature-flag-controller";
2
+ /**
3
+ * Resolved positive integer processing-poll limits.
4
+ */
5
+ type ResolvedProcessingPollConfig = {
6
+ maxAttempts: number;
7
+ pollInterval: number;
8
+ };
9
+ /**
10
+ * Messenger surface needed to read the DeFi V2 remote feature flag.
11
+ */
12
+ type GetProcessingPollConfigMessenger = {
13
+ call: (actionType: 'RemoteFeatureFlagController:getState') => RemoteFeatureFlagControllerState;
14
+ };
15
+ /**
16
+ * Reads `defiControllerV2` from RemoteFeatureFlagController and returns
17
+ * resolved processing-poll limits. Missing or invalid flag values fall back to
18
+ * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /
19
+ * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.
20
+ *
21
+ * @param messenger - Messenger that can call
22
+ * `RemoteFeatureFlagController:getState`.
23
+ * @returns Positive integer max attempts and poll interval ms.
24
+ */
25
+ export declare function getProcessingPollConfig(messenger: GetProcessingPollConfigMessenger): ResolvedProcessingPollConfig;
26
+ export {};
27
+ //# sourceMappingURL=defi-controller-v2-feature-flag.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defi-controller-v2-feature-flag.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/defi-controller-v2-feature-flag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,iDAAiD;AAyCjG;;GAEG;AACH,KAAK,4BAA4B,GAAG;IAClC,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,KAAK,gCAAgC,GAAG;IACtC,IAAI,EAAE,CACJ,UAAU,EAAE,sCAAsC,KAC/C,gCAAgC,CAAC;CACvC,CAAC;AA0CF;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CACrC,SAAS,EAAE,gCAAgC,GAC1C,4BAA4B,CAS9B"}
@@ -0,0 +1,27 @@
1
+ import type { RemoteFeatureFlagControllerState } from "@metamask/remote-feature-flag-controller";
2
+ /**
3
+ * Resolved positive integer processing-poll limits.
4
+ */
5
+ type ResolvedProcessingPollConfig = {
6
+ maxAttempts: number;
7
+ pollInterval: number;
8
+ };
9
+ /**
10
+ * Messenger surface needed to read the DeFi V2 remote feature flag.
11
+ */
12
+ type GetProcessingPollConfigMessenger = {
13
+ call: (actionType: 'RemoteFeatureFlagController:getState') => RemoteFeatureFlagControllerState;
14
+ };
15
+ /**
16
+ * Reads `defiControllerV2` from RemoteFeatureFlagController and returns
17
+ * resolved processing-poll limits. Missing or invalid flag values fall back to
18
+ * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /
19
+ * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.
20
+ *
21
+ * @param messenger - Messenger that can call
22
+ * `RemoteFeatureFlagController:getState`.
23
+ * @returns Positive integer max attempts and poll interval ms.
24
+ */
25
+ export declare function getProcessingPollConfig(messenger: GetProcessingPollConfigMessenger): ResolvedProcessingPollConfig;
26
+ export {};
27
+ //# sourceMappingURL=defi-controller-v2-feature-flag.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defi-controller-v2-feature-flag.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/defi-controller-v2-feature-flag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,iDAAiD;AAyCjG;;GAEG;AACH,KAAK,4BAA4B,GAAG;IAClC,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,KAAK,gCAAgC,GAAG;IACtC,IAAI,EAAE,CACJ,UAAU,EAAE,sCAAsC,KAC/C,gCAAgC,CAAC;CACvC,CAAC;AA0CF;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CACrC,SAAS,EAAE,gCAAgC,GAC1C,4BAA4B,CAS9B"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Remote feature flag key for DeFi Positions Controller V2 (camelCase, as
3
+ * stored by RemoteFeatureFlagController after client-config resolution).
4
+ */
5
+ const DEFI_CONTROLLER_V2_FEATURE_FLAG = 'defiControllerV2';
6
+ /** Delay between polls while Accounts API reports DeFi indexing in progress. */
7
+ const DEFAULT_PROCESSING_POLL_INTERVAL_MS = 3000;
8
+ /**
9
+ * Maximum fetch attempts (including the first) while any account still has
10
+ * `processingDefiPositions: true`. After this, the call resolves without
11
+ * updating state, so prior positions are kept for every selected account.
12
+ */
13
+ const DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS = 3;
14
+ /**
15
+ * @param config - Optional remote poll overrides.
16
+ * @returns Resolved positive integer max attempts and poll interval ms.
17
+ */
18
+ function resolveProcessingPollConfig(config) {
19
+ const maxAttempts = typeof config?.maxAttempts === 'number' &&
20
+ Number.isFinite(config.maxAttempts) &&
21
+ config.maxAttempts > 0
22
+ ? Math.floor(config.maxAttempts)
23
+ : DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS;
24
+ const pollInterval = typeof config?.pollInterval === 'number' &&
25
+ Number.isFinite(config.pollInterval) &&
26
+ config.pollInterval > 0
27
+ ? config.pollInterval
28
+ : DEFAULT_PROCESSING_POLL_INTERVAL_MS;
29
+ return { maxAttempts, pollInterval };
30
+ }
31
+ /**
32
+ * Narrows a remote feature-flag JSON value to the DeFi V2 poll config fields.
33
+ *
34
+ * @param flag - Raw flag value from RemoteFeatureFlagController state.
35
+ * @returns Poll config fields when present, otherwise `undefined`.
36
+ */
37
+ function parseDeFiControllerV2FeatureFlag(flag) {
38
+ if (!flag || typeof flag !== 'object' || Array.isArray(flag)) {
39
+ return undefined;
40
+ }
41
+ const { maxAttempts, pollInterval } = flag;
42
+ return { maxAttempts, pollInterval };
43
+ }
44
+ /**
45
+ * Reads `defiControllerV2` from RemoteFeatureFlagController and returns
46
+ * resolved processing-poll limits. Missing or invalid flag values fall back to
47
+ * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /
48
+ * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.
49
+ *
50
+ * @param messenger - Messenger that can call
51
+ * `RemoteFeatureFlagController:getState`.
52
+ * @returns Positive integer max attempts and poll interval ms.
53
+ */
54
+ export function getProcessingPollConfig(messenger) {
55
+ const { remoteFeatureFlags } = messenger.call('RemoteFeatureFlagController:getState');
56
+ return resolveProcessingPollConfig(parseDeFiControllerV2FeatureFlag(remoteFeatureFlags?.[DEFI_CONTROLLER_V2_FEATURE_FLAG]));
57
+ }
58
+ //# sourceMappingURL=defi-controller-v2-feature-flag.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defi-controller-v2-feature-flag.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/defi-controller-v2-feature-flag.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,+BAA+B,GAAG,kBAAkB,CAAC;AAE3D,gFAAgF;AAChF,MAAM,mCAAmC,GAAG,IAAK,CAAC;AAElD;;;;GAIG;AACH,MAAM,oCAAoC,GAAG,CAAC,CAAC;AAyC/C;;;GAGG;AACH,SAAS,2BAA2B,CAClC,MAA6D;IAE7D,MAAM,WAAW,GACf,OAAO,MAAM,EAAE,WAAW,KAAK,QAAQ;QACvC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC;QACnC,MAAM,CAAC,WAAW,GAAG,CAAC;QACpB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC;QAChC,CAAC,CAAC,oCAAoC,CAAC;IAC3C,MAAM,YAAY,GAChB,OAAO,MAAM,EAAE,YAAY,KAAK,QAAQ;QACxC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,YAAY,CAAC;QACpC,MAAM,CAAC,YAAY,GAAG,CAAC;QACrB,CAAC,CAAC,MAAM,CAAC,YAAY;QACrB,CAAC,CAAC,mCAAmC,CAAC;IAE1C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC;AACvC,CAAC;AAED;;;;;GAKG;AACH,SAAS,gCAAgC,CACvC,IAAsB;IAEtB,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7D,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,IAAmC,CAAC;IAC1E,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC;AACvC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CACrC,SAA2C;IAE3C,MAAM,EAAE,kBAAkB,EAAE,GAAG,SAAS,CAAC,IAAI,CAC3C,sCAAsC,CACvC,CAAC;IACF,OAAO,2BAA2B,CAChC,gCAAgC,CAC9B,kBAAkB,EAAE,CAAC,+BAA+B,CAAC,CACtD,CACF,CAAC;AACJ,CAAC","sourcesContent":["import type { RemoteFeatureFlagControllerState } from '@metamask/remote-feature-flag-controller';\nimport type { Json } from '@metamask/utils';\n\n/**\n * Remote feature flag key for DeFi Positions Controller V2 (camelCase, as\n * stored by RemoteFeatureFlagController after client-config resolution).\n */\nconst DEFI_CONTROLLER_V2_FEATURE_FLAG = 'defiControllerV2';\n\n/** Delay between polls while Accounts API reports DeFi indexing in progress. */\nconst DEFAULT_PROCESSING_POLL_INTERVAL_MS = 3_000;\n\n/**\n * Maximum fetch attempts (including the first) while any account still has\n * `processingDefiPositions: true`. After this, the call resolves without\n * updating state, so prior positions are kept for every selected account.\n */\nconst DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS = 3;\n\n/**\n * Resolved `defiControllerV2` remote feature flag shape used for processing\n * poll overrides. `enabled` is read by clients for gating; the controller\n * only consumes `maxAttempts` / `pollInterval`.\n */\ntype DeFiControllerV2FeatureFlag = {\n enabled?: boolean;\n maxAttempts?: number;\n pollInterval?: number;\n};\n\n/**\n * Optional processing-poll overrides from {@link DeFiControllerV2FeatureFlag}.\n * Missing or non-positive values fall back to\n * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /\n * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.\n */\ntype DeFiPositionsControllerV2ProcessingPollConfig = {\n maxAttempts?: number;\n pollInterval?: number;\n};\n\n/**\n * Resolved positive integer processing-poll limits.\n */\ntype ResolvedProcessingPollConfig = {\n maxAttempts: number;\n pollInterval: number;\n};\n\n/**\n * Messenger surface needed to read the DeFi V2 remote feature flag.\n */\ntype GetProcessingPollConfigMessenger = {\n call: (\n actionType: 'RemoteFeatureFlagController:getState',\n ) => RemoteFeatureFlagControllerState;\n};\n\n/**\n * @param config - Optional remote poll overrides.\n * @returns Resolved positive integer max attempts and poll interval ms.\n */\nfunction resolveProcessingPollConfig(\n config?: DeFiPositionsControllerV2ProcessingPollConfig | null,\n): ResolvedProcessingPollConfig {\n const maxAttempts =\n typeof config?.maxAttempts === 'number' &&\n Number.isFinite(config.maxAttempts) &&\n config.maxAttempts > 0\n ? Math.floor(config.maxAttempts)\n : DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS;\n const pollInterval =\n typeof config?.pollInterval === 'number' &&\n Number.isFinite(config.pollInterval) &&\n config.pollInterval > 0\n ? config.pollInterval\n : DEFAULT_PROCESSING_POLL_INTERVAL_MS;\n\n return { maxAttempts, pollInterval };\n}\n\n/**\n * Narrows a remote feature-flag JSON value to the DeFi V2 poll config fields.\n *\n * @param flag - Raw flag value from RemoteFeatureFlagController state.\n * @returns Poll config fields when present, otherwise `undefined`.\n */\nfunction parseDeFiControllerV2FeatureFlag(\n flag: Json | undefined,\n): DeFiPositionsControllerV2ProcessingPollConfig | undefined {\n if (!flag || typeof flag !== 'object' || Array.isArray(flag)) {\n return undefined;\n }\n\n const { maxAttempts, pollInterval } = flag as DeFiControllerV2FeatureFlag;\n return { maxAttempts, pollInterval };\n}\n\n/**\n * Reads `defiControllerV2` from RemoteFeatureFlagController and returns\n * resolved processing-poll limits. Missing or invalid flag values fall back to\n * {@link DEFAULT_PROCESSING_POLL_MAX_ATTEMPTS} /\n * {@link DEFAULT_PROCESSING_POLL_INTERVAL_MS}.\n *\n * @param messenger - Messenger that can call\n * `RemoteFeatureFlagController:getState`.\n * @returns Positive integer max attempts and poll interval ms.\n */\nexport function getProcessingPollConfig(\n messenger: GetProcessingPollConfigMessenger,\n): ResolvedProcessingPollConfig {\n const { remoteFeatureFlags } = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n return resolveProcessingPollConfig(\n parseDeFiControllerV2FeatureFlag(\n remoteFeatureFlags?.[DEFI_CONTROLLER_V2_FEATURE_FLAG],\n ),\n );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/assets-controllers",
3
- "version": "110.1.0-preview-7b60eca",
3
+ "version": "110.1.0-preview-54ee0c6b9",
4
4
  "description": "Controllers which manage interactions involving ERC-20, ERC-721, and ERC-1155 tokens (including NFTs)",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -82,6 +82,7 @@
82
82
  "@metamask/polling-controller": "^16.0.9",
83
83
  "@metamask/preferences-controller": "^23.1.0",
84
84
  "@metamask/profile-sync-controller": "^28.3.0",
85
+ "@metamask/remote-feature-flag-controller": "^5.0.0",
85
86
  "@metamask/rpc-errors": "^7.0.2",
86
87
  "@metamask/snaps-controllers": "^19.0.0",
87
88
  "@metamask/snaps-sdk": "^11.0.0",