@metamask/ramps-controller 6.0.0 → 7.1.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,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [7.1.0]
11
+
12
+ ### Fixed
13
+
14
+ - Fixes quote race condition bug with missing payment method ([#7863](https://github.com/MetaMask/core/pull/7863))
15
+
16
+ ## [7.0.0]
17
+
18
+ ### Added
19
+
20
+ - Update payment method delay type to match API response structure ([#7845](https://github.com/MetaMask/core/pull/7845))
21
+ - Add automatic quote polling with `startQuotePolling()`, `stopQuotePolling()`, and `setSelectedQuote()` methods, with auto-selection when a single quote is returned ([#7824](https://github.com/MetaMask/core/pull/7824))
22
+
23
+ ### Changed
24
+
25
+ - **BREAKING:** Require provider selection for quote polling and update quotes API endpoint to `/v2/quotes` ([#7846](https://github.com/MetaMask/core/pull/7846))
26
+
10
27
  ## [6.0.0]
11
28
 
12
29
  ### Changed
@@ -46,14 +63,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
46
63
  ### Added
47
64
 
48
65
  - Add sync trigger methods to RampsController ([#7662](https://github.com/MetaMask/core/pull/7662))
49
-
50
66
  - Export `RampAction` type for `'buy' | 'sell'` ramp actions ([#7663](https://github.com/MetaMask/core/pull/7663))
51
67
  - Add payment methods support with `getPaymentMethods()` method, `paymentMethods` and `selectedPaymentMethod` state ([#7665](https://github.com/MetaMask/core/pull/7665))
52
68
 
53
69
  ### Changed
54
70
 
55
71
  - Evict expired cache entries based on TTL in addition to size-based eviction ([#7674](https://github.com/MetaMask/core/pull/7674))
56
-
57
72
  - Update `getTokens()` to use v2 API endpoint and support optional provider parameter ([#7664](https://github.com/MetaMask/core/pull/7664))
58
73
 
59
74
  ## [4.0.0]
@@ -61,19 +76,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
61
76
  ### Added
62
77
 
63
78
  - Add `preferredProvider` state and `setPreferredProvider()` method to RampsController ([#7617](https://github.com/MetaMask/core/pull/7617))
64
-
65
79
  - Export `UserRegion` type ([#7646](https://github.com/MetaMask/core/pull/7646))
66
-
67
80
  - Add `defaultAmount` and `quickAmounts` fields to the `Country` type ([#7645](https://github.com/MetaMask/core/pull/7645))
68
-
69
81
  - Add `providers` state and `getProviders()` method to RampsController. Providers are automatically fetched on init and when the region changes ([#7652](https://github.com/MetaMask/core/pull/7652))
70
82
 
71
83
  ### Changed
72
84
 
73
85
  - **BREAKING:** Change `userRegion` from `string | null` to `UserRegion | null`. Access region code via `userRegion.regionCode`. ([#7646](https://github.com/MetaMask/core/pull/7646))
74
-
75
86
  - Update `getCountries()` endpoint to use v2 API (`v2/regions/countries`) ([#7645](https://github.com/MetaMask/core/pull/7645))
76
-
77
87
  - Add `getApiPath()` helper function for versioned API paths with v2 default ([#7645](https://github.com/MetaMask/core/pull/7645))
78
88
 
79
89
  ### Removed
@@ -89,7 +99,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
89
99
  ### Changed
90
100
 
91
101
  - **BREAKING:** Rename `geolocation` to `userRegion` and `updateGeolocation()` to `updateUserRegion()` in RampsController ([#7563](https://github.com/MetaMask/core/pull/7563))
92
-
93
102
  - Bump `@metamask/controller-utils` from `^11.17.0` to `^11.18.0` ([#7583](https://github.com/MetaMask/core/pull/7583))
94
103
 
95
104
  ## [2.1.0]
@@ -97,11 +106,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
97
106
  ### Added
98
107
 
99
108
  - Add eligibility state ([#7539](https://github.com/MetaMask/core/pull/7539))
100
-
101
109
  - Add `createRequestSelector` utility function for creating memoized selectors for RampsController request states ([#7554](https://github.com/MetaMask/core/pull/7554))
102
-
103
110
  - Add request caching infrastructure with TTL, deduplication, and abort support ([#7536](https://github.com/MetaMask/core/pull/7536))
104
-
105
111
  - Add `init()` and `setUserRegion()` methods to RampsController ([#7563](https://github.com/MetaMask/core/pull/7563))
106
112
 
107
113
  ### Changed
@@ -128,7 +134,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
128
134
  - Add `OnRampService` for interacting with the OnRamp API
129
135
  - Add geolocation detection via IP address lookup
130
136
 
131
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@6.0.0...HEAD
137
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@7.1.0...HEAD
138
+ [7.1.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@7.0.0...@metamask/ramps-controller@7.1.0
139
+ [7.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@6.0.0...@metamask/ramps-controller@7.0.0
132
140
  [6.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@5.1.0...@metamask/ramps-controller@6.0.0
133
141
  [5.1.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@5.0.0...@metamask/ramps-controller@5.1.0
134
142
  [5.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@4.1.0...@metamask/ramps-controller@5.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_clearPendingResourceCountForDependentResources, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _RampsController_updateResourceField, _RampsController_setResourceLoading, _RampsController_setResourceError, _RampsController_updateRequestState;
13
+ var _RampsController_instances, _RampsController_requestCacheTTL, _RampsController_requestCacheMaxSize, _RampsController_pendingRequests, _RampsController_pendingResourceCount, _RampsController_quotePollingInterval, _RampsController_quotePollingOptions, _RampsController_clearPendingResourceCountForDependentResources, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _RampsController_restartPollingIfActive, _RampsController_updateResourceField, _RampsController_setResourceLoading, _RampsController_setResourceError, _RampsController_updateRequestState;
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");
@@ -119,7 +119,7 @@ function getDefaultRampsControllerState() {
119
119
  providers: createDefaultResourceState([], null),
120
120
  tokens: createDefaultResourceState(null, null),
121
121
  paymentMethods: createDefaultResourceState([], null),
122
- quotes: createDefaultResourceState(null),
122
+ quotes: createDefaultResourceState(null, null),
123
123
  requests: {},
124
124
  };
125
125
  }
@@ -149,6 +149,7 @@ function resetDependentResources(state, options) {
149
149
  state.paymentMethods.isLoading = false;
150
150
  state.paymentMethods.error = null;
151
151
  state.quotes.data = null;
152
+ state.quotes.selected = null;
152
153
  state.quotes.isLoading = false;
153
154
  state.quotes.error = null;
154
155
  }
@@ -260,6 +261,16 @@ class RampsController extends base_controller_1.BaseController {
260
261
  * Used so isLoading is only cleared when the last request for that resource finishes.
261
262
  */
262
263
  _RampsController_pendingResourceCount.set(this, new Map());
264
+ /**
265
+ * Interval ID for automatic quote polling.
266
+ * Set when startQuotePolling() is called, cleared when stopQuotePolling() is called.
267
+ */
268
+ _RampsController_quotePollingInterval.set(this, null);
269
+ /**
270
+ * Options used for quote polling (walletAddress, amount, redirectUrl).
271
+ * Stored so polling can be restarted when dependencies change.
272
+ */
273
+ _RampsController_quotePollingOptions.set(this, null);
263
274
  __classPrivateFieldSet(this, _RampsController_requestCacheTTL, requestCacheTTL, "f");
264
275
  __classPrivateFieldSet(this, _RampsController_requestCacheMaxSize, requestCacheMaxSize, "f");
265
276
  }
@@ -415,6 +426,9 @@ class RampsController extends base_controller_1.BaseController {
415
426
  if (regionChanged) {
416
427
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
417
428
  }
429
+ if (regionChanged) {
430
+ this.stopQuotePolling();
431
+ }
418
432
  this.update((state) => {
419
433
  if (regionChanged) {
420
434
  resetDependentResources(state);
@@ -450,6 +464,7 @@ class RampsController extends base_controller_1.BaseController {
450
464
  */
451
465
  setSelectedProvider(providerId) {
452
466
  if (providerId === null) {
467
+ this.stopQuotePolling();
453
468
  this.update((state) => {
454
469
  state.providers.selected = null;
455
470
  state.paymentMethods.data = [];
@@ -473,8 +488,13 @@ class RampsController extends base_controller_1.BaseController {
473
488
  state.providers.selected = provider;
474
489
  state.paymentMethods.data = [];
475
490
  state.paymentMethods.selected = null;
491
+ state.quotes.selected = null;
476
492
  });
477
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }));
493
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }).then(() => {
494
+ // Restart quote polling after payment methods are fetched
495
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
496
+ return undefined;
497
+ }));
478
498
  }
479
499
  /**
480
500
  * Initializes the controller by fetching the user's region from geolocation.
@@ -570,6 +590,7 @@ class RampsController extends base_controller_1.BaseController {
570
590
  */
571
591
  setSelectedToken(assetId) {
572
592
  if (!assetId) {
593
+ this.stopQuotePolling();
573
594
  this.update((state) => {
574
595
  state.tokens.selected = null;
575
596
  state.paymentMethods.data = [];
@@ -594,8 +615,12 @@ class RampsController extends base_controller_1.BaseController {
594
615
  state.tokens.selected = token;
595
616
  state.paymentMethods.data = [];
596
617
  state.paymentMethods.selected = null;
618
+ state.quotes.selected = null;
597
619
  });
598
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { assetId: token.assetId }));
620
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { assetId: token.assetId }).then(() => {
621
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
622
+ return undefined;
623
+ }));
599
624
  }
600
625
  /**
601
626
  * Fetches the list of providers for a given region.
@@ -735,6 +760,8 @@ class RampsController extends base_controller_1.BaseController {
735
760
  this.update((state) => {
736
761
  state.paymentMethods.selected = paymentMethod;
737
762
  });
763
+ // Restart quote polling if active
764
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_restartPollingIfActive).call(this);
738
765
  }
739
766
  /**
740
767
  * Fetches quotes from all providers for a given set of parameters.
@@ -821,6 +848,106 @@ class RampsController extends base_controller_1.BaseController {
821
848
  });
822
849
  return response;
823
850
  }
851
+ /**
852
+ * Starts automatic quote polling with a 15-second refresh interval.
853
+ * Fetches quotes immediately and then every 15 seconds.
854
+ * If the response contains exactly one quote, it is auto-selected.
855
+ * If multiple quotes are returned, the existing selection is preserved if still valid.
856
+ *
857
+ * @param options - Parameters for fetching quotes.
858
+ * @param options.walletAddress - The destination wallet address.
859
+ * @param options.amount - The amount (in fiat for buy, crypto for sell).
860
+ * @param options.redirectUrl - Optional redirect URL after order completion.
861
+ * @throws If required dependencies (region, token, provider, payment method) are not set.
862
+ */
863
+ startQuotePolling(options) {
864
+ // Validate required dependencies
865
+ const regionCode = this.state.userRegion?.regionCode;
866
+ const token = this.state.tokens.selected;
867
+ const provider = this.state.providers.selected;
868
+ const paymentMethod = this.state.paymentMethods.selected;
869
+ if (!regionCode) {
870
+ throw new Error('Region is required. Cannot start quote polling without valid region information.');
871
+ }
872
+ if (!token) {
873
+ throw new Error('Token is required. Cannot start quote polling without a selected token.');
874
+ }
875
+ if (!provider) {
876
+ throw new Error('Provider is required. Cannot start quote polling without a selected provider.');
877
+ }
878
+ if (!paymentMethod) {
879
+ return;
880
+ }
881
+ // Stop any existing polling first
882
+ this.stopQuotePolling();
883
+ // Store options for restarts (must be after stop to avoid being cleared)
884
+ __classPrivateFieldSet(this, _RampsController_quotePollingOptions, options, "f");
885
+ // Define the fetch function
886
+ const fetchQuotes = () => {
887
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getQuotes({
888
+ assetId: token.assetId,
889
+ amount: options.amount,
890
+ walletAddress: options.walletAddress,
891
+ redirectUrl: options.redirectUrl,
892
+ paymentMethods: [paymentMethod.id],
893
+ provider: provider.id,
894
+ forceRefresh: true,
895
+ }).then((response) => {
896
+ // Auto-select logic: only when exactly one quote is returned
897
+ this.update((state) => {
898
+ if (response.success.length === 1) {
899
+ state.quotes.selected = response.success[0];
900
+ }
901
+ else {
902
+ // Keep existing selection if still valid, but update with fresh data
903
+ const currentSelection = state.quotes.selected;
904
+ if (currentSelection) {
905
+ const freshQuote = response.success.find((quote) => quote.provider === currentSelection.provider &&
906
+ quote.quote.paymentMethod ===
907
+ currentSelection.quote.paymentMethod);
908
+ // Update with fresh quote data, or clear if no longer valid
909
+ state.quotes.selected = freshQuote ?? null;
910
+ }
911
+ }
912
+ });
913
+ return undefined;
914
+ }));
915
+ };
916
+ // Fetch immediately
917
+ fetchQuotes();
918
+ // Set up 15-second polling
919
+ __classPrivateFieldSet(this, _RampsController_quotePollingInterval, setInterval(fetchQuotes, 15000), "f");
920
+ }
921
+ /**
922
+ * Stops automatic quote polling.
923
+ * Does not clear quotes data or selection, only stops the interval.
924
+ */
925
+ stopQuotePolling() {
926
+ if (__classPrivateFieldGet(this, _RampsController_quotePollingInterval, "f") !== null) {
927
+ clearInterval(__classPrivateFieldGet(this, _RampsController_quotePollingInterval, "f"));
928
+ __classPrivateFieldSet(this, _RampsController_quotePollingInterval, null, "f");
929
+ }
930
+ __classPrivateFieldSet(this, _RampsController_quotePollingOptions, null, "f");
931
+ }
932
+ /**
933
+ * Manually sets the selected quote.
934
+ *
935
+ * @param quote - The quote to select, or null to clear the selection.
936
+ */
937
+ setSelectedQuote(quote) {
938
+ this.update((state) => {
939
+ state.quotes.selected = quote;
940
+ });
941
+ }
942
+ /**
943
+ * Cleans up controller resources.
944
+ * Stops any active quote polling to prevent memory leaks.
945
+ * Should be called when the controller is no longer needed.
946
+ */
947
+ destroy() {
948
+ this.stopQuotePolling();
949
+ super.destroy();
950
+ }
824
951
  /**
825
952
  * Extracts the widget URL from a quote for redirect providers.
826
953
  * Returns the widget URL if available, or null if the quote doesn't have one.
@@ -833,7 +960,7 @@ class RampsController extends base_controller_1.BaseController {
833
960
  }
834
961
  }
835
962
  exports.RampsController = RampsController;
836
- _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() {
963
+ _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() {
837
964
  const types = [
838
965
  'providers',
839
966
  'tokens',
@@ -849,12 +976,25 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
849
976
  delete requests[cacheKey];
850
977
  });
851
978
  }, _RampsController_cleanupState = function _RampsController_cleanupState() {
979
+ this.stopQuotePolling();
852
980
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
853
981
  this.update((state) => resetDependentResources(state, {
854
982
  clearUserRegionData: true,
855
983
  }));
856
984
  }, _RampsController_fireAndForget = function _RampsController_fireAndForget(promise) {
857
985
  promise.catch((_error) => undefined);
986
+ }, _RampsController_restartPollingIfActive = function _RampsController_restartPollingIfActive() {
987
+ if (__classPrivateFieldGet(this, _RampsController_quotePollingInterval, "f") !== null && __classPrivateFieldGet(this, _RampsController_quotePollingOptions, "f")) {
988
+ const options = __classPrivateFieldGet(this, _RampsController_quotePollingOptions, "f");
989
+ this.stopQuotePolling();
990
+ try {
991
+ this.startQuotePolling(options);
992
+ }
993
+ catch {
994
+ // Dependencies not met yet, polling will need to be manually restarted
995
+ // when dependencies are available
996
+ }
997
+ }
858
998
  }, _RampsController_updateResourceField = function _RampsController_updateResourceField(resourceType, field, value) {
859
999
  this.update((state) => {
860
1000
  const resource = state[resourceType];