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

update TokenRatesController to poll on chain id instead of network client id #4887

Merged
merged 1 commit into from
Nov 1, 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
24 changes: 12 additions & 12 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { createMockInternalAccount } from '../../accounts-controller/src/tests/m
import {
buildCustomNetworkClientConfiguration,
buildMockGetNetworkClientById,
buildNetworkConfiguration,
} from '../../network-controller/tests/helpers';
import { TOKEN_PRICES_BATCH_SIZE } from './assetsUtil';
import type {
Expand Down Expand Up @@ -1280,7 +1281,7 @@ describe('TokenRatesController', () => {
},
async ({ controller }) => {
controller.startPolling({
networkClientId: 'mainnet',
chainId: ChainId.mainnet,
});

await advanceTime({ clock, duration: 0 });
Expand Down Expand Up @@ -1334,7 +1335,7 @@ describe('TokenRatesController', () => {
},
async ({ controller }) => {
controller.startPolling({
networkClientId: 'mainnet',
chainId: ChainId.mainnet,
});
await advanceTime({ clock, duration: 0 });

Expand Down Expand Up @@ -1404,18 +1405,17 @@ describe('TokenRatesController', () => {
return currency !== 'LOL';
},
});
const selectedNetworkClientConfiguration =
buildCustomNetworkClientConfiguration({
chainId: ChainId.mainnet,
ticker: 'LOL',
});
await withController(
{
options: {
tokenPricesService,
},
mockNetworkClientConfigurationsByNetworkClientId: {
mainnet: selectedNetworkClientConfiguration,
mockNetworkState: {
networkConfigurationsByChainId: {
[ChainId.mainnet]: buildNetworkConfiguration({
nativeCurrency: 'LOL',
}),
},
},
mockTokensControllerState: {
allTokens: {
Expand All @@ -1440,7 +1440,7 @@ describe('TokenRatesController', () => {
},
async ({ controller }) => {
controller.startPolling({
networkClientId: 'mainnet',
chainId: ChainId.mainnet,
});
// flush promises and advance setTimeouts they enqueue 3 times
// needed because fetch() doesn't resolve immediately, so any
Expand Down Expand Up @@ -1542,7 +1542,7 @@ describe('TokenRatesController', () => {
},
async ({ controller }) => {
controller.startPolling({
networkClientId: 'mainnet',
chainId: ChainId.mainnet,
});
// flush promises and advance setTimeouts they enqueue 3 times
// needed because fetch() doesn't resolve immediately, so any
Expand Down Expand Up @@ -1585,7 +1585,7 @@ describe('TokenRatesController', () => {
},
async ({ controller }) => {
const pollingToken = controller.startPolling({
networkClientId: 'mainnet',
chainId: ChainId.mainnet,
});
await advanceTime({ clock, duration: 0 });
expect(tokenPricesService.fetchTokenPrices).toHaveBeenCalledTimes(
Expand Down
27 changes: 16 additions & 11 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from '@metamask/controller-utils';
import type { InternalAccount } from '@metamask/keyring-api';
import type {
NetworkClientId,
NetworkControllerGetNetworkClientByIdAction,
NetworkControllerGetStateAction,
NetworkControllerStateChangeEvent,
Expand Down Expand Up @@ -223,7 +222,7 @@ export const getDefaultTokenRatesControllerState =

/** The input to start polling for the {@link TokenRatesController} */
export type TokenRatesPollingInput = {
networkClientId: NetworkClientId;
chainId: Hex;
};

/**
Expand Down Expand Up @@ -630,18 +629,24 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
* Updates token rates for the given networkClientId
*
* @param input - The input for the poll.
* @param input.networkClientId - The network client ID used to get a ticker value.
* @param input.chainId - The chain id to poll token rates on.
*/
async _executePoll({
networkClientId,
}: TokenRatesPollingInput): Promise<void> {
const networkClient = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
networkClientId,
async _executePoll({ chainId }: TokenRatesPollingInput): Promise<void> {
const { networkConfigurationsByChainId } = this.messagingSystem.call(
'NetworkController:getState',
);

const networkConfiguration = networkConfigurationsByChainId[chainId];
if (!networkConfiguration) {
console.error(
`TokenRatesController: No network configuration found for chainId ${chainId}`,
);
return;
}

await this.updateExchangeRatesByChainId({
chainId: networkClient.configuration.chainId,
nativeCurrency: networkClient.configuration.ticker,
chainId,
nativeCurrency: networkConfiguration.nativeCurrency,
});
}

Expand Down
Loading