Skip to content

Commit 8a54c2f

Browse files
committed
feat(ramps-controller): update countries endpoint to v2 and add amount fields
- Add getApiPath helper function for versioned API paths (defaults to v2) - Update getCountries endpoint to use v2/regions/countries - Keep getGeolocation, getEligibility, and getTokens on v1 (no version prefix) - Add defaultAmount and quickAmounts fields to Country type - Update test mocks to use new v2 endpoint path
1 parent 4a1b947 commit 8a54c2f

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

packages/ramps-controller/src/RampsService.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ describe('RampsService', () => {
275275

276276
it('returns the countries from the cache API filtered by support', async () => {
277277
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
278-
.get('/regions/countries')
278+
.get('/v2/regions/countries')
279279
.query({
280280
action: 'buy',
281281
sdk: '2.1.6',
@@ -326,7 +326,7 @@ describe('RampsService', () => {
326326

327327
it('uses the production cache URL when environment is Production', async () => {
328328
nock('https://on-ramp-cache.api.cx.metamask.io')
329-
.get('/regions/countries')
329+
.get('/v2/regions/countries')
330330
.query({
331331
action: 'buy',
332332
sdk: '2.1.6',
@@ -379,7 +379,7 @@ describe('RampsService', () => {
379379

380380
it('uses staging cache URL when environment is Development', async () => {
381381
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
382-
.get('/regions/countries')
382+
.get('/v2/regions/countries')
383383
.query({
384384
action: 'buy',
385385
sdk: '2.1.6',
@@ -432,7 +432,7 @@ describe('RampsService', () => {
432432

433433
it('passes the action parameter correctly', async () => {
434434
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
435-
.get('/regions/countries')
435+
.get('/v2/regions/countries')
436436
.query({
437437
action: 'sell',
438438
sdk: '2.1.6',
@@ -516,7 +516,7 @@ describe('RampsService', () => {
516516
},
517517
];
518518
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
519-
.get('/regions/countries')
519+
.get('/v2/regions/countries')
520520
.query({
521521
action: 'sell',
522522
sdk: '2.1.6',
@@ -564,7 +564,7 @@ describe('RampsService', () => {
564564
},
565565
];
566566
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
567-
.get('/regions/countries')
567+
.get('/v2/regions/countries')
568568
.query({
569569
action: 'buy',
570570
sdk: '2.1.6',
@@ -586,7 +586,7 @@ describe('RampsService', () => {
586586
});
587587
it('throws if the countries API returns an error', async () => {
588588
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
589-
.get('/regions/countries')
589+
.get('/v2/regions/countries')
590590
.query({
591591
action: 'buy',
592592
sdk: '2.1.6',
@@ -607,13 +607,13 @@ describe('RampsService', () => {
607607
await clock.runAllAsync();
608608
await flushPromises();
609609
await expect(countriesPromise).rejects.toThrow(
610-
`Fetching 'https://on-ramp-cache.uat-api.cx.metamask.io/regions/countries?action=buy&sdk=2.1.6&controller=${CONTROLLER_VERSION}&context=mobile-ios' failed with status '500'`,
610+
`Fetching 'https://on-ramp-cache.uat-api.cx.metamask.io/v2/regions/countries?action=buy&sdk=2.1.6&controller=${CONTROLLER_VERSION}&context=mobile-ios' failed with status '500'`,
611611
);
612612
});
613613

614614
it('throws if the API returns a non-array response', async () => {
615615
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
616-
.get('/regions/countries')
616+
.get('/v2/regions/countries')
617617
.query({
618618
action: 'buy',
619619
sdk: '2.1.6',
@@ -636,7 +636,7 @@ describe('RampsService', () => {
636636

637637
it('throws if the API returns an object instead of an array', async () => {
638638
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
639-
.get('/regions/countries')
639+
.get('/v2/regions/countries')
640640
.query({
641641
action: 'buy',
642642
sdk: '2.1.6',
@@ -754,7 +754,7 @@ describe('RampsService', () => {
754754
},
755755
];
756756
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
757-
.get('/regions/countries')
757+
.get('/v2/regions/countries')
758758
.query({
759759
action: 'buy',
760760
sdk: '2.1.6',
@@ -807,7 +807,7 @@ describe('RampsService', () => {
807807
},
808808
];
809809
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
810-
.get('/regions/countries')
810+
.get('/v2/regions/countries')
811811
.query({
812812
action: 'buy',
813813
sdk: '2.1.6',
@@ -864,7 +864,7 @@ describe('RampsService', () => {
864864
},
865865
];
866866
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
867-
.get('/regions/countries')
867+
.get('/v2/regions/countries')
868868
.query({
869869
action: 'buy',
870870
sdk: '2.1.6',
@@ -905,7 +905,7 @@ describe('RampsService', () => {
905905
},
906906
];
907907
nock('https://on-ramp-cache.uat-api.cx.metamask.io')
908-
.get('/regions/countries')
908+
.get('/v2/regions/countries')
909909
.query({
910910
action: 'buy',
911911
sdk: '2.1.6',

packages/ramps-controller/src/RampsService.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ export type Country = {
134134
* Array of state objects.
135135
*/
136136
states?: State[];
137+
/**
138+
* Default amount for ramps transactions.
139+
*/
140+
defaultAmount?: number;
141+
/**
142+
* Quick amount options for ramps transactions.
143+
*/
144+
quickAmounts?: number[];
137145
};
138146

139147
/**
@@ -281,6 +289,17 @@ function getBaseUrl(
281289
}
282290
}
283291

292+
/**
293+
* Constructs an API path with an optional version prefix.
294+
*
295+
* @param path - The API endpoint path.
296+
* @param version - The API version prefix (e.g., 'v2'). Defaults to 'v2'.
297+
* @returns The versioned API path.
298+
*/
299+
function getApiPath(path: string, version: string = 'v2'): string {
300+
return version ? `${version}/${path}` : path;
301+
}
302+
284303
/**
285304
* This service object is responsible for interacting with the Ramps API.
286305
*
@@ -513,7 +532,7 @@ export class RampsService {
513532
async getGeolocation(): Promise<string> {
514533
const textResponse = await this.#request<string>(
515534
RampsApiService.Orders,
516-
'geolocation',
535+
getApiPath('geolocation', ''),
517536
{ responseType: 'text' },
518537
);
519538

@@ -535,7 +554,7 @@ export class RampsService {
535554
async getCountries(action: 'buy' | 'sell' = 'buy'): Promise<Country[]> {
536555
const countries = await this.#request<Country[]>(
537556
RampsApiService.Regions,
538-
'regions/countries',
557+
getApiPath('regions/countries'),
539558
{ action, responseType: 'json' },
540559
);
541560

@@ -565,7 +584,7 @@ export class RampsService {
565584
const normalizedIsoCode = isoCode.toLowerCase().trim();
566585
return this.#request<Eligibility>(
567586
RampsApiService.Regions,
568-
`regions/countries/${normalizedIsoCode}`,
587+
getApiPath(`regions/countries/${normalizedIsoCode}`, ''),
569588
{ responseType: 'json' },
570589
);
571590
}
@@ -584,7 +603,7 @@ export class RampsService {
584603
const normalizedRegion = region.toLowerCase().trim();
585604
const response = await this.#request<TokensResponse>(
586605
RampsApiService.Regions,
587-
`regions/${normalizedRegion}/tokens`,
606+
getApiPath(`regions/${normalizedRegion}/tokens`, ''),
588607
{ action, responseType: 'json' },
589608
);
590609

0 commit comments

Comments
 (0)