Skip to content

Commit 2659ea1

Browse files
feat: send x-mm-service-profile on token search (assets-controllers ^113.2.0) (#47655)
## **Description** Token search requests now send the `x-mm-service-profile: assets--trending-search` header, so backend services can identify this traffic ([ASSETS-4243](https://consensyssoftware.atlassian.net/browse/ASSETS-4243)). This is the extension version of MetaMask/metamask-mobile#38852. The extension makes `/tokens/search` requests from two places: 1. **Discover crypto search** (`ui/hooks/discover-search/useDiscoverCryptoSearch.ts`) calls core's `searchTokens`. Since MetaMask/core#11274 (released in `@metamask/assets-controllers` 113.2.0), core sends this header with `assets--trending-search` as the default. The hook now also passes `serviceProfile` explicitly. 2. **Token management search** (`ui/hooks/useTokenSearch.ts`) uses a local helper, `shared/lib/token-search/token-search-api.ts`, that builds its own `/tokens/search` URL. Bumping the package alone would not tag these requests. Core's `searchTokens` can't replace this helper as-is: it has a different signature and response shape, and it takes no `AbortSignal`. So the helper's fetch now adds the header itself. Changes: - Bump `@metamask/assets-controllers` from `^113.1.1` to `^113.2.0`. Peer dependencies are the same as 113.1.1. The only transitive change is the `uuid` range (`^14.0.1` → `^14.0.2`), which still resolves to 14.0.3, so there are no LavaMoat policy changes. The lockfile is deduped. - Add the shared constant `TOKEN_SEARCH_SERVICE_PROFILE = 'assets--trending-search'`, used by both paths. - Unit tests check the header on the local fetch, and check `serviceProfile` on both the first page and the next page of the Discover search. - The token API allows this header in CORS (`access-control-allow-headers: x-client-id,x-mm-service-profile`). Note: the token-management search is tagged as `assets--trending-search`, the same profile core now uses by default for every `searchTokens` caller. If backend wants a separate profile for it, that is a one-line change to the constant. ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/ASSETS-4243 Refs: MetaMask/core#11274, MetaMask/metamask-mobile#38852 ## **Manual testing steps** 1. Run the extension and open DevTools on the extension UI with the Network tab open. 2. Open Discover/Explore and search for a token (e.g. "USDC"). Check that the `GET /tokens/search` request includes the header `x-mm-service-profile: assets--trending-search`. Scroll to load more results and check that the paginated request sends the same header. 3. Go to token management (Import tokens / Manage tokens) and type a token name in the search. Check that the `/tokens/search` request includes `x-mm-service-profile: assets--trending-search` and still includes `X-Client-Id: extension`. 4. Check that search results still show up as before in both places. <!-- ## **Screenshots/Recordings** ### **Before** ### **After** --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. ## Adversarial review (adverse) Final verdict (round 2/3): SHIP (unanimous, 3/3) Findings: 0 critical · 1 warning · 11 info (across both rounds). Fixed: the comment wrongly said the constant "matches the core default" (round 1 warning); it now says both paths pass the constant explicitly. Declined: none. Info items were not actioned (some are notes only, e.g. that the header is not a trust boundary and that the transitive `uuid` range changed). Reviewer: claude -p via adverse 0.2.1 (3 personas, 2 rounds). [ASSETS-4243]: https://consensyssoftware.atlassian.net/browse/ASSETS-4243?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Adds a request header and bumps a dependency; no auth, signing, or response parsing changes beyond what tests cover. > > **Overview** > Extension **token search** traffic is now tagged for backend analytics by sending **`x-mm-service-profile: assets--trending-search`** on every `/tokens/search` call, alongside the existing **`X-Client-Id: extension`**. > > A shared **`TOKEN_SEARCH_SERVICE_PROFILE`** constant keeps both call paths aligned: **Discover crypto search** passes **`serviceProfile`** into **`@metamask/assets-controllers`** **`searchTokens`** (including paginated loads), and the **local** **`shared/lib/token-search/token-search-api`** helper (used for import/manage token search) sets the same header on its direct fetch. **`@metamask/assets-controllers`** is bumped to **`^113.2.0`** so core’s search client supports the option; tests assert the headers and **`serviceProfile`** on first and next pages. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 9d7e1ce. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 0c56a84 commit 2659ea1

6 files changed

Lines changed: 53 additions & 11 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
"@metamask/announcement-controller": "^8.0.0",
418418
"@metamask/approval-controller": "^9.0.0",
419419
"@metamask/assets-controller": "^20.2.2",
420-
"@metamask/assets-controllers": "^113.1.1",
420+
"@metamask/assets-controllers": "^113.2.0",
421421
"@metamask/authenticated-user-storage": "^4.0.0",
422422
"@metamask/base-controller": "^10.0.0",
423423
"@metamask/base-data-service": "^2.1.0",

‎shared/lib/token-search/token-search-api.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ describe('searchTokens', () => {
123123
);
124124
});
125125

