Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TokenDetectionController] pass header to accounts API #4877

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2931,6 +2931,7 @@ async function withController<ReturnValue>(
trackMetaMetricsEvent: jest.fn(),
messenger: buildTokenDetectionControllerMessenger(controllerMessenger),
useAccountsAPI: false,
platform: 'extension',
...options,
});
try {
Expand Down
16 changes: 13 additions & 3 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
#accountsAPI = {
isAccountsAPIEnabled: true,
supportedNetworksCache: null as number[] | null,
platform: '' as 'extension' | 'mobile',

async getSupportedNetworks() {
/* istanbul ignore next */
if (!this.isAccountsAPIEnabled) {
Expand Down Expand Up @@ -232,9 +234,13 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
);
}

const result = await fetchMultiChainBalances(address, {
networks: [chainIdNumber],
});
const result = await fetchMultiChainBalances(
address,
{
networks: [chainIdNumber],
},
this.platform,
);

return result.balances;
},
Expand All @@ -250,6 +256,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
* @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
* @param options.useAccountsAPI - Feature Switch for using the accounts API when detecting tokens (default: true)
* @param options.platform - Indicates whether the platform is extension or mobile
*/
constructor({
interval = DEFAULT_INTERVAL,
Expand All @@ -258,6 +265,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
trackMetaMetricsEvent,
messenger,
useAccountsAPI = true,
platform,
}: {
interval?: number;
disabled?: boolean;
Expand All @@ -277,6 +285,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
}) => void;
messenger: TokenDetectionControllerMessenger;
useAccountsAPI?: boolean;
platform: 'extension' | 'mobile';
}) {
super({
name: controllerName,
Expand Down Expand Up @@ -315,6 +324,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
this.#isUnlocked = isUnlocked;

this.#accountsAPI.isAccountsAPIEnabled = useAccountsAPI;
this.#accountsAPI.platform = platform;

this.#registerEventListeners();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('fetchMultiChainBalances()', () => {
it('should successfully return balances response', async () => {
const mockAPI = createMockAPI().reply(200, MOCK_GET_BALANCES_RESPONSE);

const result = await fetchMultiChainBalances(MOCK_ADDRESS);
const result = await fetchMultiChainBalances(MOCK_ADDRESS, {}, 'extension');
expect(result).toBeDefined();
expect(result).toStrictEqual(MOCK_GET_BALANCES_RESPONSE);
expect(mockAPI.isDone()).toBe(true);
Expand All @@ -59,9 +59,13 @@ describe('fetchMultiChainBalances()', () => {
})
.reply(200, MOCK_GET_BALANCES_RESPONSE);

const result = await fetchMultiChainBalances(MOCK_ADDRESS, {
networks: [1, 10],
});
const result = await fetchMultiChainBalances(
MOCK_ADDRESS,
{
networks: [1, 10],
},
'extension',
);
expect(result).toBeDefined();
expect(result).toStrictEqual(MOCK_GET_BALANCES_RESPONSE);
expect(mockAPI.isDone()).toBe(true);
Expand All @@ -79,7 +83,8 @@ describe('fetchMultiChainBalances()', () => {
const mockAPI = createMockAPI().reply(httpCode);

await expect(
async () => await fetchMultiChainBalances(MOCK_ADDRESS),
async () =>
await fetchMultiChainBalances(MOCK_ADDRESS, {}, 'extension'),
).rejects.toThrow(expect.any(Error));
expect(mockAPI.isDone()).toBe(true);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ export async function fetchSupportedNetworks(): Promise<number[]> {
* @param address - address to fetch balances from
* @param options - params to pass down for a more refined search
* @param options.networks - the networks (in decimal) that you want to filter by
* @param platform - indicates whether the platform is extension or mobile
* @returns a Balances Response
*/
export async function fetchMultiChainBalances(
address: string,
options?: { networks?: number[] },
options: { networks?: number[] },
platform: 'extension' | 'mobile',
) {
const url = getBalancesUrl(address, {
networks: options?.networks?.join(),
});
const response: GetBalancesResponse = await handleFetch(url);
const response: GetBalancesResponse = await handleFetch(url, {
headers: {
'x-metamask-clientproduct': `metamask-${platform}`,
},
});
return response;
}
Loading