@metamask-previews/ramps-controller 3.0.0-preview-fa81dffb → 4.0.0-preview-903db2da

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.
@@ -11,7 +11,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
11
  };
12
12
  var _RampsController_instances, _RampsController_requestCacheTTL, _RampsController_requestCacheMaxSize, _RampsController_pendingRequests, _RampsController_removeRequestState, _RampsController_updateRequestState;
13
13
  import { BaseController } from "@metamask/base-controller";
14
- import { DEFAULT_REQUEST_CACHE_TTL, DEFAULT_REQUEST_CACHE_MAX_SIZE, createCacheKey, isCacheExpired, createLoadingState, createSuccessState, createErrorState } from "./RequestCache.mjs";
14
+ import { DEFAULT_REQUEST_CACHE_TTL, DEFAULT_REQUEST_CACHE_MAX_SIZE, createCacheKey, isCacheExpired, createLoadingState, createSuccessState, createErrorState, RequestStatus } from "./RequestCache.mjs";
15
15
  // === GENERAL ===
16
16
  /**
17
17
  * The name of the {@link RampsController}, used to namespace the
@@ -35,7 +35,7 @@ const rampsControllerMetadata = {
35
35
  includeInStateLogs: true,
36
36
  usedInUi: true,
37
37
  },
38
- eligibility: {
38
+ providers: {
39
39
  persist: true,
40
40
  includeInDebugSnapshot: true,
41
41
  includeInStateLogs: true,
@@ -66,11 +66,64 @@ export function getDefaultRampsControllerState() {
66
66
  return {
67
67
  userRegion: null,
68
68
  preferredProvider: null,
69
- eligibility: null,
69
+ providers: [],
70
70
  tokens: null,
71
71
  requests: {},
72
72
  };
73
73
  }
74
+ // === HELPER FUNCTIONS ===
75
+ /**
76
+ * Finds a country and state from a region code string.
77
+ *
78
+ * @param regionCode - The region code (e.g., "us-ca" or "us").
79
+ * @param countries - Array of countries to search.
80
+ * @returns UserRegion object with country and state, or null if not found.
81
+ */
82
+ function findRegionFromCode(regionCode, countries) {
83
+ const normalizedCode = regionCode.toLowerCase().trim();
84
+ const parts = normalizedCode.split('-');
85
+ const countryCode = parts[0];
86
+ const stateCode = parts[1];
87
+ const country = countries.find((countryItem) => {
88
+ if (countryItem.isoCode?.toLowerCase() === countryCode) {
89
+ return true;
90
+ }
91
+ if (countryItem.id) {
92
+ const id = countryItem.id.toLowerCase();
93
+ if (id.startsWith('/regions/')) {
94
+ const extractedCode = id.replace('/regions/', '').split('/')[0];
95
+ return extractedCode === countryCode;
96
+ }
97
+ return id === countryCode || id.endsWith(`/${countryCode}`);
98
+ }
99
+ return false;
100
+ });
101
+ if (!country) {
102
+ return null;
103
+ }
104
+ let state = null;
105
+ if (stateCode && country.states) {
106
+ state =
107
+ country.states.find((stateItem) => {
108
+ if (stateItem.stateId?.toLowerCase() === stateCode) {
109
+ return true;
110
+ }
111
+ if (stateItem.id) {
112
+ const stateId = stateItem.id.toLowerCase();
113
+ if (stateId.includes(`-${stateCode}`) ||
114
+ stateId.endsWith(`/${stateCode}`)) {
115
+ return true;
116
+ }
117
+ }
118
+ return false;
119
+ }) ?? null;
120
+ }
121
+ return {
122
+ country,
123
+ state,
124
+ regionCode: normalizedCode,
125
+ };
126
+ }
74
127
  // === CONTROLLER DEFINITION ===
75
128
  /**
76
129
  * Manages cryptocurrency on/off ramps functionality.
@@ -204,73 +257,134 @@ export class RampsController extends BaseController {
204
257
  return this.state.requests[cacheKey];
205
258
  }
206
259
  /**
207
- * Updates the user's region by fetching geolocation and eligibility.
208
- * This method calls the RampsService to get the geolocation,
209
- * then automatically fetches eligibility for that region.
260
+ * Updates the user's region by fetching geolocation.
261
+ * This method calls the RampsService to get the geolocation.
210
262
  *
211
263
  * @param options - Options for cache behavior.
212
- * @returns The user region string.
264
+ * @returns The user region object.
213
265
  */
