@metamask-previews/ramps-controller 9.0.0-preview-e117c8109 → 9.0.0-preview-32ed9958c

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,16 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ### Changed
11
-
12
- - Improve `TransakService` error handling ([#8010](https://github.com/MetaMask/core/pull/8010))
13
- - **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))
14
-
15
- ### Removed
16
-
17
- - Remove `stopQuotePolling()` method (no interval to stop) ([#7999](https://github.com/MetaMask/core/pull/7999))
18
- - Remove internal polling restart logic (`#restartPollingIfActive`) from `setSelectedProvider`, `setSelectedToken`, and `setSelectedPaymentMethod` ([#7999](https://github.com/MetaMask/core/pull/7999))
19
-
20
10
  ## [9.0.0]
21
11
 
22
12
  ### Added
@@ -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_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_syncWidgetUrl, _RampsController_syncTransakAuthOnError;
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;
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");
@@ -326,6 +326,16 @@ class RampsController extends base_controller_1.BaseController {
326
326
  * Used so isLoading is only cleared when the last request for that resource finishes.
327
327
  */
328
328
  _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);
329
339
  __classPrivateFieldSet(this, _RampsController_requestCacheTTL, requestCacheTTL, "f");
330
340
  __classPrivateFieldSet(this, _RampsController_requestCacheMaxSize, requestCacheMaxSize, "f");
331
341
  }
@@ -496,6 +506,7 @@ class RampsController extends base_controller_1.BaseController {
496
506
  if (regionChanged) {
497
507
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_abortDependentRequests).call(this);
498
508
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
509
+ this.stopQuotePolling();
499
510
  }
500
511
  this.update((state) => {
501
512
  if (regionChanged) {
@@ -532,6 +543,7 @@ class RampsController extends base_controller_1.BaseController {
532
543
  */
533
544
  setSelectedProvider(providerId) {
534
545
  if (providerId === null) {
546
+ this.stopQuotePolling();
535
547
  this.update((state) => {
536
548
  state.providers.selected = null;
537
549
  resetResource(state, 'paymentMethods');
@@ -553,7 +565,11 @@ class RampsController extends base_controller_1.BaseController {
553
565
  state.quotes.selected = null;
554
566
  resetWidgetUrl(state);
555
567
  });
556
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }));
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
+ }));
557
573
  }
558
574
  /**
559
575
  * Initializes the controller by fetching the user's region from geolocation.
@@ -642,6 +658,7 @@ class RampsController extends base_controller_1.BaseController {
642
658
  */
643
659
  setSelectedToken(assetId) {
644
660
  if (!assetId) {
661
+ this.stopQuotePolling();
645
662
  this.update((state) => {
646
663
  state.tokens.selected = null;
647
664
  resetResource(state, 'paymentMethods');
@@ -664,7 +681,10 @@ class RampsController extends base_controller_1.BaseController {
664
681
  state.quotes.selected = null;
665
682
  resetWidgetUrl(state);
666
683
  });
667
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { assetId: token.assetId }));
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
+ }));
668
688
  }
669
689
  /**
670
690
  * Fetches the list of providers for a given region.
@@ -796,6 +816,8 @@ class RampsController extends base_controller_1.BaseController {
796
816
  this.update((state) => {
797
817
  state.paymentMethods.selected = paymentMethod;
798
818
  });
819
+ // Restart quote polling if active
820
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
799
821
  }
800
822
  /**
801
823
  * Fetches quotes from all providers for a given set of parameters.
@@ -885,7 +907,8 @@ class RampsController extends base_controller_1.BaseController {
885
907
  return response;
886
908
  }
887
909
  /**
888
- * Fetches quotes for the currently selected token, provider, and payment method.
910
+ * Starts automatic quote polling with a 15-second refresh interval.
911
+ * Fetches quotes immediately and then every 15 seconds.
889
912
  * If the response contains exactly one quote, it is auto-selected.
890
913
  * If multiple quotes are returned, the existing selection is preserved if still valid.
891
914
  *
@@ -898,49 +921,73 @@ class RampsController extends base_controller_1.BaseController {
898
921
  * @param options.redirectUrl - Optional redirect URL after order completion.
899
922
  * @throws If required dependencies (region, token, provider) are not set.
900
923
  */
