@metamask-previews/ramps-controller 10.1.0-preview-968b2b4f4 → 10.2.0-preview-93519965c

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,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Add `status` field to `ResourceState` to distinguish between uninitialized and empty-fetched states ([#8116](https://github.com/MetaMask/core/pull/8116))
13
+
14
+ ## [10.2.0]
15
+
16
+ ### Fixed
17
+
18
+ - `setSelectedProvider` no longer fetches payment methods when the selected token is explicitly not supported by the new provider, preventing empty payment method state with no user feedback ([#8103](https://github.com/MetaMask/core/pull/8103))
19
+
10
20
  ## [10.1.0]
11
21
 
12
22
  ### Added
@@ -192,7 +202,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
192
202
  - Add `OnRampService` for interacting with the OnRamp API
193
203
  - Add geolocation detection via IP address lookup
194
204
 
195
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@10.1.0...HEAD
205
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@10.2.0...HEAD
206
+ [10.2.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@10.1.0...@metamask/ramps-controller@10.2.0
196
207
  [10.1.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@10.0.0...@metamask/ramps-controller@10.1.0
197
208
  [10.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@9.0.0...@metamask/ramps-controller@10.0.0
198
209
  [9.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@8.1.0...@metamask/ramps-controller@9.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_orderPollingMeta, _RampsController_orderPollingTimer, _RampsController_isPolling, _RampsController_clearPendingResourceCountForDependentResources, _RampsController_abortDependentRequests, _RampsController_registerActionHandlers, _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_refreshOrder, _RampsController_pollPendingOrders, _RampsController_syncTransakAuthOnError;
13
+ var _RampsController_instances, _RampsController_requestCacheTTL, _RampsController_requestCacheMaxSize, _RampsController_pendingRequests, _RampsController_pendingResourceCount, _RampsController_orderPollingMeta, _RampsController_orderPollingTimer, _RampsController_isPolling, _RampsController_clearPendingResourceCountForDependentResources, _RampsController_abortDependentRequests, _RampsController_registerActionHandlers, _RampsController_mutateRequests, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _RampsController_requireRegion, _RampsController_isRegionCurrent, _RampsController_isTokenCurrent, _RampsController_isProviderCurrent, _RampsController_updateResourceFields, _RampsController_setResourceError, _RampsController_setResourceLoadingAndStatus, _RampsController_updateRequestState, _RampsController_refreshOrder, _RampsController_pollPendingOrders, _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");
@@ -136,6 +136,7 @@ function createDefaultResourceState(data, selected = null) {
136
136
  selected,
137
137
  isLoading: false,
138
138
  error: null,
139
+ status: RequestCache_1.RequestStatus.IDLE,
139
140
  };
140
141
  }
141
142
  /**
@@ -179,6 +180,7 @@ function resetResource(state, resourceType, defaultResource) {
179
180
  resource.selected = def.selected;
180
181
  resource.isLoading = def.isLoading;
181
182
  resource.error = def.error;
183
+ resource.status = def.status;
182
184
  }
183
185
  /**
184
186
  * Resets region-dependent resources (userRegion, providers, tokens, paymentMethods).
@@ -382,11 +384,12 @@ class RampsController extends base_controller_1.BaseController {
382
384
  const count = __classPrivateFieldGet(this, _RampsController_pendingResourceCount, "f").get(resourceType) ?? 0;
383
385
  __classPrivateFieldGet(this, _RampsController_pendingResourceCount, "f").set(resourceType, count + 1);
384
386
  if (count === 0) {
385
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, resourceType, true);
387
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoadingAndStatus).call(this, resourceType, true, RequestCache_1.RequestStatus.LOADING);
386
388
  }
387
389
  }
388
390
  // Create the fetch promise
389
391
  const promise = (async () => {
392
+ let terminalStatus;
390
393
  try {
391
394
  const data = await fetcher(abortController.signal);
392
395
  if (abortController.signal.aborted) {
@@ -397,6 +400,7 @@ class RampsController extends base_controller_1.BaseController {
397
400
  const isCurrent = !options?.isResultCurrent || options.isResultCurrent();
398
401
  if (isCurrent) {
399
402
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceError).call(this, resourceType, null);
403
+ terminalStatus = RequestCache_1.RequestStatus.SUCCESS;
400
404
  }
401
405
  }
402
406
  return data;
@@ -411,6 +415,7 @@ class RampsController extends base_controller_1.BaseController {
411
415
  const isCurrent = !options?.isResultCurrent || options.isResultCurrent();
412
416
  if (isCurrent) {
413
417
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceError).call(this, resourceType, errorMessage);
418
+ terminalStatus = RequestCache_1.RequestStatus.ERROR;
414
419
  }
415
420
  }
416
421
  throw error;
@@ -426,7 +431,7 @@ class RampsController extends base_controller_1.BaseController {
426
431
  const next = Math.max(0, count - 1);
427
432
  if (next === 0) {
428
433
  __classPrivateFieldGet(this, _RampsController_pendingResourceCount, "f").delete(resourceType);
429
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, resourceType, false);
434
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoadingAndStatus).call(this, resourceType, false, terminalStatus ?? RequestCache_1.RequestStatus.IDLE);
430
435
  }
431
436
  else {
432
437
  __classPrivateFieldGet(this, _RampsController_pendingResourceCount, "f").set(resourceType, next);
@@ -545,11 +550,21 @@ class RampsController extends base_controller_1.BaseController {
545
550
  if (!provider) {
546
551
  throw new Error(`Provider with ID "${providerId}" not found in available providers.`);
547
552
  }
553
+ const selectedToken = this.state.tokens.selected;
554
+ const supportedCryptos = provider.supportedCryptoCurrencies;
555
+ // Only fetch payment methods if the selected token is supported by the new
556
+ // provider. If it isn't, the payment methods request would fail or return
557
+ // empty for the wrong reason; the UI will show the Token Not Available modal
558
+ // so the user can change token or pick a different provider.
559
+ const assetId = selectedToken?.assetId;
560
+ const tokenSupportedByProvider = !(assetId && supportedCryptos?.[assetId] === false);
548
561
  this.update((state) => {
549
562
  state.providers.selected = provider;
550
563
  resetResource(state, 'paymentMethods');
551
564
  });
552
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }));
565
+ if (tokenSupportedByProvider) {
566
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, this.getPaymentMethods(regionCode, { provider: provider.id }));
567
+ }
553
568
  }
554
569
  /**
555
570
  * Initializes the controller by fetching the user's region from geolocation.
@@ -949,7 +964,7 @@ class RampsController extends base_controller_1.BaseController {
949
964
  }
950
965
  try {
951
966
  const buyWidget = await this.messenger.call('RampsService:getBuyWidgetUrl', buyUrl);
952
- return buyWidget.url ?? null;
967
+ return buyWidget.url;
953
968
  }
954
969
  catch {
955
970
  return null;
@@ -1434,17 +1449,19 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
1434
1449
  }, _RampsController_isProviderCurrent = function _RampsController_isProviderCurrent(normalizedProviderId) {
1435
1450
  const current = this.state.providers.selected?.id ?? '';
1436
1451
  return current === normalizedProviderId;
1437
- }, _RampsController_updateResourceField = function _RampsController_updateResourceField(resourceType, field, value) {
1452
+ }, _RampsController_updateResourceFields = function _RampsController_updateResourceFields(resourceType, fields) {
1438
1453
  this.update((state) => {
1439
1454
  const resource = state[resourceType];
1440
1455
  if (resource) {
1441
- resource[field] = value;
1456
+ for (const [field, value] of Object.entries(fields)) {
1457
+ resource[field] = value;
1458
+ }
1442
1459
  }
1443
1460
  });
1444
- }, _RampsController_setResourceLoading = function _RampsController_setResourceLoading(resourceType, loading) {
1445
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_updateResourceField).call(this, resourceType, 'isLoading', loading);
1446
1461
  }, _RampsController_setResourceError = function _RampsController_setResourceError(resourceType, error) {
1447
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_updateResourceField).call(this, resourceType, 'error', error);
1462
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_updateResourceFields).call(this, resourceType, { error });
1463
+ }, _RampsController_setResourceLoadingAndStatus = function _RampsController_setResourceLoadingAndStatus(resourceType, loading, status) {
1464
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_updateResourceFields).call(this, resourceType, { isLoading: loading, status });
1448
1465
  }, _RampsController_updateRequestState = function _RampsController_updateRequestState(cacheKey, requestState) {
1449
1466
  const maxSize = __classPrivateFieldGet(this, _RampsController_requestCacheMaxSize, "f");
1450
1467
  const ttl = __classPrivateFieldGet(this, _RampsController_requestCacheTTL, "f");