214
266
  async updateUserRegion(options) {
267
+ // If a userRegion already exists and forceRefresh is not requested,
268
+ // return it immediately without fetching geolocation.
269
+ // This ensures that once a region is set (either via geolocation or manual selection),
270
+ // it will not be overwritten by subsequent geolocation fetches.
271
+ if (this.state.userRegion && !options?.forceRefresh) {
272
+ return this.state.userRegion;
273
+ }
274
+ // When forceRefresh is true, clear the existing region, tokens, and providers before fetching
275
+ if (options?.forceRefresh) {
276
+ this.update((state) => {
277
+ state.userRegion = null;
278
+ state.tokens = null;
279
+ state.providers = [];
280
+ });
281
+ }
215
282
  const cacheKey = createCacheKey('updateUserRegion', []);
216
- const userRegion = await this.executeRequest(cacheKey, async () => {
283
+ const regionCode = await this.executeRequest(cacheKey, async () => {
217
284
  const result = await this.messenger.call('RampsService:getGeolocation');
218
285
  return result;
219
286
  }, options);
220
- const normalizedRegion = userRegion
221
- ? userRegion.toLowerCase().trim()
222
- : userRegion;
223
- this.update((state) => {
224
- state.userRegion = normalizedRegion;
225
- state.tokens = null;
226
- });
227
- if (normalizedRegion) {
228
- try {
229
- await this.updateEligibility(normalizedRegion, options);
230
- }
231
- catch {
287
+ if (!regionCode) {
288
+ this.update((state) => {
289
+ state.userRegion = null;
290
+ state.tokens = null;
291
+ state.providers = [];
292
+ });
293
+ return null;
294
+ }
295
+ const normalizedRegion = regionCode.toLowerCase().trim();
296
+ try {
297
+ const countries = await this.getCountries('buy', options);
298
+ const userRegion = findRegionFromCode(normalizedRegion, countries);
299
+ if (userRegion) {
232
300
  this.update((state) => {
233
- const currentUserRegion = state.userRegion?.toLowerCase().trim();
234
- if (currentUserRegion === normalizedRegion) {
235
- state.eligibility = null;
301
+ const regionChanged = state.userRegion?.regionCode !== userRegion.regionCode;
302
+ state.userRegion = userRegion;
303
+ // Clear tokens and providers when region changes
304
+ if (regionChanged) {
236
305
  state.tokens = null;
306
+ state.providers = [];
237
307
  }
238
308
  });
309
+ // Fetch providers for the new region
310
+ if (userRegion.regionCode) {
311
+ try {
312
+ await this.getProviders(userRegion.regionCode, options);
313
+ }
314
+ catch {
315
+ // Provider fetch failed - error state will be available via selectors
316
+ }
317
+ }
318
+ return userRegion;
239
319
  }
320
+ // Region not found in countries data
321
+ this.update((state) => {
322
+ state.userRegion = null;
323
+ state.tokens = null;
324
+ state.providers = [];
325
+ });
326
+ return null;
327
+ }
328
+ catch {
329
+ // If countries fetch fails, we can't create a valid UserRegion
330
+ // Return null to indicate we don't have valid country data
331
+ this.update((state) => {
332
+ state.userRegion = null;
333
+ state.tokens = null;
334
+ state.providers = [];
335
+ });
336
+ return null;
240
337
  }
241
- return normalizedRegion;
242
338
  }
243
339
  /**
244
340
  * Sets the user's region manually (without fetching geolocation).
245
341
  * This allows users to override the detected region.
246
342
  *
247
343
  * @param region - The region code to set (e.g., "US-CA").
248
- * @param options - Options for cache behavior when fetching eligibility.
249
- * @returns The eligibility information for the region.
344
+ * @param options - Options for cache behavior.
345
+ * @returns The user region object.
250
346
  */
251
347
  async setUserRegion(region, options) {
252
348
  const normalizedRegion = region.toLowerCase().trim();
253
- this.update((state) => {
254
- state.userRegion = normalizedRegion;
255
- state.tokens = null;
256
- });
257
349
  try {
258
- return await this.updateEligibility(normalizedRegion, options);
350
+ const countries = await this.getCountries('buy', options);
351
+ const userRegion = findRegionFromCode(normalizedRegion, countries);
352
+ if (userRegion) {
353
+ this.update((state) => {
354
+ state.userRegion = userRegion;
355
+ state.tokens = null;
356
+ state.providers = [];
357
+ });
358
+ // Fetch providers for the new region
359
+ try {
360
+ await this.getProviders(userRegion.regionCode, options);
361
+ }
362
+ catch {
363
+ // Provider fetch failed - error state will be available via selectors
364
+ }
365
+ return userRegion;
366
+ }
367
+ // Region not found in countries data
368
+ this.update((state) => {
369
+ state.userRegion = null;
370
+ state.tokens = null;
371
+ state.providers = [];
372
+ });
373
+ throw new Error(`Region "${normalizedRegion}" not found in countries data. Cannot set user region without valid country information.`);
259
374
  }
260
375
  catch (error) {
261
- // Eligibility fetch failed, but user region was successfully set.
262
- // Don't let eligibility errors prevent user region state from being updated.
263
- // Clear eligibility state to avoid showing stale data from a previous location.
264
- // Only clear if the region still matches to avoid race conditions where a newer
265
- // region change has already succeeded.
376
+ // If the error is "not found", re-throw it
377
+ // Otherwise, it's from countries fetch failure
378
+ if (error instanceof Error && error.message.includes('not found')) {
379
+ throw error;
380
+ }
381
+ // Countries fetch failed
266
382
  this.update((state) => {
267
- const currentUserRegion = state.userRegion?.toLowerCase().trim();
268
- if (currentUserRegion === normalizedRegion) {
269
- state.eligibility = null;
270
- state.tokens = null;
271
- }
383
+ state.userRegion = null;
384
+ state.tokens = null;
385
+ state.providers = [];
272
386
  });
273
- throw error;
387
+ throw new Error('Failed to fetch countries data. Cannot set user region without valid country information.');
274
388
  }
275
389
  }
276
390
  /**
@@ -287,8 +401,10 @@ export class RampsController extends BaseController {
287
401
  /**
288
402
  * Initializes the controller by fetching the user's region from geolocation.
289
403
  * This should be called once at app startup to set up the initial region.
290
- * After the region is set and eligibility is determined, tokens are fetched
291
- * and saved to state.
404
+ * After the region is set, tokens are fetched and saved to state.
405
+ *
406
+ * If a userRegion already exists (from persistence or manual selection),
407
+ * this method will skip geolocation fetch and only fetch tokens if needed.
292
408
  *
293
409
  * @param options - Options for cache behavior.
294
410
  * @returns Promise that resolves when initialization is complete.
@@ -300,40 +416,25 @@ export class RampsController extends BaseController {
300
416
  });
301
417
  if (userRegion) {
302
418
  try {
303
- await this.getTokens(userRegion, 'buy', options);
419
+ await this.getTokens(userRegion.regionCode, 'buy', options);
304
420
  }
305
421
  catch {
306
422
  // Token fetch failed - error state will be available via selectors
307
423
  }
308
- }
309
- }
310
- /**
311
- * Updates the eligibility information for a given region.
312
- *
313
- * @param isoCode - The ISO code for the region (e.g., "us", "fr", "us-ny").
314
- * @param options - Options for cache behavior.
315
- * @returns The eligibility information.
316
- */
317
- async updateEligibility(isoCode, options) {
318
- const normalizedIsoCode = isoCode.toLowerCase().trim();
319
- const cacheKey = createCacheKey('updateEligibility', [normalizedIsoCode]);
320
- const eligibility = await this.executeRequest(cacheKey, async () => {
321
- return this.messenger.call('RampsService:getEligibility', normalizedIsoCode);
322
- }, options);
323
- this.update((state) => {
324
- const userRegion = state.userRegion?.toLowerCase().trim();
325
- if (userRegion === undefined || userRegion === normalizedIsoCode) {
326
- state.eligibility = eligibility;
424
+ try {
425
+ await this.getProviders(userRegion.regionCode, options);
327
426
  }
328
- });
329
- return eligibility;
427
+ catch {
428
+ // Provider fetch failed - error state will be available via selectors
429
+ }
430
+ }
330
431
  }
331
432
  /**
332
433
  * Fetches the list of supported countries for a given ramp action.
333
434
  *
334
435
  * @param action - The ramp action type ('buy' or 'sell').
335
436
  * @param options - Options for cache behavior.
336
- * @returns An array of countries with their eligibility information.
437
+ * @returns An array of countries.
337
438
  */
338
439
  async getCountries(action = 'buy', options) {
339
440
  const cacheKey = createCacheKey('getCountries', [action]);
@@ -347,27 +448,135 @@ export class RampsController extends BaseController {
347
448
  *
348
449
  * @param region - The region code (e.g., "us", "fr", "us-ny"). If not provided, uses the user's region from controller state.
349
450
  * @param action - The ramp action type ('buy' or 'sell').
350
- * @param options - Options for cache behavior.
451
+ * @param options - Options for cache behavior and query filters.
452
+ * @param options.provider - Provider ID(s) to filter by.
351
453
  * @returns The tokens response containing topTokens and allTokens.
352
454
  */
353
455
  async getTokens(region, action = 'buy', options) {
354
- const regionToUse = region ?? this.state.userRegion;
456
+ const regionToUse = region ?? this.state.userRegion?.regionCode;
355
457
  if (!regionToUse) {
356
458
  throw new Error('Region is required. Either provide a region parameter or ensure userRegion is set in controller state.');
357
459
  }
358
460
  const normalizedRegion = regionToUse.toLowerCase().trim();
359
- const cacheKey = createCacheKey('getTokens', [normalizedRegion, action]);
461
+ const cacheKey = createCacheKey('getTokens', [
462
+ normalizedRegion,
463
+ action,
464
+ options?.provider,
465
+ ]);
360
466
  const tokens = await this.executeRequest(cacheKey, async () => {
361
- return this.messenger.call('RampsService:getTokens', normalizedRegion, action);
467
+ return this.messenger.call('RampsService:getTokens', normalizedRegion, action, {
468
+ provider: options?.provider,
469
+ });
362
470
  }, options);
363
471
  this.update((state) => {
364
- const userRegion = state.userRegion?.toLowerCase().trim();
365
- if (userRegion === undefined || userRegion === normalizedRegion) {
472
+ const userRegionCode = state.userRegion?.regionCode;
473
+ if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
366
474
  state.tokens = tokens;
367
475
  }
368
476
  });
369
477
  return tokens;
370
478
  }
479
+ /**
480
+ * Fetches the list of providers for a given region.
481
+ * The providers are saved in the controller state once fetched.
482
+ *
483
+ * @param region - The region code (e.g., "us", "fr", "us-ny"). If not provided, uses the user's region from controller state.
484
+ * @param options - Options for cache behavior and query filters.
485
+ * @param options.provider - Provider ID(s) to filter by.
486
+ * @param options.crypto - Crypto currency ID(s) to filter by.
487
+ * @param options.fiat - Fiat currency ID(s) to filter by.
488
+ * @param options.payments - Payment method ID(s) to filter by.
489
+ * @returns The providers response containing providers array.
490
+ */
491
+ async getProviders(region, options) {
492
+ const regionToUse = region ?? this.state.userRegion?.regionCode;
493
+ if (!regionToUse) {
494
+ throw new Error('Region is required. Either provide a region parameter or ensure userRegion is set in controller state.');
495
+ }
496
+ const normalizedRegion = regionToUse.toLowerCase().trim();
497
+ const cacheKey = createCacheKey('getProviders', [
498
+ normalizedRegion,
499
+ options?.provider,
500
+ options?.crypto,
501
+ options?.fiat,
502
+ options?.payments,
503
+ ]);
504
+ const { providers } = await this.executeRequest(cacheKey, async () => {
505
+ return this.messenger.call('RampsService:getProviders', normalizedRegion, {
506
+ provider: options?.provider,
507
+ crypto: options?.crypto,
508
+ fiat: options?.fiat,
509
+ payments: options?.payments,
510
+ });
511
+ }, options);
512
+ this.update((state) => {
513
+ const userRegionCode = state.userRegion?.regionCode;
514
+ if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
515
+ state.providers = providers;
516
+ }
517
+ });
518
+ return { providers };
519
+ }
520
+ // ============================================================
521
+ // Sync Trigger Methods
522
+ // These fire-and-forget methods are for use in React effects.
523
+ // Errors are stored in state and available via selectors.
524
+ // ============================================================
525
+ /**
526
+ * Triggers a user region update without throwing.
527
+ *
528
+ * @param options - Options for cache behavior.
529
+ */
530
+ triggerUpdateUserRegion(options) {
531
+ this.updateUserRegion(options).catch(() => {
532
+ // Error stored in state
533
+ });
534
+ }
535
+ /**
536
+ * Triggers setting the user region without throwing.
537
+ *
538
+ * @param region - The region code to set (e.g., "US-CA").
539
+ * @param options - Options for cache behavior.
540
+ */
541
+ triggerSetUserRegion(region, options) {
542
+ this.setUserRegion(region, options).catch(() => {
543
+ // Error stored in state
544
+ });
545
+ }
546
+ /**
547
+ * Triggers fetching countries without throwing.
548
+ *
549
+ * @param action - The ramp action type ('buy' or 'sell').
550
+ * @param options - Options for cache behavior.
551
+ */
552
+ triggerGetCountries(action = 'buy', options) {
553
+ this.getCountries(action, options).catch(() => {
554
+ // Error stored in state
555
+ });
556
+ }
557
+ /**
558
+ * Triggers fetching tokens without throwing.
559
+ *
560
+ * @param region - The region code. If not provided, uses userRegion from state.
561
+ * @param action - The ramp action type ('buy' or 'sell').
562
+ * @param options - Options for cache behavior.
563
+ */
564
+ triggerGetTokens(region, action = 'buy', options) {
565
+ this.getTokens(region, action, options).catch(() => {
566
+ // Error stored in state
567
+ });
568
+ }
569
+ /**
570
+ * Triggers fetching providers without throwing.
571
+ *
572
+ * @param region - The region code. If not provided, uses userRegion from state.
573
+ * @param options - Options for cache behavior and query filters.
574
+ */
575
+ triggerGetProviders(region, options) {
576
+ this.getProviders(region, options).catch(() => {
577
+ // Error stored in state
578
+ });
579
+ }
371
580
  }
372
581
  _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheMaxSize = new WeakMap(), _RampsController_pendingRequests = new WeakMap(), _RampsController_instances = new WeakSet(), _RampsController_removeRequestState = function _RampsController_removeRequestState(cacheKey) {
373
582
  this.update((state) => {
@@ -376,20 +585,32 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
376
585
  });
377
586
  }, _RampsController_updateRequestState = function _RampsController_updateRequestState(cacheKey, requestState) {
378
587
  const maxSize = __classPrivateFieldGet(this, _RampsController_requestCacheMaxSize, "f");
588
+ const ttl = __classPrivateFieldGet(this, _RampsController_requestCacheTTL, "f");
379
589
  this.update((state) => {
380
590
  const requests = state.requests;
381
591
  requests[cacheKey] = requestState;
382
- // Evict oldest entries if cache exceeds max size
592
+ // Evict expired entries based on TTL
593
+ // Only evict SUCCESS states that have exceeded their TTL
383
594
  const keys = Object.keys(requests);
384
- if (keys.length > maxSize) {
595
+ for (const key of keys) {
596
+ const entry = requests[key];
597
+ if (entry &&
598
+ entry.status === RequestStatus.SUCCESS &&
599
+ isCacheExpired(entry, ttl)) {
600
+ delete requests[key];
601
+ }
602
+ }
603
+ // Evict oldest entries if cache still exceeds max size
604
+ const remainingKeys = Object.keys(requests);
605
+ if (remainingKeys.length > maxSize) {
385
606
  // Sort by timestamp (oldest first)
386
- const sortedKeys = keys.sort((a, b) => {
607
+ const sortedKeys = remainingKeys.sort((a, b) => {
387
608
  const aTime = requests[a]?.timestamp ?? 0;
388
609
  const bTime = requests[b]?.timestamp ?? 0;
389
610
  return aTime - bTime;
390
611
  });
391
612
  // Remove oldest entries until we're under the limit
392
- const entriesToRemove = keys.length - maxSize;
613
+ const entriesToRemove = remainingKeys.length - maxSize;
393
614
  for (let i = 0; i < entriesToRemove; i++) {
394
615
  const keyToRemove = sortedKeys[i];
395
616
  if (keyToRemove) {