901
- fetchQuotesForSelection(options) {
924
+ startQuotePolling(options) {
902
925
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_requireRegion).call(this);
903
926
  const token = this.state.tokens.selected;
904
927
  const provider = this.state.providers.selected;
905
928
  const paymentMethod = this.state.paymentMethods.selected;
906
929
  if (!token) {
907
- throw new Error('Token is required. Cannot fetch quotes without a selected token.');
930
+ throw new Error('Token is required. Cannot start quote polling without a selected token.');
908
931
  }
909
932
  if (!provider) {
910
- throw new Error('Provider is required. Cannot fetch quotes without a selected provider.');
933
+ throw new Error('Provider is required. Cannot start quote polling without a selected provider.');
911
934
  }
912
935
  if (!paymentMethod) {
913
936
  return;
914
937
  }
915
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getQuotes({
916
- assetId: token.assetId,
917
- amount: options.amount,
918
- walletAddress: options.walletAddress,
919
- redirectUrl: options.redirectUrl,
920
- paymentMethods: [paymentMethod.id],
921
- providers: [provider.id],
922
- forceRefresh: true,
923
- }).then((response) => {
924
- let newSelectedQuote = null;
925
- this.update((state) => {
926
- if (response.success.length === 1) {
927
- newSelectedQuote = response.success[0];
928
- state.quotes.selected = newSelectedQuote;
929
- }
930
- else {
931
- const currentSelection = state.quotes.selected;
932
- if (currentSelection) {
933
- const freshQuote = response.success.find((quote) => quote.provider === currentSelection.provider &&
934
- quote.quote.paymentMethod ===
935
- currentSelection.quote.paymentMethod);
936
- newSelectedQuote = freshQuote ?? null;
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];
937
958
  state.quotes.selected = newSelectedQuote;
938
959
  }
939
- }
940
- });
941
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_syncWidgetUrl).call(this, newSelectedQuote);
942
- return undefined;
943
- }));
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");
944
991
  }
945
992
  /**
946
993
  * Manually sets the selected quote.
@@ -956,9 +1003,11 @@ class RampsController extends base_controller_1.BaseController {
956
1003
  }
957
1004
  /**
958
1005
  * Cleans up controller resources.
1006
+ * Stops any active quote polling to prevent memory leaks.
959
1007
  * Should be called when the controller is no longer needed.
960
1008
  */
961
1009
  destroy() {
1010
+ this.stopQuotePolling();
962
1011
  super.destroy();
963
1012
  }
964
1013
  /**
@@ -1406,7 +1455,7 @@ class RampsController extends base_controller_1.BaseController {
1406
1455
  }
1407
1456
  }
1408
1457
  exports.RampsController = RampsController;
1409
- _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() {
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() {
1410
1459
  for (const resourceType of DEPENDENT_RESOURCE_KEYS) {
1411
1460
  __classPrivateFieldGet(this, _RampsController_pendingResourceCount, "f").delete(resourceType);
1412
1461
  }
@@ -1429,11 +1478,24 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
1429
1478
  delete requests[cacheKey];
1430
1479
  });
1431
1480
  }, _RampsController_cleanupState = function _RampsController_cleanupState() {
1481
+ this.stopQuotePolling();
1432
1482
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_abortDependentRequests).call(this);
1433
1483
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
1434
1484
  this.update((state) => resetDependentResources(state, { clearUserRegionData: true }));
1435
1485
  }, _RampsController_fireAndForget = function _RampsController_fireAndForget(promise) {
1436
1486
  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
+ }
1437
1499
  }, _RampsController_requireRegion = function _RampsController_requireRegion() {
1438
1500
  const regionCode = this.state.userRegion?.regionCode;
1439
1501
  if (!regionCode) {