@metamask/ramps-controller 9.0.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [10.0.0]
11
+
12
+ ### Changed
13
+
14
+ - **BREAKING:** Remove `state.quotes` and `state.widgetUrl` from RampsController state. Quote and widget URL data are now managed by consuming components ([#8013](https://github.com/MetaMask/core/pull/8013))
15
+ - **BREAKING:** Remove `fetchQuotesForSelection()` and `setSelectedQuote()`. Components call `getQuotes()` directly and manage selection locally ([#8013](https://github.com/MetaMask/core/pull/8013))
16
+ - Simplify `getWidgetUrl()` to a pure fetch-and-return API; it no longer reads or writes controller state ([#8013](https://github.com/MetaMask/core/pull/8013))
17
+ - Improve `TransakService` error handling ([#8010](https://github.com/MetaMask/core/pull/8010))
18
+ - **BREAKING:** Replace `startQuotePolling()`/`stopQuotePolling()` with `fetchQuotesForSelection()` — quotes are now fetched once per call instead of polling on a 15-second interval ([#7999](https://github.com/MetaMask/core/pull/7999))
19
+
20
+ ### Removed
21
+
22
+ - Remove `stopQuotePolling()` method (no interval to stop) ([#7999](https://github.com/MetaMask/core/pull/7999))
23
+ - Remove internal polling restart logic (`#restartPollingIfActive`) from `setSelectedProvider`, `setSelectedToken`, and `setSelectedPaymentMethod` ([#7999](https://github.com/MetaMask/core/pull/7999))
24
+
25
+ ### Fixed
26
+
27
+ - Fix RampsController flaky test ([#8018](https://github.com/MetaMask/core/pull/8018))
28
+
10
29
  ## [9.0.0]
11
30
 
12
31
  ### Added
@@ -164,7 +183,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
164
183
  - Add `OnRampService` for interacting with the OnRamp API
165
184
  - Add geolocation detection via IP address lookup
166
185
 
167
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@9.0.0...HEAD
186
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@10.0.0...HEAD
187
+ [10.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@9.0.0...@metamask/ramps-controller@10.0.0
168
188
  [9.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@8.1.0...@metamask/ramps-controller@9.0.0
169
189
  [8.1.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@8.0.0...@metamask/ramps-controller@8.1.0
170
190
  [8.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@7.1.0...@metamask/ramps-controller@8.0.0
@@ -10,7 +10,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
11
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
12
  };
13
- var _RampsController_instances, _RampsController_requestCacheTTL, _RampsController_requestCacheMaxSize, _RampsController_pendingRequests, _RampsController_pendingResourceCount, _RampsController_quotePollingInterval, _RampsController_quotePollingOptions, _RampsController_clearPendingResourceCountForDependentResources, _RampsController_abortDependentRequests, _RampsController_mutateRequests, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _RampsController_restartPollingIfActive, _RampsController_requireRegion, _RampsController_isRegionCurrent, _RampsController_isTokenCurrent, _RampsController_isProviderCurrent, _RampsController_updateResourceField, _RampsController_setResourceLoading, _RampsController_setResourceError, _RampsController_updateRequestState, _RampsController_syncWidgetUrl, _RampsController_syncTransakAuthOnError;
13
+ var _RampsController_instances, _RampsController_requestCacheTTL, _RampsController_requestCacheMaxSize, _RampsController_pendingRequests, _RampsController_pendingResourceCount, _RampsController_clearPendingResourceCountForDependentResources, _RampsController_abortDependentRequests, _RampsController_mutateRequests, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _RampsController_requireRegion, _RampsController_isRegionCurrent, _RampsController_isTokenCurrent, _RampsController_isProviderCurrent, _RampsController_updateResourceField, _RampsController_setResourceLoading, _RampsController_setResourceError, _RampsController_updateRequestState, _RampsController_syncTransakAuthOnError;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.RampsController = exports.getDefaultRampsControllerState = exports.RAMPS_CONTROLLER_REQUIRED_SERVICE_ACTIONS = exports.controllerName = void 0;
16
16
  const base_controller_1 = require("@metamask/base-controller");
@@ -101,18 +101,6 @@ const rampsControllerMetadata = {
101
101
  includeInStateLogs: true,
102
102
  usedInUi: true,
103
103
  },
104
- quotes: {
105
- persist: false,
106
- includeInDebugSnapshot: true,
107
- includeInStateLogs: false,
108
- usedInUi: true,
109
- },
110
- widgetUrl: {
111
- persist: false,
112
- includeInDebugSnapshot: true,
113
- includeInStateLogs: false,
114
- usedInUi: true,
115
- },
116
104
  requests: {
117
105
  persist: false,
118
106
  includeInDebugSnapshot: true,
@@ -158,8 +146,6 @@ function getDefaultRampsControllerState() {
158
146
  providers: createDefaultResourceState([], null),
159
147
  tokens: createDefaultResourceState(null, null),
160
148
  paymentMethods: createDefaultResourceState([], null),
161
- quotes: createDefaultResourceState(null, null),
162
- widgetUrl: createDefaultResourceState(null),
163
149
  requests: {},
164
150
  nativeProviders: {
165
151
  transak: {
@@ -176,7 +162,6 @@ const DEPENDENT_RESOURCE_KEYS = [
176
162
  'providers',
177
163
  'tokens',
178
164
  'paymentMethods',
179
- 'quotes',
180
165
  ];
181
166
  const DEPENDENT_RESOURCE_KEYS_SET = new Set(DEPENDENT_RESOURCE_KEYS);
182
167
  function resetResource(state, resourceType, defaultResource) {
@@ -188,20 +173,7 @@ function resetResource(state, resourceType, defaultResource) {
188
173
  resource.error = def.error;
189
174
  }
190
175
  /**
191
- * Resets the widgetUrl resource to its default state.
192
- * Mutates state in place; use from within controller update() for atomic updates.
193
- *
194
- * @param state - The state object to mutate.
195
- */
196
- function resetWidgetUrl(state) {
197
- const def = getDefaultRampsControllerState().widgetUrl;
198
- state.widgetUrl.data = def.data;
199
- state.widgetUrl.selected = def.selected;
200
- state.widgetUrl.isLoading = def.isLoading;
201
- state.widgetUrl.error = def.error;
202
- }
203
- /**
204
- * Resets region-dependent resources (userRegion, providers, tokens, paymentMethods, quotes).
176
+ * Resets region-dependent resources (userRegion, providers, tokens, paymentMethods).
205
177
  * Mutates state in place; use from within controller update() for atomic updates.
206
178
  *
207
179
  * @param state - The state object to mutate.
@@ -216,7 +188,6 @@ function resetDependentResources(state, options) {
216
188
  for (const key of DEPENDENT_RESOURCE_KEYS) {
217
189
  resetResource(state, key, defaultState[key]);
218
190
  }
219
- resetWidgetUrl(state);
220
191
  }
221
192
  // === HELPER FUNCTIONS ===
222
193
  /**
@@ -326,16 +297,6 @@ class RampsController extends base_controller_1.BaseController {
326
297
  * Used so isLoading is only cleared when the last request for that resource finishes.
327
298
  */
328
299
  _RampsController_pendingResourceCount.set(this, new Map());
329
- /**
330
- * Interval ID for automatic quote polling.
331
- * Set when startQuotePolling() is called, cleared when stopQuotePolling() is called.
332
- */
333
- _RampsController_quotePollingInterval.set(this, null);
334
- /**
335
- * Options used for quote polling (walletAddress, amount, redirectUrl).
336
- * Stored so polling can be restarted when dependencies change.
337
- */
338
- _RampsController_quotePollingOptions.set(this, null);
339
300
  __classPrivateFieldSet(this, _RampsController_requestCacheTTL, requestCacheTTL, "f");
340
301
  __classPrivateFieldSet(this, _RampsController_requestCacheMaxSize, requestCacheMaxSize, "f");
341
302
  }
@@ -506,7 +467,6 @@ class RampsController extends base_controller_1.BaseController {
506
467
  if (regionChanged) {
507
468
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_abortDependentRequests).call(this);
508
469
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
509
- this.stopQuotePolling();
510
470
  }
511
471
  this.update((state) => {
512
472
  if (regionChanged) {
@@ -543,7 +503,6 @@ class RampsController extends base_controller_1.BaseController {
543
503
  */
544
504
  setSelectedProvider(providerId) {
545
505
  if (providerId === null) {
546
- this.stopQuotePolling();
547
506
  this.update((state) => {
548
507
  state.providers.selected = null;
549
508
  resetResource(state, 'paymentMethods');
@@ -562,14 +521,8 @@ class RampsController extends base_controller_1.BaseController {
562
521
  this.update((state) => {
563
522
  state.providers.selected = provider;
564
523
  resetResource(state, 'paymentMethods');
565
- state.quotes.selected = null;
566
- resetWidgetUrl(state);
567
524
  });
568
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }).then(() => {
569
- // Restart quote polling after payment methods are fetched
570
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
571
- return undefined;
572
- }));
525
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }));
573
526
  }
574
527
  /**
575
528
  * Initializes the controller by fetching the user's region from geolocation.
@@ -658,7 +611,6 @@ class RampsController extends base_controller_1.BaseController {
658
611
  */
659
612
  setSelectedToken(assetId) {
660
613
  if (!assetId) {
661
- this.stopQuotePolling();
662
614
  this.update((state) => {
663
615
  state.tokens.selected = null;
664
616
  resetResource(state, 'paymentMethods');
@@ -678,13 +630,8 @@ class RampsController extends base_controller_1.BaseController {
678
630
  this.update((state) => {
679
631
  state.tokens.selected = token;
680
632
  resetResource(state, 'paymentMethods');
681
- state.quotes.selected = null;
682
- resetWidgetUrl(state);
683
633
  });
684
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { assetId: token.assetId }).then(() => {
685
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
686
- return undefined;
687
- }));
634
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { assetId: token.assetId }));
688
635
  }
689
636
  /**
690
637
  * Fetches the list of providers for a given region.
@@ -816,12 +763,10 @@ class RampsController extends base_controller_1.BaseController {
816
763
  this.update((state) => {
817
764
  state.paymentMethods.selected = paymentMethod;
818
765
  });
819
- // Restart quote polling if active
820
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
821
766
  }
822
767
  /**
823
768
  * Fetches quotes from all providers for a given set of parameters.
824
- * The quotes are saved in the controller state once fetched.
769
+ * Uses the controller's request cache; callers manage the response in local state.
825
770
  *
826
771
  * @param options - The parameters for fetching quotes.
827
772
  * @param options.region - User's region code. If not provided, uses userRegion from state.
@@ -890,135 +835,27 @@ class RampsController extends base_controller_1.BaseController {
890
835
  redirectUrl: options.redirectUrl,
891
836
  action,
892
837
  };
893
- const response = await this.executeRequest(cacheKey, async () => {
838
+ return this.executeRequest(cacheKey, async () => {
894
839
  return this.messenger.call('RampsService:getQuotes', params);
895
840
  }, {
896
841
  forceRefresh: options.forceRefresh,
897
842
  ttl: options.ttl ?? DEFAULT_QUOTES_TTL,
898
- resourceType: 'quotes',
899
- isResultCurrent: () => __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_isRegionCurrent).call(this, normalizedRegion),
900
- });
901
- this.update((state) => {
902
- const userRegionCode = state.userRegion?.regionCode;
903
- if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
904
- state.quotes.data = response;
905
- }
906
- });
907
- return response;
908
- }
909
- /**
910
- * Starts automatic quote polling with a 15-second refresh interval.
911
- * Fetches quotes immediately and then every 15 seconds.
912
- * If the response contains exactly one quote, it is auto-selected.
913
- * If multiple quotes are returned, the existing selection is preserved if still valid.
914
- *
915
- * Returns early (no-op) if the selected payment method is not yet set,
916
- * allowing callers to invoke this before payment-method selection is finalized.
917
- *
918
- * @param options - Parameters for fetching quotes.
919
- * @param options.walletAddress - The destination wallet address.
920
- * @param options.amount - The amount (in fiat for buy, crypto for sell).
921
- * @param options.redirectUrl - Optional redirect URL after order completion.
922
- * @throws If required dependencies (region, token, provider) are not set.
923
- */
924
- startQuotePolling(options) {
925
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_requireRegion).call(this);
926
- const token = this.state.tokens.selected;
927
- const provider = this.state.providers.selected;
928
- const paymentMethod = this.state.paymentMethods.selected;
929
- if (!token) {
930
- throw new Error('Token is required. Cannot start quote polling without a selected token.');
931
- }
932
- if (!provider) {
933
- throw new Error('Provider is required. Cannot start quote polling without a selected provider.');
934
- }
935
- if (!paymentMethod) {
936
- return;
937
- }
938
- // Stop any existing polling first
939
- this.stopQuotePolling();
940
- // Store options for restarts (must be after stop to avoid being cleared)
941
- __classPrivateFieldSet(this, _RampsController_quotePollingOptions, options, "f");
942
- // Define the fetch function
943
- const fetchQuotes = () => {
944
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getQuotes({
945
- assetId: token.assetId,
946
- amount: options.amount,
947
- walletAddress: options.walletAddress,
948
- redirectUrl: options.redirectUrl,
949
- paymentMethods: [paymentMethod.id],
950
- providers: [provider.id],
951
- forceRefresh: true,
952
- }).then((response) => {
953
- let newSelectedQuote = null;
954
- // Auto-select logic: only when exactly one quote is returned
955
- this.update((state) => {
956
- if (response.success.length === 1) {
957
- newSelectedQuote = response.success[0];
958
- state.quotes.selected = newSelectedQuote;
959
- }
960
- else {
961
- // Keep existing selection if still valid, but update with fresh data
962
- const currentSelection = state.quotes.selected;
963
- if (currentSelection) {
964
- const freshQuote = response.success.find((quote) => quote.provider === currentSelection.provider &&
965
- quote.quote.paymentMethod ===
966
- currentSelection.quote.paymentMethod);
967
- newSelectedQuote = freshQuote ?? null;
968
- state.quotes.selected = newSelectedQuote;
969
- }
970
- }
971
- });
972
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_syncWidgetUrl).call(this, newSelectedQuote);
973
- return undefined;
974
- }));
975
- };
976
- // Fetch immediately
977
- fetchQuotes();
978
- // Set up 15-second polling
979
- __classPrivateFieldSet(this, _RampsController_quotePollingInterval, setInterval(fetchQuotes, 15000), "f");
980
- }
981
- /**
982
- * Stops automatic quote polling.
983
- * Does not clear quotes data or selection, only stops the interval.
984
- */
985
- stopQuotePolling() {
986
- if (__classPrivateFieldGet(this, _RampsController_quotePollingInterval, "f") !== null) {
987
- clearInterval(__classPrivateFieldGet(this, _RampsController_quotePollingInterval, "f"));
988
- __classPrivateFieldSet(this, _RampsController_quotePollingInterval, null, "f");
989
- }
990
- __classPrivateFieldSet(this, _RampsController_quotePollingOptions, null, "f");
991
- }
992
- /**
993
- * Manually sets the selected quote.
994
- * Automatically triggers a widget URL fetch for the new quote.
995
- *
996
- * @param quote - The quote to select, or null to clear the selection.
997
- */
998
- setSelectedQuote(quote) {
999
- this.update((state) => {
1000
- state.quotes.selected = quote;
1001
843
  });
1002
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_syncWidgetUrl).call(this, quote);
1003
844
  }
1004
845
  /**
1005
846
  * Cleans up controller resources.
1006
- * Stops any active quote polling to prevent memory leaks.
1007
847
  * Should be called when the controller is no longer needed.
1008
848
  */
1009
849
  destroy() {
1010
- this.stopQuotePolling();
1011
850
  super.destroy();
1012
851
  }
1013
852
  /**
1014
853
  * Fetches the widget URL from a quote for redirect providers.
1015
854
  * Makes a request to the buyURL endpoint via the RampsService to get the
1016
- * actual provider widget URL, using the injected fetch and retry policy.
855
+ * actual provider widget URL.
1017
856
  *
1018
857
  * @param quote - The quote to fetch the widget URL from.
1019
858
  * @returns Promise resolving to the widget URL string, or null if not available.
1020
- * @deprecated Read `state.widgetUrl` instead. The widget URL is now automatically
1021
- * fetched and stored in state whenever the selected quote changes.
1022
859
  */
1023
860
  async getWidgetUrl(quote) {
1024
861
  const buyUrl = quote.quote?.buyURL;
@@ -1455,7 +1292,7 @@ class RampsController extends base_controller_1.BaseController {
1455
1292
  }
1456
1293
  }
1457
1294
  exports.RampsController = RampsController;
1458
- _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheMaxSize = new WeakMap(), _RampsController_pendingRequests = new WeakMap(), _RampsController_pendingResourceCount = new WeakMap(), _RampsController_quotePollingInterval = new WeakMap(), _RampsController_quotePollingOptions = new WeakMap(), _RampsController_instances = new WeakSet(), _RampsController_clearPendingResourceCountForDependentResources = function _RampsController_clearPendingResourceCountForDependentResources() {
1295
+ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheMaxSize = new WeakMap(), _RampsController_pendingRequests = new WeakMap(), _RampsController_pendingResourceCount = new WeakMap(), _RampsController_instances = new WeakSet(), _RampsController_clearPendingResourceCountForDependentResources = function _RampsController_clearPendingResourceCountForDependentResources() {
1459
1296
  for (const resourceType of DEPENDENT_RESOURCE_KEYS) {
1460
1297
  __classPrivateFieldGet(this, _RampsController_pendingResourceCount, "f").delete(resourceType);
1461
1298
  }
@@ -1478,24 +1315,11 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
1478
1315
  delete requests[cacheKey];
1479
1316
  });
1480
1317
  }, _RampsController_cleanupState = function _RampsController_cleanupState() {
1481
- this.stopQuotePolling();
1482
1318
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_abortDependentRequests).call(this);
1483
1319
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
1484
1320
  this.update((state) => resetDependentResources(state, { clearUserRegionData: true }));
1485
1321
  }, _RampsController_fireAndForget = function _RampsController_fireAndForget(promise) {
1486
1322
  promise.catch((_error) => undefined);
1487
- }, _RampsController_restartPollingIfActive = function _RampsController_restartPollingIfActive() {
1488
- if (__classPrivateFieldGet(this, _RampsController_quotePollingInterval, "f") !== null && __classPrivateFieldGet(this, _RampsController_quotePollingOptions, "f")) {
1489
- const options = __classPrivateFieldGet(this, _RampsController_quotePollingOptions, "f");
1490
- this.stopQuotePolling();
1491
- try {
1492
- this.startQuotePolling(options);
1493
- }
1494
- catch {
1495
- // Dependencies not met yet, polling will need to be manually restarted
1496
- // when dependencies are available
1497
- }
1498
- }
1499
1323
  }, _RampsController_requireRegion = function _RampsController_requireRegion() {
1500
1324
  const regionCode = this.state.userRegion?.regionCode;
1501
1325
  if (!regionCode) {
@@ -1551,39 +1375,6 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
1551
1375
  }
1552
1376
  }
1553
1377
  });
1554
- }, _RampsController_syncWidgetUrl = function _RampsController_syncWidgetUrl(quote) {
1555
- const buyUrl = quote?.quote?.buyURL;
1556
- if (!buyUrl) {
1557
- this.update((state) => {
1558
- resetWidgetUrl(state);
1559
- });
1560
- return;
1561
- }
1562
- if (this.state.widgetUrl.data === null) {
1563
- this.update((state) => {
1564
- state.widgetUrl.isLoading = true;
1565
- state.widgetUrl.error = null;
1566
- });
1567
- }
1568
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.messenger
1569
- .call('RampsService:getBuyWidgetUrl', buyUrl)
1570
- .then((buyWidget) => {
1571
- this.update((state) => {
1572
- state.widgetUrl.data = buyWidget;
1573
- state.widgetUrl.isLoading = false;
1574
- state.widgetUrl.error = null;
1575
- });
1576
- return undefined;
1577
- })
1578
- .catch((error) => {
1579
- this.update((state) => {
1580
- state.widgetUrl.isLoading = false;
1581
- state.widgetUrl.error =
1582
- error instanceof Error
1583
- ? error.message
1584
- : 'Failed to fetch widget URL';
1585
- });
1586
- }));
1587
1378
  }, _RampsController_syncTransakAuthOnError = function _RampsController_syncTransakAuthOnError(error) {
1588
1379
  if (error instanceof Error &&
1589
1380
  'httpStatus' in error &&