@metamask-previews/ramps-controller 5.1.0-preview-8a281087 → 5.1.0-preview-2aeb1204

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.
@@ -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_setUserRegionRefetchCount, _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_clearPendingResourceCountForDependentResources, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _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");
@@ -114,7 +114,7 @@ function createDefaultResourceState(data, selected = null) {
114
114
  */
115
115
  function getDefaultRampsControllerState() {
116
116
  return {
117
- userRegion: createDefaultResourceState(null),
117
+ userRegion: null,
118
118
  countries: createDefaultResourceState([]),
119
119
  providers: createDefaultResourceState([], null),
120
120
  tokens: createDefaultResourceState(null, null),
@@ -130,14 +130,12 @@ exports.getDefaultRampsControllerState = getDefaultRampsControllerState;
130
130
  *
131
131
  * @param state - The state object to mutate.
132
132
  * @param options - Options for the reset.
133
- * @param options.clearUserRegionData - When true, sets userRegion.data to null (e.g. for full cleanup).
133
+ * @param options.clearUserRegionData - When true, sets userRegion to null (e.g. for full cleanup).
134
134
  */
135
135
  function resetDependentResources(state, options) {
136
136
  if (options?.clearUserRegionData) {
137
- state.userRegion.data = null;
137
+ state.userRegion = null;
138
138
  }
139
- state.userRegion.isLoading = false;
140
- state.userRegion.error = null;
141
139
  state.providers.selected = null;
142
140
  state.providers.data = [];
143
141
  state.providers.isLoading = false;
@@ -262,11 +260,6 @@ class RampsController extends base_controller_1.BaseController {
262
260
  * Used so isLoading is only cleared when the last request for that resource finishes.
263
261
  */
264
262
  _RampsController_pendingResourceCount.set(this, new Map());
265
- /**
266
- * Count of in-flight setUserRegion refetch batches.
267
- * Used so userRegion.isLoading is only cleared when the last batch's refetches finish (avoids race when region is changed rapidly or when init() clears loading before refetches complete).
268
- */
269
- _RampsController_setUserRegionRefetchCount.set(this, 0);
270
263
  __classPrivateFieldSet(this, _RampsController_requestCacheTTL, requestCacheTTL, "f");
271
264
  __classPrivateFieldSet(this, _RampsController_requestCacheMaxSize, requestCacheMaxSize, "f");
272
265
  }
@@ -398,12 +391,6 @@ class RampsController extends base_controller_1.BaseController {
398
391
  * Sets the user's region manually (without fetching geolocation).
399
392
  * This allows users to override the detected region.
400
393
  *
401
- * Sets userRegion.isLoading to true while the region is being applied and
402
- * tokens/providers are refetched (when the region actually changes), so
403
- * the UI can show a loading indicator when called directly (e.g. from a
404
- * region selector). Clears loading when refetches complete or when no
405
- * refetch is needed.
406
- *
407
394
  * @param region - The region code to set (e.g., "US-CA").
408
395
  * @param options - Options for cache behavior.
409
396
  * @returns The user region object.
@@ -421,13 +408,10 @@ class RampsController extends base_controller_1.BaseController {
421
408
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_cleanupState).call(this);
422
409
  throw new Error(`Region "${normalizedRegion}" not found in countries data. Cannot set user region without valid country information.`);
423
410
  }
424
- const regionChanged = normalizedRegion !== this.state.userRegion.data?.regionCode;
411
+ const regionChanged = normalizedRegion !== this.state.userRegion?.regionCode;
425
412
  const needsRefetch = regionChanged ||
426
413
  !this.state.tokens.data ||
427
414
  this.state.providers.data.length === 0;
428
- if (needsRefetch) {
429
- __classPrivateFieldSet(this, _RampsController_setUserRegionRefetchCount, __classPrivateFieldGet(this, _RampsController_setUserRegionRefetchCount, "f") + 1, "f");
430
- }
431
415
  if (regionChanged) {
432
416
  __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_clearPendingResourceCountForDependentResources).call(this);
433
417
  }
@@ -435,29 +419,19 @@ class RampsController extends base_controller_1.BaseController {
435
419
  if (regionChanged) {
436
420
  resetDependentResources(state);
437
421
  }
438
- state.userRegion.data = userRegion;
422
+ state.userRegion = userRegion;
439
423
  });
440
424
  if (needsRefetch) {
441
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, 'userRegion', true);
442
- }
443
- // this code is needed to prevent race conditions in the unlikely event that the user's region is changed rapidly
444
- const refetchPromises = [];
445
- if (regionChanged || !this.state.tokens.data) {
446
- refetchPromises.push(this.getTokens(userRegion.regionCode, 'buy', options));
447
- }
448
- if (regionChanged || this.state.providers.data.length === 0) {
449
- refetchPromises.push(this.getProviders(userRegion.regionCode, options));
450
- }
451
- if (refetchPromises.length > 0) {
452
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, Promise.all(refetchPromises).finally(() => {
453
- __classPrivateFieldSet(this, _RampsController_setUserRegionRefetchCount, Math.max(0, __classPrivateFieldGet(this, _RampsController_setUserRegionRefetchCount, "f") - 1), "f");
454
- if (__classPrivateFieldGet(this, _RampsController_setUserRegionRefetchCount, "f") === 0) {
455
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, 'userRegion', false);
456
- }
457
- }));
458
- }
459
- else {
460
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, 'userRegion', false);
425
+ const refetchPromises = [];
426
+ if (regionChanged || !this.state.tokens.data) {
427
+ refetchPromises.push(this.getTokens(userRegion.regionCode, 'buy', options));
428
+ }
429
+ if (regionChanged || this.state.providers.data.length === 0) {
430
+ refetchPromises.push(this.getProviders(userRegion.regionCode, options));
431
+ }
432
+ if (refetchPromises.length > 0) {
433
+ __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_fireAndForget).call(this, Promise.all(refetchPromises));
434
+ }
461
435
  }