126+
it('sends the X-Client-Id and x-mm-service-profile headers', async () => {
127+
const fetchMock = jest.fn().mockResolvedValue({
128+
ok: true,
129+
status: 200,
130+
statusText: 'OK',
131+
json: async () => ({
132+
data: [],
133+
count: 0,
134+
totalCount: 0,
135+
pageInfo: { hasNextPage: false, endCursor: '' },
136+
}),
137+
});
138+
global.fetch = fetchMock as unknown as typeof global.fetch;
139+
140+
await searchTokens({ query: 'usdc' });
141+
142+
const [, init] = fetchMock.mock.calls[0];
143+
expect(init.headers).toStrictEqual({
144+
'X-Client-Id': 'extension',
145+
'x-mm-service-profile': 'assets--trending-search',
146+
});
147+
});
148+
126149
it('short-circuits with an empty page when the query is whitespace-only', async () => {
127150
const fetchMock = jest.fn();
128151
global.fetch = fetchMock as unknown as typeof global.fetch;

‎shared/lib/token-search/token-search-api.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const TOKEN_V3_BASE_URL = 'https://tokens.api.cx.metamask.io';
77
const DEFAULT_BROWSE_OCCURRENCE_FLOOR = 3;
88
const EVM_CHAIN_NAMESPACE = 'eip155:';
99

10+
/**
11+
* Service profile sent as the `x-mm-service-profile` header on every
12+
* `/tokens/search` request made by the extension. Both the local helper below
13+
* and `useDiscoverCryptoSearch` (via core `searchTokens`) pass this value
14+
* explicitly, so they stay in sync regardless of the core default.
15+
*/
16+
export const TOKEN_SEARCH_SERVICE_PROFILE = 'assets--trending-search';
17+
1018
export type TokenSearchResult = {
1119
assetId: CaipAssetType;
1220
symbol: string;
@@ -77,7 +85,10 @@ const fetchTokenSearch = async (
7785
const fetchWithTimeout = getFetchWithTimeout(TEN_SECONDS_IN_MILLISECONDS);
7886
const response = await fetchWithTimeout(url, {
7987
method: 'GET',
80-
headers: { 'X-Client-Id': 'extension' },
88+
headers: {
89+
'X-Client-Id': 'extension',
90+
'x-mm-service-profile': TOKEN_SEARCH_SERVICE_PROFILE,
91+
},
8192
signal: options.signal,
8293
});
8394

‎ui/hooks/discover-search/useDiscoverCryptoSearch.test.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ describe('useDiscoverCryptoSearch', () => {
129129
expect.objectContaining({
130130
includeMarketData: true,
131131
includeTokenSecurityData: true,
132+
serviceProfile: 'assets--trending-search',
132133
}),
133134
);
134135
expect(result.current.data[0].priceChangePct?.h24).toBe('1.2');
@@ -229,7 +230,10 @@ describe('useDiscoverCryptoSearch', () => {
229230
expect(mockSearchTokens).toHaveBeenLastCalledWith(
230231
expect.any(Array),
231232
'eth',
232-
expect.objectContaining({ after: 'next-page' }),
233+
expect.objectContaining({
234+
after: 'next-page',
235+
serviceProfile: 'assets--trending-search',
236+
}),
233237
);
234238
expect(result.current.data.map(({ symbol }) => symbol)).toStrictEqual([
235239
'GEM',

‎ui/hooks/discover-search/useDiscoverCryptoSearch.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
type TrendingAsset,
66
} from '@metamask/assets-controllers';
77
import type { CaipChainId } from '@metamask/utils';
8-
import { byMarketCap } from '#shared/lib/token-search/token-search-api';
8+
import {
9+
byMarketCap,
10+
TOKEN_SEARCH_SERVICE_PROFILE,
11+
} from '#shared/lib/token-search/token-search-api';
912
import { MultichainNetworks } from '../../../shared/constants/multichain/networks';
1013
import { getIsStellarSupportEnabled } from '../../selectors/multichain/feature-flags';
1114
import {
@@ -129,6 +132,7 @@ export const useDiscoverCryptoSearch = ({
129132
after: pageParam,
130133
includeMarketData: true,
131134
includeTokenSecurityData: true,
135+
serviceProfile: TOKEN_SEARCH_SERVICE_PROFILE,
132136
});
133137

134138
if (response.error) {

‎yarn.lock‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7429,9 +7429,9 @@ __metadata:
74297429
languageName: node
74307430
linkType: hard
74317431

7432-
"@metamask/assets-controllers@npm:^113.0.0, @metamask/assets-controllers@npm:^113.1.1":
7433-
version: 113.1.1
7434-
resolution: "@metamask/assets-controllers@npm:113.1.1"
7432+
"@metamask/assets-controllers@npm:^113.0.0, @metamask/assets-controllers@npm:^113.1.1, @metamask/assets-controllers@npm:^113.2.0":
7433+
version: 113.2.0
7434+
resolution: "@metamask/assets-controllers@npm:113.2.0"
74357435
dependencies:
74367436
"@ethereumjs/util": "npm:^9.1.0"
74377437
"@ethersproject/abi": "npm:^5.7.0"
@@ -7478,11 +7478,11 @@ __metadata:
74787478
multiformats: "npm:^9.9.0"
74797479
reselect: "npm:^5.3.0"
74807480
single-call-balance-checker-abi: "npm:^1.0.0"
7481-
uuid: "npm:^14.0.1"
7481+
uuid: "npm:^14.0.2"
74827482
peerDependencies:
74837483
"@metamask/providers": ^22.0.0
74847484
webextension-polyfill: ^0.10.0 || ^0.11.0 || ^0.12.0
7485-
checksum: 10/379b859f93f67794fec4781ea4d955d828882f6de34bff53331b3f2ffc2fa7fe1dc451e155aff03afe21da996db7294dc76a786a7c7eb4d5230d49ea3f0abe0a
7485+
checksum: 10/c79851f7ebd75aa1691fa780711cefa41616572ddbb932ce491114e42a6a8f9c4a3badbbd691d8efd2de29898ea1fd241329123e1c4f3ee662afbec8d728cf18
74867486
languageName: node
74877487
linkType: hard
74887488

@@ -35493,7 +35493,7 @@ __metadata:
3549335493
"@metamask/api-specs": "npm:^0.13.0"
3549435494
"@metamask/approval-controller": "npm:^9.0.0"
3549535495
"@metamask/assets-controller": "npm:^20.2.2"
35496-
"@metamask/assets-controllers": "npm:^113.1.1"
35496+
"@metamask/assets-controllers": "npm:^113.2.0"
3549735497
"@metamask/authenticated-user-storage": "npm:^4.0.0"
3549835498
"@metamask/auto-changelog": "npm:^6.2.0"
3549935499
"@metamask/base-controller": "npm:^10.0.0"
@@ -47633,7 +47633,7 @@ __metadata:
4763347633
languageName: node
4763447634
linkType: hard
4763547635

47636-
"uuid@npm:^14.0.1":
47636+
"uuid@npm:^14.0.1, uuid@npm:^14.0.2":
4763747637
version: 14.0.3
4763847638
resolution: "uuid@npm:14.0.3"
4763947639
bin:

0 commit comments

Comments
 (0)