Skip to content

Commit 10309a9

Browse files
authored
Add swappable param to discovery endpoints (#5819)
## Explanation * What is the current state of things and why does it need to change? token discovery controller doesn't know about the swappable* endpoints of the token discovery API * What is the solution your changes offer and how does it work? add swappable* versions of the discovery endpoints to the controller + api service * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? no * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? N/A * If you had to upgrade a dependency, why did you do so? N/A ## References MMPD-1626 <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Changelog <!-- THIS SECTION IS NO LONGER NEEDED. The process for updating changelogs has changed. Please consult the "Updating changelogs" section of the Contributing doc for more. --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent e1b135c commit 10309a9

File tree

4 files changed

+41
-82
lines changed

4 files changed

+41
-82
lines changed

packages/token-search-discovery-controller/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111

1212
- Bump `@metamask/base-controller` from ^8.0.0 to ^8.0.1 ([#5722](https://github.com/MetaMask/core/pull/5722))
13+
- Add `swappable` param to token discovery controller and API service ([#5819](https://github.com/MetaMask/core/pull/5819))
1314

1415
## [3.1.0]
1516

packages/token-search-discovery-controller/src/token-discovery-api-service/token-discovery-api-service.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ describe('TokenDiscoveryApiService', () => {
8989
params: { limit: '10' },
9090
expectedPath: '/tokens-search/trending?limit=10',
9191
},
92+
{
93+
params: { swappable: true },
94+
expectedPath: '/tokens-search/trending?swappable=true',
95+
},
9296
{
9397
params: {},
9498
expectedPath: '/tokens-search/trending',
@@ -154,6 +158,10 @@ describe('TokenDiscoveryApiService', () => {
154158
params: { chains: ['1', '137'] },
155159
expectedPath: '/tokens-search/top-gainers?chains=1,137',
156160
},
161+
{
162+
params: { swappable: true },
163+
expectedPath: '/tokens-search/top-gainers?swappable=true',
164+
},
157165
])(
158166
'should construct correct URL for params: $params',
159167
async ({ params, expectedPath }) => {
@@ -196,6 +204,10 @@ describe('TokenDiscoveryApiService', () => {
196204
params: { chains: ['1', '137'] },
197205
expectedPath: '/tokens-search/top-losers?chains=1,137',
198206
},
207+
{
208+
params: { swappable: true },
209+
expectedPath: '/tokens-search/top-losers?swappable=true',
210+
},
199211
])(
200212
'should construct correct URL for params: $params',
201213
async ({ params, expectedPath }) => {
@@ -238,6 +250,10 @@ describe('TokenDiscoveryApiService', () => {
238250
params: { chains: ['1', '137'] },
239251
expectedPath: '/tokens-search/blue-chip?chains=1,137',
240252
},
253+
{
254+
params: { swappable: true },
255+
expectedPath: '/tokens-search/blue-chip?swappable=true',
256+
},
241257
])(
242258
'should construct correct URL for params: $params',
243259
async ({ params, expectedPath }) => {

packages/token-search-discovery-controller/src/token-discovery-api-service/token-discovery-api-service.ts

Lines changed: 21 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
TopLosersParams,
66
TrendingTokensParams,
77
BlueChipParams,
8+
ParamsBase,
89
} from '../types';
910

1011
export class TokenDiscoveryApiService extends AbstractTokenDiscoveryApiService {
@@ -18,19 +19,19 @@ export class TokenDiscoveryApiService extends AbstractTokenDiscoveryApiService {
1819
this.#baseUrl = baseUrl;
1920
}
2021

21-
async getTrendingTokensByChains(
22-
trendingTokensParams?: TrendingTokensParams,
23-
): Promise<MoralisTokenResponseItem[]> {
24-
const url = new URL('/tokens-search/trending', this.#baseUrl);
22+
async #fetch(subPath: string, params?: ParamsBase) {
23+
const url = new URL(`/tokens-search/${subPath}`, this.#baseUrl);
2524

26-
if (
27-
trendingTokensParams?.chains &&
28-
trendingTokensParams.chains.length > 0
29-
) {
30-
url.searchParams.append('chains', trendingTokensParams.chains.join());
25+
if (params?.chains && params.chains.length > 0) {
26+
url.searchParams.append('chains', params.chains.join());
3127
}
32-
if (trendingTokensParams?.limit) {
33-
url.searchParams.append('limit', trendingTokensParams.limit);
28+
29+
if (params?.limit) {
30+
url.searchParams.append('limit', params.limit);
31+
}
32+
33+
if (params?.swappable) {
34+
url.searchParams.append('swappable', 'true');
3435
}
3536

3637
const response = await fetch(url, {
@@ -49,87 +50,27 @@ export class TokenDiscoveryApiService extends AbstractTokenDiscoveryApiService {
4950
return response.json();
5051
}
5152

53+
async getTrendingTokensByChains(
54+
trendingTokensParams?: TrendingTokensParams,
55+
): Promise<MoralisTokenResponseItem[]> {
56+
return this.#fetch('trending', trendingTokensParams);
57+
}
58+
5259
async getTopLosersByChains(
5360
topLosersParams?: TopLosersParams,
5461
): Promise<MoralisTokenResponseItem[]> {
55-
const url = new URL('/tokens-search/top-losers', this.#baseUrl);
56-
57-
if (topLosersParams?.chains && topLosersParams.chains.length > 0) {
58-
url.searchParams.append('chains', topLosersParams.chains.join());
59-
}
60-
if (topLosersParams?.limit) {
61-
url.searchParams.append('limit', topLosersParams.limit);
62-
}
63-
64-
const response = await fetch(url, {
65-
method: 'GET',
66-
headers: {
67-
'Content-Type': 'application/json',
68-
},
69-
});
70-
71-
if (!response.ok) {
72-
throw new Error(
73-
`Portfolio API request failed with status: ${response.status}`,
74-
);
75-
}
76-
77-
return response.json();
62+
return this.#fetch('top-losers', topLosersParams);
7863
}
7964

8065
async getTopGainersByChains(
8166
topGainersParams?: TopGainersParams,
8267
): Promise<MoralisTokenResponseItem[]> {
83-
const url = new URL('/tokens-search/top-gainers', this.#baseUrl);
84-
85-
if (topGainersParams?.chains && topGainersParams.chains.length > 0) {
86-
url.searchParams.append('chains', topGainersParams.chains.join());
87-
}
88-
if (topGainersParams?.limit) {
89-
url.searchParams.append('limit', topGainersParams.limit);
90-
}
91-
92-
const response = await fetch(url, {
93-
method: 'GET',
94-
headers: {
95-
'Content-Type': 'application/json',
96-
},
97-
});
98-
99-
if (!response.ok) {
100-
throw new Error(
101-
`Portfolio API request failed with status: ${response.status}`,
102-
);
103-
}
104-
105-
return response.json();
68+
return this.#fetch('top-gainers', topGainersParams);
10669
}
10770

10871
async getBlueChipTokensByChains(
10972
blueChipParams?: BlueChipParams,
11073
): Promise<MoralisTokenResponseItem[]> {
111-
const url = new URL('/tokens-search/blue-chip', this.#baseUrl);
112-
113-
if (blueChipParams?.chains && blueChipParams.chains.length > 0) {
114-
url.searchParams.append('chains', blueChipParams.chains.join());
115-
}
116-
if (blueChipParams?.limit) {
117-
url.searchParams.append('limit', blueChipParams.limit);
118-
}
119-
120-
const response = await fetch(url, {
121-
method: 'GET',
122-
headers: {
123-
'Content-Type': 'application/json',
124-
},
125-
});
126-
127-
if (!response.ok) {
128-
throw new Error(
129-
`Portfolio API request failed with status: ${response.status}`,
130-
);
131-
}
132-
133-
return response.json();
74+
return this.#fetch('blue-chip', blueChipParams);
13475
}
13576
}

packages/token-search-discovery-controller/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Function params
22

3-
type ParamsBase = {
3+
export type ParamsBase = {
44
chains?: string[];
55
limit?: string;
6+
swappable?: boolean;
67
};
78

8-
export type TokenSearchParams = ParamsBase & {
9+
export type TokenSearchParams = Omit<ParamsBase, 'swappable'> & {
910
query?: string;
1011
};
1112

0 commit comments

Comments
 (0)