462
436
  return userRegion;
463
437
  }
@@ -483,7 +457,7 @@ class RampsController extends base_controller_1.BaseController {
483
457
  });
484
458
  return;
485
459
  }
486
- const regionCode = this.state.userRegion.data?.regionCode;
460
+ const regionCode = this.state.userRegion?.regionCode;
487
461
  if (!regionCode) {
488
462
  throw new Error('Region is required. Cannot set selected provider without valid region information.');
489
463
  }
@@ -513,31 +487,16 @@ class RampsController extends base_controller_1.BaseController {
513
487
  * @returns Promise that resolves when initialization is complete.
514
488
  */
515
489
  async init(options) {
516
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, 'userRegion', true);
517
- let setUserRegionCompleted = false;
518
- try {
519
- await this.getCountries(options);
520
- let regionCode = this.state.userRegion.data?.regionCode;
521
- regionCode ?? (regionCode = await this.messenger.call('RampsService:getGeolocation'));
522
- if (!regionCode) {
523
- throw new Error('Failed to fetch geolocation. Cannot initialize controller without valid region information.');
524
- }
525
- await this.setUserRegion(regionCode, options);
526
- setUserRegionCompleted = true;
527
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceError).call(this, 'userRegion', null);
528
- }
529
- catch (error) {
530
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceError).call(this, 'userRegion', error?.message ?? 'Unknown error');
531
- throw error;
532
- }
533
- finally {
534
- if (!setUserRegionCompleted) {
535
- __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_setResourceLoading).call(this, 'userRegion', false);
536
- }
490
+ await this.getCountries(options);
491
+ let regionCode = this.state.userRegion?.regionCode;
492
+ regionCode ?? (regionCode = await this.messenger.call('RampsService:getGeolocation'));
493
+ if (!regionCode) {
494
+ throw new Error('Failed to fetch geolocation. Cannot initialize controller without valid region information.');
537
495
  }
496
+ await this.setUserRegion(regionCode, options);
538
497
  }
