@metamask-previews/ramps-controller 4.1.0-preview-3fd5e61f → 4.1.0-preview-e09bf49f
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 +0 -4
- package/dist/RampsController.cjs +37 -127
- package/dist/RampsController.cjs.map +1 -1
- package/dist/RampsController.d.cts +20 -36
- package/dist/RampsController.d.cts.map +1 -1
- package/dist/RampsController.d.mts +20 -36
- package/dist/RampsController.d.mts.map +1 -1
- package/dist/RampsController.mjs +37 -127
- package/dist/RampsController.mjs.map +1 -1
- package/dist/RampsService.cjs +8 -23
- package/dist/RampsService.cjs.map +1 -1
- package/dist/RampsService.d.cts +2 -8
- package/dist/RampsService.d.cts.map +1 -1
- package/dist/RampsService.d.mts +2 -8
- package/dist/RampsService.d.mts.map +1 -1
- package/dist/RampsService.mjs +8 -23
- package/dist/RampsService.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
-
import type { Country, TokensResponse, Provider, State, RampAction, PaymentMethod, PaymentMethodsResponse
|
|
4
|
+
import type { Country, TokensResponse, Provider, State, RampAction, PaymentMethod, PaymentMethodsResponse } from "./RampsService.cjs";
|
|
5
5
|
import type { RampsServiceGetGeolocationAction, RampsServiceGetCountriesAction, RampsServiceGetTokensAction, RampsServiceGetProvidersAction, RampsServiceGetPaymentMethodsAction } from "./RampsService-method-action-types.cjs";
|
|
6
6
|
import type { RequestCache as RequestCacheType, RequestState, ExecuteRequestOptions } from "./RequestCache.cjs";
|
|
7
7
|
/**
|
|
@@ -39,10 +39,10 @@ export type RampsControllerState = {
|
|
|
39
39
|
*/
|
|
40
40
|
userRegion: UserRegion | null;
|
|
41
41
|
/**
|
|
42
|
-
* The user's
|
|
42
|
+
* The user's preferred provider.
|
|
43
43
|
* Can be manually set by the user.
|
|
44
44
|
*/
|
|
45
|
-
|
|
45
|
+
preferredProvider: Provider | null;
|
|
46
46
|
/**
|
|
47
47
|
* List of countries available for ramp actions.
|
|
48
48
|
*/
|
|
@@ -56,11 +56,6 @@ export type RampsControllerState = {
|
|
|
56
56
|
* Contains topTokens and allTokens arrays.
|
|
57
57
|
*/
|
|
58
58
|
tokens: TokensResponse | null;
|
|
59
|
-
/**
|
|
60
|
-
* The user's selected token.
|
|
61
|
-
* When set, automatically fetches and sets payment methods for that token.
|
|
62
|
-
*/
|
|
63
|
-
selectedToken: RampsToken | null;
|
|
64
59
|
/**
|
|
65
60
|
* Payment methods available for the current context.
|
|
66
61
|
* Filtered by region, fiat, asset, and provider.
|
|
@@ -181,14 +176,12 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
181
176
|
*/
|
|
182
177
|
setUserRegion(region: string, options?: ExecuteRequestOptions): Promise<UserRegion>;
|
|
183
178
|
/**
|
|
184
|
-
* Sets the user's
|
|
185
|
-
*
|
|
186
|
-
* fetches payment methods for that provider.
|
|
179
|
+
* Sets the user's preferred provider.
|
|
180
|
+
* This allows users to set their preferred ramp provider.
|
|
187
181
|
*
|
|
188
|
-
* @param
|
|
189
|
-
* @throws If region is not set, providers are not loaded, or provider is not found.
|
|
182
|
+
* @param provider - The provider object to set.
|
|
190
183
|
*/
|
|
191
|
-
|
|
184
|
+
setPreferredProvider(provider: Provider | null): void;
|
|
192
185
|
/**
|
|
193
186
|
* Initializes the controller by fetching the user's region from geolocation.
|
|
194
187
|
* This should be called once at app startup to set up the initial region.
|
|
@@ -223,15 +216,6 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
223
216
|
getTokens(region?: string, action?: RampAction, options?: ExecuteRequestOptions & {
|
|
224
217
|
provider?: string | string[];
|
|
225
218
|
}): Promise<TokensResponse>;
|
|
226
|
-
/**
|
|
227
|
-
* Sets the user's selected token by asset ID.
|
|
228
|
-
* Looks up the token from the current tokens in state and automatically
|
|
229
|
-
* fetches payment methods for that token.
|
|
230
|
-
*
|
|
231
|
-
* @param assetId - The asset identifier in CAIP-19 format (e.g., "eip155:1/erc20:0x...").
|
|
232
|
-
* @throws If assetId is not provided, region is not set, tokens are not loaded, or token is not found.
|
|
233
|
-
*/
|
|
234
|
-
setSelectedToken(assetId: string): void;
|
|
235
219
|
/**
|
|
236
220
|
* Fetches the list of providers for a given region.
|
|
237
221
|
* The providers are saved in the controller state once fetched.
|
|
@@ -256,8 +240,8 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
256
240
|
* Fetches the list of payment methods for a given context.
|
|
257
241
|
* The payment methods are saved in the controller state once fetched.
|
|
258
242
|
*
|
|
259
|
-
* @param region - User's region code (e.g. "fr", "us-ny").
|
|
260
243
|
* @param options - Query parameters for filtering payment methods.
|
|
244
|
+
* @param options.region - User's region code. If not provided, uses the user's region from controller state.
|
|
261
245
|
* @param options.fiat - Fiat currency code (e.g., "usd"). If not provided, uses the user's region currency.
|
|
262
246
|
* @param options.assetId - CAIP-19 cryptocurrency identifier.
|
|
263
247
|
* @param options.provider - Provider ID path.
|
|
@@ -265,21 +249,20 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
265
249
|
* @param options.ttl - Custom TTL for this request.
|
|
266
250
|
* @returns The payment methods response containing payments array.
|
|
267
251
|
*/
|
|
268
|
-
getPaymentMethods(
|
|
252
|
+
getPaymentMethods(options: {
|
|
253
|
+
region?: string;
|
|
269
254
|
fiat?: string;
|
|
270
|
-
assetId
|
|
271
|
-
provider
|
|
255
|
+
assetId: string;
|
|
256
|
+
provider: string;
|
|
272
257
|
forceRefresh?: boolean;
|
|
273
258
|
ttl?: number;
|
|
274
259
|
}): Promise<PaymentMethodsResponse>;
|
|
275
260
|
/**
|
|
276
|
-
* Sets the user's selected payment method
|
|
277
|
-
* Looks up the payment method from the current payment methods in state.
|
|
261
|
+
* Sets the user's selected payment method.
|
|
278
262
|
*
|
|
279
|
-
* @param
|
|
280
|
-
* @throws If payment methods are not loaded or payment method is not found.
|
|
263
|
+
* @param paymentMethod - The payment method to select, or null to clear.
|
|
281
264
|
*/
|
|
282
|
-
setSelectedPaymentMethod(
|
|
265
|
+
setSelectedPaymentMethod(paymentMethod: PaymentMethod | null): void;
|
|
283
266
|
/**
|
|
284
267
|
* Triggers setting the user region without throwing.
|
|
285
268
|
*
|
|
@@ -316,18 +299,19 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
316
299
|
/**
|
|
317
300
|
* Triggers fetching payment methods without throwing.
|
|
318
301
|
*
|
|
319
|
-
* @param region - User's region code (e.g., "us", "fr", "us-ny").
|
|
320
302
|
* @param options - Query parameters for filtering payment methods.
|
|
303
|
+
* @param options.region - User's region code. If not provided, uses userRegion from state.
|
|
321
304
|
* @param options.fiat - Fiat currency code. If not provided, uses userRegion currency.
|
|
322
305
|
* @param options.assetId - CAIP-19 cryptocurrency identifier.
|
|
323
306
|
* @param options.provider - Provider ID path.
|
|
324
307
|
* @param options.forceRefresh - Whether to bypass cache.
|
|
325
308
|
* @param options.ttl - Custom TTL for this request.
|
|
326
309
|
*/
|
|
327
|
-
triggerGetPaymentMethods(
|
|
310
|
+
triggerGetPaymentMethods(options: {
|
|
311
|
+
region?: string;
|
|
328
312
|
fiat?: string;
|
|
329
|
-
assetId
|
|
330
|
-
provider
|
|
313
|
+
assetId: string;
|
|
314
|
+
provider: string;
|
|
331
315
|
forceRefresh?: boolean;
|
|
332
316
|
ttl?: number;
|
|
333
317
|
}): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RampsController.d.cts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,QAAQ,EACR,KAAK,EACL,UAAU,EACV,aAAa,EACb,sBAAsB,
|
|
1
|
+
{"version":3,"file":"RampsController.d.cts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,QAAQ,EACR,KAAK,EACL,UAAU,EACV,aAAa,EACb,sBAAsB,EACvB,2BAAuB;AACxB,OAAO,KAAK,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,2BAA2B,EAC3B,8BAA8B,EAC9B,mCAAmC,EACpC,+CAA2C;AAC5C,OAAO,KAAK,EACV,YAAY,IAAI,gBAAgB,EAChC,YAAY,EACZ,qBAAqB,EAEtB,2BAAuB;AAcxB;;;;GAIG;AACH,eAAO,MAAM,cAAc,oBAAoB,CAAC;AAIhD;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;;;OAKG;IACH,UAAU,EAAE,UAAU,GAAG,IAAI,CAAC;IAC9B;;;OAGG;IACH,iBAAiB,EAAE,QAAQ,GAAG,IAAI,CAAC;IACnC;;OAEG;IACH,SAAS,EAAE,OAAO,EAAE,CAAC;IACrB;;OAEG;IACH,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtB;;;OAGG;IACH,MAAM,EAAE,cAAc,GAAG,IAAI,CAAC;IAC9B;;;OAGG;IACH,cAAc,EAAE,aAAa,EAAE,CAAC;IACh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,MAAM,EAAE,CAAC;KAC9B,GACA,IAAI;IAMP;;;;;;;;;;OAUG;IACH,wBAAwB,CAAC,OAAO,EAAE;QAChC,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;QACjB,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,GAAG,IAAI;CAKT"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
-
import type { Country, TokensResponse, Provider, State, RampAction, PaymentMethod, PaymentMethodsResponse
|
|
4
|
+
import type { Country, TokensResponse, Provider, State, RampAction, PaymentMethod, PaymentMethodsResponse } from "./RampsService.mjs";
|
|
5
5
|
import type { RampsServiceGetGeolocationAction, RampsServiceGetCountriesAction, RampsServiceGetTokensAction, RampsServiceGetProvidersAction, RampsServiceGetPaymentMethodsAction } from "./RampsService-method-action-types.mjs";
|
|
6
6
|
import type { RequestCache as RequestCacheType, RequestState, ExecuteRequestOptions } from "./RequestCache.mjs";
|
|
7
7
|
/**
|
|
@@ -39,10 +39,10 @@ export type RampsControllerState = {
|
|
|
39
39
|
*/
|
|
40
40
|
userRegion: UserRegion | null;
|
|
41
41
|
/**
|
|
42
|
-
* The user's
|
|
42
|
+
* The user's preferred provider.
|
|
43
43
|
* Can be manually set by the user.
|
|
44
44
|
*/
|
|
45
|
-
|
|
45
|
+
preferredProvider: Provider | null;
|
|
46
46
|
/**
|
|
47
47
|
* List of countries available for ramp actions.
|
|
48
48
|
*/
|
|
@@ -56,11 +56,6 @@ export type RampsControllerState = {
|
|
|
56
56
|
* Contains topTokens and allTokens arrays.
|
|
57
57
|
*/
|
|
58
58
|
tokens: TokensResponse | null;
|
|
59
|
-
/**
|
|
60
|
-
* The user's selected token.
|
|
61
|
-
* When set, automatically fetches and sets payment methods for that token.
|
|
62
|
-
*/
|
|
63
|
-
selectedToken: RampsToken | null;
|
|
64
59
|
/**
|
|
65
60
|
* Payment methods available for the current context.
|
|
66
61
|
* Filtered by region, fiat, asset, and provider.
|
|
@@ -181,14 +176,12 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
181
176
|
*/
|
|
182
177
|
setUserRegion(region: string, options?: ExecuteRequestOptions): Promise<UserRegion>;
|
|
183
178
|
/**
|
|
184
|
-
* Sets the user's
|
|
185
|
-
*
|
|
186
|
-
* fetches payment methods for that provider.
|
|
179
|
+
* Sets the user's preferred provider.
|
|
180
|
+
* This allows users to set their preferred ramp provider.
|
|
187
181
|
*
|
|
188
|
-
* @param
|
|
189
|
-
* @throws If region is not set, providers are not loaded, or provider is not found.
|
|
182
|
+
* @param provider - The provider object to set.
|
|
190
183
|
*/
|
|
191
|
-
|
|
184
|
+
setPreferredProvider(provider: Provider | null): void;
|
|
192
185
|
/**
|
|
193
186
|
* Initializes the controller by fetching the user's region from geolocation.
|
|
194
187
|
* This should be called once at app startup to set up the initial region.
|
|
@@ -223,15 +216,6 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
223
216
|
getTokens(region?: string, action?: RampAction, options?: ExecuteRequestOptions & {
|
|
224
217
|
provider?: string | string[];
|
|
225
218
|
}): Promise<TokensResponse>;
|
|
226
|
-
/**
|
|
227
|
-
* Sets the user's selected token by asset ID.
|
|
228
|
-
* Looks up the token from the current tokens in state and automatically
|
|
229
|
-
* fetches payment methods for that token.
|
|
230
|
-
*
|
|
231
|
-
* @param assetId - The asset identifier in CAIP-19 format (e.g., "eip155:1/erc20:0x...").
|
|
232
|
-
* @throws If assetId is not provided, region is not set, tokens are not loaded, or token is not found.
|
|
233
|
-
*/
|
|
234
|
-
setSelectedToken(assetId: string): void;
|
|
235
219
|
/**
|
|
236
220
|
* Fetches the list of providers for a given region.
|
|
237
221
|
* The providers are saved in the controller state once fetched.
|
|
@@ -256,8 +240,8 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
256
240
|
* Fetches the list of payment methods for a given context.
|
|
257
241
|
* The payment methods are saved in the controller state once fetched.
|
|
258
242
|
*
|
|
259
|
-
* @param region - User's region code (e.g. "fr", "us-ny").
|
|
260
243
|
* @param options - Query parameters for filtering payment methods.
|
|
244
|
+
* @param options.region - User's region code. If not provided, uses the user's region from controller state.
|
|
261
245
|
* @param options.fiat - Fiat currency code (e.g., "usd"). If not provided, uses the user's region currency.
|
|
262
246
|
* @param options.assetId - CAIP-19 cryptocurrency identifier.
|
|
263
247
|
* @param options.provider - Provider ID path.
|
|
@@ -265,21 +249,20 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
265
249
|
* @param options.ttl - Custom TTL for this request.
|
|
266
250
|
* @returns The payment methods response containing payments array.
|
|
267
251
|
*/
|
|
268
|
-
getPaymentMethods(
|
|
252
|
+
getPaymentMethods(options: {
|
|
253
|
+
region?: string;
|
|
269
254
|
fiat?: string;
|
|
270
|
-
assetId
|
|
271
|
-
provider
|
|
255
|
+
assetId: string;
|
|
256
|
+
provider: string;
|
|
272
257
|
forceRefresh?: boolean;
|
|
273
258
|
ttl?: number;
|
|
274
259
|
}): Promise<PaymentMethodsResponse>;
|
|
275
260
|
/**
|
|
276
|
-
* Sets the user's selected payment method
|
|
277
|
-
* Looks up the payment method from the current payment methods in state.
|
|
261
|
+
* Sets the user's selected payment method.
|
|
278
262
|
*
|
|
279
|
-
* @param
|
|
280
|
-
* @throws If payment methods are not loaded or payment method is not found.
|
|
263
|
+
* @param paymentMethod - The payment method to select, or null to clear.
|
|
281
264
|
*/
|
|
282
|
-
setSelectedPaymentMethod(
|
|
265
|
+
setSelectedPaymentMethod(paymentMethod: PaymentMethod | null): void;
|
|
283
266
|
/**
|
|
284
267
|
* Triggers setting the user region without throwing.
|
|
285
268
|
*
|
|
@@ -316,18 +299,19 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
316
299
|
/**
|
|
317
300
|
* Triggers fetching payment methods without throwing.
|
|
318
301
|
*
|
|
319
|
-
* @param region - User's region code (e.g., "us", "fr", "us-ny").
|
|
320
302
|
* @param options - Query parameters for filtering payment methods.
|
|
303
|
+
* @param options.region - User's region code. If not provided, uses userRegion from state.
|
|
321
304
|
* @param options.fiat - Fiat currency code. If not provided, uses userRegion currency.
|
|
322
305
|
* @param options.assetId - CAIP-19 cryptocurrency identifier.
|
|
323
306
|
* @param options.provider - Provider ID path.
|
|
324
307
|
* @param options.forceRefresh - Whether to bypass cache.
|
|
325
308
|
* @param options.ttl - Custom TTL for this request.
|
|
326
309
|
*/
|
|
327
|
-
triggerGetPaymentMethods(
|
|
310
|
+
triggerGetPaymentMethods(options: {
|
|
311
|
+
region?: string;
|
|
328
312
|
fiat?: string;
|
|
329
|
-
assetId
|
|
330
|
-
provider
|
|
313
|
+
assetId: string;
|
|
314
|
+
provider: string;
|
|
331
315
|
forceRefresh?: boolean;
|
|
332
316
|
ttl?: number;
|
|
333
317
|
}): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RampsController.d.mts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,QAAQ,EACR,KAAK,EACL,UAAU,EACV,aAAa,EACb,sBAAsB,
|
|
1
|
+
{"version":3,"file":"RampsController.d.mts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D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AG,CAAC,EAAE,MAAM,CAAC;KACd,GAAG,IAAI;CAKT"}
|
package/dist/RampsController.mjs
CHANGED
|
@@ -29,8 +29,8 @@ const rampsControllerMetadata = {
|
|
|
29
29
|
includeInStateLogs: true,
|
|
30
30
|
usedInUi: true,
|
|
31
31
|
},
|
|
32
|
-
|
|
33
|
-
persist:
|
|
32
|
+
preferredProvider: {
|
|
33
|
+
persist: true,
|
|
34
34
|
includeInDebugSnapshot: true,
|
|
35
35
|
includeInStateLogs: true,
|
|
36
36
|
usedInUi: true,
|
|
@@ -53,12 +53,6 @@ const rampsControllerMetadata = {
|
|
|
53
53
|
includeInStateLogs: true,
|
|
54
54
|
usedInUi: true,
|
|
55
55
|
},
|
|
56
|
-
selectedToken: {
|
|
57
|
-
persist: false,
|
|
58
|
-
includeInDebugSnapshot: true,
|
|
59
|
-
includeInStateLogs: true,
|
|
60
|
-
usedInUi: true,
|
|
61
|
-
},
|
|
62
56
|
paymentMethods: {
|
|
63
57
|
persist: false,
|
|
64
58
|
includeInDebugSnapshot: true,
|
|
@@ -89,11 +83,10 @@ const rampsControllerMetadata = {
|
|
|
89
83
|
export function getDefaultRampsControllerState() {
|
|
90
84
|
return {
|
|
91
85
|
userRegion: null,
|
|
92
|
-
|
|
86
|
+
preferredProvider: null,
|
|
93
87
|
countries: [],
|
|
94
88
|
providers: [],
|
|
95
89
|
tokens: null,
|
|
96
|
-
selectedToken: null,
|
|
97
90
|
paymentMethods: [],
|
|
98
91
|
selectedPaymentMethod: null,
|
|
99
92
|
requests: {},
|
|
@@ -215,6 +208,7 @@ export class RampsController extends BaseController {
|
|
|
215
208
|
if (pending) {
|
|
216
209
|
return pending.promise;
|
|
217
210
|
}
|
|
211
|
+
// Check cache validity (unless force refresh)
|
|
218
212
|
if (!options?.forceRefresh) {
|
|
219
213
|
const cached = this.state.requests[cacheKey];
|
|
220
214
|
if (cached && !isCacheExpired(cached, ttl)) {
|
|
@@ -309,8 +303,7 @@ export class RampsController extends BaseController {
|
|
|
309
303
|
// Set the new region atomically with cleanup to avoid intermediate null state
|
|
310
304
|
this.update((state) => {
|
|
311
305
|
if (regionChanged) {
|
|
312
|
-
state.
|
|
313
|
-
state.selectedToken = null;
|
|
306
|
+
state.preferredProvider = null;
|
|
314
307
|
state.tokens = null;
|
|
315
308
|
state.providers = [];
|
|
316
309
|
state.paymentMethods = [];
|
|
@@ -333,40 +326,14 @@ export class RampsController extends BaseController {
|
|
|
333
326
|
}
|
|
334
327
|
}
|
|
335
328
|
/**
|
|
336
|
-
* Sets the user's
|
|
337
|
-
*
|
|
338
|
-
* fetches payment methods for that provider.
|
|
329
|
+
* Sets the user's preferred provider.
|
|
330
|
+
* This allows users to set their preferred ramp provider.
|
|
339
331
|
*
|
|
340
|
-
* @param
|
|
341
|
-
* @throws If region is not set, providers are not loaded, or provider is not found.
|
|
332
|
+
* @param provider - The provider object to set.
|
|
342
333
|
*/
|
|
343
|
-
|
|
344
|
-
if (providerId === null) {
|
|
345
|
-
this.update((state) => {
|
|
346
|
-
state.selectedProvider = null;
|
|
347
|
-
});
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
const regionCode = this.state.userRegion?.regionCode;
|
|
351
|
-
if (!regionCode) {
|
|
352
|
-
throw new Error('Region is required. Cannot set selected provider without valid region information.');
|
|
353
|
-
}
|
|
354
|
-
const { providers } = this.state;
|
|
355
|
-
if (!providers || providers.length === 0) {
|
|
356
|
-
throw new Error('Providers not loaded. Cannot set selected provider before providers are fetched.');
|
|
357
|
-
}
|
|
358
|
-
const provider = providers.find((prov) => prov.id === providerId);
|
|
359
|
-
if (!provider) {
|
|
360
|
-
throw new Error(`Provider with ID "${providerId}" not found in available providers.`);
|
|
361
|
-
}
|
|
334
|
+
setPreferredProvider(provider) {
|
|
362
335
|
this.update((state) => {
|
|
363
|
-
state.
|
|
364
|
-
});
|
|
365
|
-
// fetch payment methods for the the new provider
|
|
366
|
-
// this is needed because you can change providers without changing the token
|
|
367
|
-
// (getPaymentMethods will use state as its default)
|
|
368
|
-
this.triggerGetPaymentMethods(regionCode, {
|
|
369
|
-
provider: provider.id,
|
|
336
|
+
state.preferredProvider = provider;
|
|
370
337
|
});
|
|
371
338
|
}
|
|
372
339
|
/**
|
|
@@ -408,7 +375,7 @@ export class RampsController extends BaseController {
|
|
|
408
375
|
const cacheKey = createCacheKey('getCountries', []);
|
|
409
376
|
const countries = await this.executeRequest(cacheKey, async () => {
|
|
410
377
|
return this.messenger.call('RampsService:getCountries');
|
|
411
|
-
},
|
|
378
|
+
}, options);
|
|
412
379
|
this.update((state) => {
|
|
413
380
|
state.countries = countries;
|
|
414
381
|
});
|
|
@@ -439,7 +406,7 @@ export class RampsController extends BaseController {
|
|
|
439
406
|
return this.messenger.call('RampsService:getTokens', normalizedRegion, action, {
|
|
440
407
|
provider: options?.provider,
|
|
441
408
|
});
|
|
442
|
-
},
|
|
409
|
+
}, options);
|
|
443
410
|
this.update((state) => {
|
|
444
411
|
const userRegionCode = state.userRegion?.regionCode;
|
|
445
412
|
if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
|
|
@@ -448,38 +415,6 @@ export class RampsController extends BaseController {
|
|
|
448
415
|
});
|
|
449
416
|
return tokens;
|
|
450
417
|
}
|
|
451
|
-
/**
|
|
452
|
-
* Sets the user's selected token by asset ID.
|
|
453
|
-
* Looks up the token from the current tokens in state and automatically
|
|
454
|
-
* fetches payment methods for that token.
|
|
455
|
-
*
|
|
456
|
-
* @param assetId - The asset identifier in CAIP-19 format (e.g., "eip155:1/erc20:0x...").
|
|
457
|
-
* @throws If assetId is not provided, region is not set, tokens are not loaded, or token is not found.
|
|
458
|
-
*/
|
|
459
|
-
setSelectedToken(assetId) {
|
|
460
|
-
if (!assetId) {
|
|
461
|
-
throw new Error('Asset ID is required.');
|
|
462
|
-
}
|
|
463
|
-
const regionCode = this.state.userRegion?.regionCode;
|
|
464
|
-
if (!regionCode) {
|
|
465
|
-
throw new Error('Region is required. Cannot set selected token without valid region information.');
|
|
466
|
-
}
|
|
467
|
-
const { tokens } = this.state;
|
|
468
|
-
if (!tokens) {
|
|
469
|
-
throw new Error('Tokens not loaded. Cannot set selected token before tokens are fetched.');
|
|
470
|
-
}
|
|
471
|
-
const token = tokens.allTokens.find((tok) => tok.assetId === assetId) ??
|
|
472
|
-
tokens.topTokens.find((tok) => tok.assetId === assetId);
|
|
473
|
-
if (!token) {
|
|
474
|
-
throw new Error(`Token with asset ID "${assetId}" not found in available tokens.`);
|
|
475
|
-
}
|
|
476
|
-
this.update((state) => {
|
|
477
|
-
state.selectedToken = token;
|
|
478
|
-
});
|
|
479
|
-
this.triggerGetPaymentMethods(regionCode, {
|
|
480
|
-
assetId: token.assetId,
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
418
|
/**
|
|
484
419
|
* Fetches the list of providers for a given region.
|
|
485
420
|
* The providers are saved in the controller state once fetched.
|
|
@@ -512,7 +447,7 @@ export class RampsController extends BaseController {
|
|
|
512
447
|
fiat: options?.fiat,
|
|
513
448
|
payments: options?.payments,
|
|
514
449
|
});
|
|
515
|
-
},
|
|
450
|
+
}, options);
|
|
516
451
|
this.update((state) => {
|
|
517
452
|
const userRegionCode = state.userRegion?.regionCode;
|
|
518
453
|
if (userRegionCode === undefined || userRegionCode === normalizedRegion) {
|
|
@@ -525,8 +460,8 @@ export class RampsController extends BaseController {
|
|
|
525
460
|
* Fetches the list of payment methods for a given context.
|
|
526
461
|
* The payment methods are saved in the controller state once fetched.
|
|
527
462
|
*
|
|
528
|
-
* @param region - User's region code (e.g. "fr", "us-ny").
|
|
529
463
|
* @param options - Query parameters for filtering payment methods.
|
|
464
|
+
* @param options.region - User's region code. If not provided, uses the user's region from controller state.
|
|
530
465
|
* @param options.fiat - Fiat currency code (e.g., "usd"). If not provided, uses the user's region currency.
|
|
531
466
|
* @param options.assetId - CAIP-19 cryptocurrency identifier.
|
|
532
467
|
* @param options.provider - Provider ID path.
|
|
@@ -534,72 +469,48 @@ export class RampsController extends BaseController {
|
|
|
534
469
|
* @param options.ttl - Custom TTL for this request.
|
|
535
470
|
* @returns The payment methods response containing payments array.
|
|
536
471
|
*/
|
|
537
|
-
async getPaymentMethods(
|
|
538
|
-
const
|
|
539
|
-
const fiatToUse = options
|
|
540
|
-
|
|
541
|
-
const selectedProviderAtStart = this.state.selectedProvider?.id ?? '';
|
|
542
|
-
const assetIdToUse = options?.assetId ?? selectedTokenAtStart;
|
|
543
|
-
const providerToUse = options?.provider ?? selectedProviderAtStart;
|
|
544
|
-
if (!regionCode) {
|
|
472
|
+
async getPaymentMethods(options) {
|
|
473
|
+
const regionToUse = options.region ?? this.state.userRegion?.regionCode;
|
|
474
|
+
const fiatToUse = options.fiat ?? this.state.userRegion?.country?.currency;
|
|
475
|
+
if (!regionToUse) {
|
|
545
476
|
throw new Error('Region is required. Either provide a region parameter or ensure userRegion is set in controller state.');
|
|
546
477
|
}
|
|
547
478
|
if (!fiatToUse) {
|
|
548
479
|
throw new Error('Fiat currency is required. Either provide a fiat parameter or ensure userRegion is set in controller state.');
|
|
549
480
|
}
|
|
550
|
-
const normalizedRegion =
|
|
481
|
+
const normalizedRegion = regionToUse.toLowerCase().trim();
|
|
551
482
|
const normalizedFiat = fiatToUse.toLowerCase().trim();
|
|
552
483
|
const cacheKey = createCacheKey('getPaymentMethods', [
|
|
553
484
|
normalizedRegion,
|
|
554
485
|
normalizedFiat,
|
|
555
|
-
|
|
556
|
-
|
|
486
|
+
options.assetId,
|
|
487
|
+
options.provider,
|
|
557
488
|
]);
|
|
558
489
|
const response = await this.executeRequest(cacheKey, async () => {
|
|
559
490
|
return this.messenger.call('RampsService:getPaymentMethods', {
|
|
560
491
|
region: normalizedRegion,
|
|
561
492
|
fiat: normalizedFiat,
|
|
562
|
-
assetId:
|
|
563
|
-
provider:
|
|
493
|
+
assetId: options.assetId,
|
|
494
|
+
provider: options.provider,
|
|
564
495
|
});
|
|
565
|
-
}, { forceRefresh: options
|
|
496
|
+
}, { forceRefresh: options.forceRefresh, ttl: options.ttl });
|
|
566
497
|
this.update((state) => {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
state.
|
|
573
|
-
const currentSelectionStillValid = response.payments.some((pm) => pm.id === state.selectedPaymentMethod?.id);
|
|
574
|
-
if (!currentSelectionStillValid) {
|
|
575
|
-
state.selectedPaymentMethod = response.payments[0] ?? null;
|
|
576
|
-
}
|
|
498
|
+
state.paymentMethods = response.payments;
|
|
499
|
+
// Only clear selected payment method if it's no longer in the new list
|
|
500
|
+
// This preserves the selection when cached data is returned (same context)
|
|
501
|
+
if (state.selectedPaymentMethod &&
|
|
502
|
+
!response.payments.some((pm) => pm.id === state.selectedPaymentMethod?.id)) {
|
|
503
|
+
state.selectedPaymentMethod = null;
|
|
577
504
|
}
|
|
578
505
|
});
|
|
579
506
|
return response;
|
|
580
507
|
}
|
|
581
508
|
/**
|
|
582
|
-
* Sets the user's selected payment method
|
|
583
|
-
* Looks up the payment method from the current payment methods in state.
|
|
509
|
+
* Sets the user's selected payment method.
|
|
584
510
|
*
|
|
585
|
-
* @param
|
|
586
|
-
* @throws If payment methods are not loaded or payment method is not found.
|
|
511
|
+
* @param paymentMethod - The payment method to select, or null to clear.
|
|
587
512
|
*/
|
|
588
|
-
setSelectedPaymentMethod(
|
|
589
|
-
if (paymentMethodId === null) {
|
|
590
|
-
this.update((state) => {
|
|
591
|
-
state.selectedPaymentMethod = null;
|
|
592
|
-
});
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
const { paymentMethods } = this.state;
|
|
596
|
-
if (!paymentMethods || paymentMethods.length === 0) {
|
|
597
|
-
throw new Error('Payment methods not loaded. Cannot set selected payment method before payment methods are fetched.');
|
|
598
|
-
}
|
|
599
|
-
const paymentMethod = paymentMethods.find((pm) => pm.id === paymentMethodId);
|
|
600
|
-
if (!paymentMethod) {
|
|
601
|
-
throw new Error(`Payment method with ID "${paymentMethodId}" not found in available payment methods.`);
|
|
602
|
-
}
|
|
513
|
+
setSelectedPaymentMethod(paymentMethod) {
|
|
603
514
|
this.update((state) => {
|
|
604
515
|
state.selectedPaymentMethod = paymentMethod;
|
|
605
516
|
});
|
|
@@ -656,16 +567,16 @@ export class RampsController extends BaseController {
|
|
|
656
567
|
/**
|
|
657
568
|
* Triggers fetching payment methods without throwing.
|
|
658
569
|
*
|
|
659
|
-
* @param region - User's region code (e.g., "us", "fr", "us-ny").
|
|
660
570
|
* @param options - Query parameters for filtering payment methods.
|
|
571
|
+
* @param options.region - User's region code. If not provided, uses userRegion from state.
|
|
661
572
|
* @param options.fiat - Fiat currency code. If not provided, uses userRegion currency.
|
|
662
573
|
* @param options.assetId - CAIP-19 cryptocurrency identifier.
|
|
663
574
|
* @param options.provider - Provider ID path.
|
|
664
575
|
* @param options.forceRefresh - Whether to bypass cache.
|
|
665
576
|
* @param options.ttl - Custom TTL for this request.
|
|
666
577
|
*/
|
|
667
|
-
triggerGetPaymentMethods(
|
|
668
|
-
this.getPaymentMethods(
|
|
578
|
+
triggerGetPaymentMethods(options) {
|
|
579
|
+
this.getPaymentMethods(options).catch(() => {
|
|
669
580
|
// Error stored in state
|
|
670
581
|
});
|
|
671
582
|
}
|
|
@@ -678,8 +589,7 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
|
|
|
678
589
|
}, _RampsController_cleanupState = function _RampsController_cleanupState() {
|
|
679
590
|
this.update((state) => {
|
|
680
591
|
state.userRegion = null;
|
|
681
|
-
state.
|
|
682
|
-
state.selectedToken = null;
|
|
592
|
+
state.preferredProvider = null;
|
|
683
593
|
state.tokens = null;
|
|
684
594
|
state.providers = [];
|
|
685
595
|
state.paymentMethods = [];
|