Skip to content

Commit

Permalink
feat: add logo url to token search api service and update searchToken…
Browse files Browse the repository at this point in the history
…s path (#5195)

## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* 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?
* If you had to upgrade a dependency, why did you do so?
-->

The `/tokens-search/name` endpoint has changed to `/tokens-search` to
better reflect it's usecase in
[PR-75](consensys-vertical-apps/va-mmcx-portfolio-api#75)
in the portfolio api. The endpoint searches by both name and address of
tokens from Moralis. Simply "/name" was misleading.

We also updated the param "name" to "query" to better reflect the nature
of the request.

## References

<!--
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

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/token-search-discover-controller`

- **Added**: The logoUrl is now exposed in TokenSearchResponseItem via
`searchTokens`
- **Breaking**:
- Update the TokenSearchApiService to use the updated URL for
`searchTokens`
    - The URL is now `/tokens-search` instead of `/tokens-search/name`
  - Changed the "name" parameter to "query" in the `searchTokens` method
- These updates align with the Portfolio API's `/tokens-search` endpoint
- The old endpoint `/tokens-search/name` will remain in a deprecated
state until parity across apps is achieved which for now is the
portfolio-api and core repos. The mobile
[PR-13111](MetaMask/metamask-mobile#13111) is
not yet merged.

## 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 highlighted breaking changes using the "BREAKING" category
above as appropriate
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
  • Loading branch information
Bigshmow authored Jan 28, 2025
1 parent bd681cf commit 42b28ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
12 changes: 12 additions & 0 deletions packages/token-search-discovery-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Introduce the logoUrl property to the TokenSearchApiService response
- Specifically in the `TokenSearchResponseItem` type

### Changed

- Update the TokenSearchApiService to use the updated URL for `searchTokens`
- The URL is now `/tokens-search` instead of `/tokens-search/name`
- Changed the "name" parameter to "query" in the `searchTokens` method
- These updates align with the Portfolio API's `/tokens-search` endpoint

## [1.0.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('TokenSearchApiService', () => {
tokenAddress: '0x1',
usdPrice: 100,
usdPricePercentChange: { oneDay: 10 },
logoUrl: 'https://static.cx.metamask.io/api/v1/tokenIcons/1/0x1.png',
},
{
name: 'Token2',
Expand All @@ -32,6 +33,7 @@ describe('TokenSearchApiService', () => {
tokenAddress: '0x3',
usdPrice: 300,
usdPricePercentChange: { oneDay: 30 },
logoUrl: 'https://static.cx.metamask.io/api/v1/tokenIcons/1/0x3.png',
},
],
onlyName: [
Expand Down Expand Up @@ -68,32 +70,32 @@ describe('TokenSearchApiService', () => {
describe('searchTokens', () => {
it.each([
{
params: { chains: ['1'], name: 'Test', limit: '10' },
params: { chains: ['1'], query: 'Test', limit: '10' },
expectedUrl: new URL(
`${baseUrl}/tokens-search/name?chains=1&name=Test&limit=10`,
`${baseUrl}/tokens-search?chains=1&query=Test&limit=10`,
),
},
{
params: { chains: ['1', '137'], name: 'Test' },
params: { chains: ['1', '137'], query: 'Test' },
expectedUrl: new URL(
`${baseUrl}/tokens-search/name?chains=1%2C137&name=Test`,
`${baseUrl}/tokens-search?chains=1%2C137&query=Test`,
),
},
{
params: { name: 'Test' },
expectedUrl: new URL(`${baseUrl}/tokens-search/name?name=Test`),
params: { query: 'Test' },
expectedUrl: new URL(`${baseUrl}/tokens-search?query=Test`),
},
{
params: { chains: ['1'] },
expectedUrl: new URL(`${baseUrl}/tokens-search/name?chains=1`),
expectedUrl: new URL(`${baseUrl}/tokens-search?chains=1`),
},
{
params: { limit: '20' },
expectedUrl: new URL(`${baseUrl}/tokens-search/name?limit=20`),
expectedUrl: new URL(`${baseUrl}/tokens-search?limit=20`),
},
{
params: {},
expectedUrl: new URL(`${baseUrl}/tokens-search/name`),
expectedUrl: new URL(`${baseUrl}/tokens-search`),
},
])(
'should construct correct URL for params: $params',
Expand All @@ -119,7 +121,7 @@ describe('TokenSearchApiService', () => {
describe('searchTokens response handling', () => {
it.each([
{
params: { chains: ['1'], name: 'Test', limit: '2' },
params: { chains: ['1'], query: 'Test', limit: '2' },
mockResponse: mockResponses.allParams,
description: 'all parameters',
},
Expand All @@ -129,7 +131,7 @@ describe('TokenSearchApiService', () => {
description: 'only chain parameter',
},
{
params: { name: 'Name' },
params: { query: 'Name' },
mockResponse: mockResponses.onlyName,
description: 'only name parameter',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export class TokenSearchApiService extends AbstractTokenSearchApiService {
async searchTokens(
tokenSearchParams?: TokenSearchParams,
): Promise<TokenSearchResponseItem[]> {
const url = new URL('/tokens-search/name', this.#baseUrl);
const url = new URL('/tokens-search', this.#baseUrl);

if (tokenSearchParams?.chains && tokenSearchParams.chains.length > 0) {
url.searchParams.append('chains', tokenSearchParams.chains.join());
}
if (tokenSearchParams?.name) {
url.searchParams.append('name', tokenSearchParams.name);
if (tokenSearchParams?.query) {
url.searchParams.append('query', tokenSearchParams.query);
}
if (tokenSearchParams?.limit) {
url.searchParams.append('limit', tokenSearchParams.limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('TokenSearchDiscoveryController', () => {
usdPricePercentChange: {
oneDay: 10,
},
// no logoUrl to test optional case
},
];

Expand Down Expand Up @@ -99,7 +100,7 @@ describe('TokenSearchDiscoveryController', () => {

const response = await controller.searchTokens({
chains: ['1'],
name: 'Test',
query: 'Test',
});

expect(response).toStrictEqual(mockSearchResults);
Expand Down
3 changes: 2 additions & 1 deletion packages/token-search-discovery-controller/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type TokenSearchParams = {
chains?: string[];
name?: string;
query?: string;
limit?: string;
};

Expand All @@ -13,4 +13,5 @@ export type TokenSearchResponseItem = {
usdPricePercentChange: {
oneDay: number;
};
logoUrl?: string;
};

0 comments on commit 42b28ac

Please sign in to comment.