539
498
  hydrateState(options) {
540
- const regionCode = this.state.userRegion.data?.regionCode;
499
+ const regionCode = this.state.userRegion?.regionCode;
541
500
  if (!regionCode) {
542
501
  throw new Error('Region code is required. Cannot hydrate state without valid region information.');
543
502
  }
@@ -573,7 +532,7 @@ class RampsController extends base_controller_1.BaseController {
573
532
  * @returns The tokens response containing topTokens and allTokens.
574
533
  */
575
534
  async getTokens(region, action = 'buy', options) {
576
- const regionToUse = region ?? this.state.userRegion.data?.regionCode;
535
+ const regionToUse = region ?? this.state.userRegion?.regionCode;
577
536
  if (!regionToUse) {
578
537
  throw new Error('Region is required. Either provide a region parameter or ensure userRegion is set in controller state.');
579
538
  }
@@ -590,11 +549,11 @@ class RampsController extends base_controller_1.BaseController {
590
549
  }, {
591
550
  ...options,
592
551
  resourceType: 'tokens',
593
- isResultCurrent: () => this.state.userRegion.data?.regionCode === undefined ||
594
- this.state.userRegion.data?.regionCode === normalizedRegion,
552
+ isResultCurrent: () => this.state.userRegion?.regionCode === undefined ||
553
+ this.state.userRegion?.regionCode === normalizedRegion,
595
554
  });
596
555
  this.update((state) => {
597
- const userRegionCode = state.userRegion.data?.regionCode;
556
+ const userRegionCode = state.userRegion?.regionCode;
598
557
  if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
599
558
  state.tokens.data = tokens;
600
559
  }
@@ -618,7 +577,7 @@ class RampsController extends base_controller_1.BaseController {
618
577
  });
619
578
  return;
620
579
  }
621
- const regionCode = this.state.userRegion.data?.regionCode;
580
+ const regionCode = this.state.userRegion?.regionCode;
622
581
  if (!regionCode) {
623
582
  throw new Error('Region is required. Cannot set selected token without valid region information.');
624
583
  }
@@ -651,7 +610,7 @@ class RampsController extends base_controller_1.BaseController {
651
610
  * @returns The providers response containing providers array.
652
611
  */
653
612
  async getProviders(region, options) {
654
- const regionToUse = region ?? this.state.userRegion.data?.regionCode;
613
+ const regionToUse = region ?? this.state.userRegion?.regionCode;
655
614
  if (!regionToUse) {
656
615
  throw new Error('Region is required. Either provide a region parameter or ensure userRegion is set in controller state.');
657
616
  }
@@ -673,11 +632,11 @@ class RampsController extends base_controller_1.BaseController {
673
632
  }, {
674
633
  ...options,
675
634
  resourceType: 'providers',
676
- isResultCurrent: () => this.state.userRegion.data?.regionCode === undefined ||
677
- this.state.userRegion.data?.regionCode === normalizedRegion,
635
+ isResultCurrent: () => this.state.userRegion?.regionCode === undefined ||
636
+ this.state.userRegion?.regionCode === normalizedRegion,
678
637
  });
679
638
  this.update((state) => {
680
- const userRegionCode = state.userRegion.data?.regionCode;
639
+ const userRegionCode = state.userRegion?.regionCode;
681
640
  if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
682
641
  state.providers.data = providers;
683
642
  }
@@ -696,8 +655,8 @@ class RampsController extends base_controller_1.BaseController {
696
655
  * @returns The payment methods response containing payments array.
697
656
  */
698
657
  async getPaymentMethods(region, options) {
699
- const regionCode = region ?? this.state.userRegion.data?.regionCode ?? null;
700
- const fiatToUse = options?.fiat ?? this.state.userRegion.data?.country?.currency ?? null;
658
+ const regionCode = region ?? this.state.userRegion?.regionCode ?? null;
659
+ const fiatToUse = options?.fiat ?? this.state.userRegion?.country?.currency ?? null;
701
660
  const assetIdToUse = options?.assetId ?? this.state.tokens.selected?.assetId ?? '';
702
661
  const providerToUse = options?.provider ?? this.state.providers.selected?.id ?? '';
703
662
  if (!regionCode) {
@@ -725,8 +684,8 @@ class RampsController extends base_controller_1.BaseController {
725
684
  ...options,
726
685
  resourceType: 'paymentMethods',
727
686
  isResultCurrent: () => {
728
- const regionMatch = this.state.userRegion.data?.regionCode === undefined ||
729
- this.state.userRegion.data?.regionCode === normalizedRegion;
687
+ const regionMatch = this.state.userRegion?.regionCode === undefined ||
688
+ this.state.userRegion?.regionCode === normalizedRegion;
730
689
  const tokenMatch = (this.state.tokens.selected?.assetId ?? '') === assetIdToUse;
731
690
  const providerMatch = (this.state.providers.selected?.id ?? '') === providerToUse;
732
691
  return regionMatch && tokenMatch && providerMatch;
@@ -796,8 +755,8 @@ class RampsController extends base_controller_1.BaseController {
796
755
  * @returns The quotes response containing success, sorted, error, and customActions.
797
756
  */
798
757
  async getQuotes(options) {
799
- const regionToUse = options.region ?? this.state.userRegion.data?.regionCode;
800
- const fiatToUse = options.fiat ?? this.state.userRegion.data?.country?.currency;
758
+ const regionToUse = options.region ?? this.state.userRegion?.regionCode;
759
+ const fiatToUse = options.fiat ?? this.state.userRegion?.country?.currency;
801
760
  const paymentMethodsToUse = options.paymentMethods ??
802
761
  this.state.paymentMethods.data.map((pm) => pm.id);
803
762
  const action = options.action ?? 'buy';
@@ -851,11 +810,11 @@ class RampsController extends base_controller_1.BaseController {
851
810
  forceRefresh: options.forceRefresh,
852
811
  ttl: options.ttl ?? DEFAULT_QUOTES_TTL,
853
812
  resourceType: 'quotes',
854
- isResultCurrent: () => this.state.userRegion.data?.regionCode === undefined ||
855
- this.state.userRegion.data?.regionCode === normalizedRegion,
813
+ isResultCurrent: () => this.state.userRegion?.regionCode === undefined ||
814
+ this.state.userRegion?.regionCode === normalizedRegion,
856
815
  });
857
816
  this.update((state) => {
858
- const userRegionCode = state.userRegion.data?.regionCode;
817
+ const userRegionCode = state.userRegion?.regionCode;
859
818
  if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
860
819
  state.quotes.data = response;
861
820
  }
@@ -874,9 +833,8 @@ class RampsController extends base_controller_1.BaseController {
874
833
  }
875
834
  }
876
835
  exports.RampsController = RampsController;
877
- _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheMaxSize = new WeakMap(), _RampsController_pendingRequests = new WeakMap(), _RampsController_pendingResourceCount = new WeakMap(), _RampsController_setUserRegionRefetchCount = new WeakMap(), _RampsController_instances = new WeakSet(), _RampsController_clearPendingResourceCountForDependentResources = function _RampsController_clearPendingResourceCountForDependentResources() {
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() {
878
837
  const types = [
879
- 'userRegion',
880
838
  'providers',
881
839
  'tokens',
882
840
  'paymentMethods',