Skip to content

Commit

Permalink
update TokenRatesController to poll on chain id instead of network cl…
Browse files Browse the repository at this point in the history
…ient id (#4887)

## Explanation

Updates the polling input for `TokenRatesController` to a chain id
instead of a network client id.


## References

* Related to MetaMask/metamask-extension#28158

## 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/assets-controllers`

- **BREAKING**: The `TokenRatesController` now accepts `{chainId: Hex}`
as its polling input to `startPolling()` instead of `{networkClientId:
NetworkClientId}`

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
  • Loading branch information
bergeron authored Nov 1, 2024
1 parent 5526c7a commit abd7517
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
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

0 comments on commit abd7517

Please sign in to comment.