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 token rates controller for multi evm fetching #4866

Merged
merged 14 commits into from
Nov 1, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('TokenRatesController', () => {
},
async ({ controller, triggerTokensStateChange }) => {
const updateExchangeRatesSpy = jest
.spyOn(controller, 'updateExchangeRates')
.spyOn(controller, 'updateExchangeRatesByChainId')
.mockResolvedValue();
await controller.start();
triggerTokensStateChange({
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('TokenRatesController', () => {
},
async ({ controller, triggerTokensStateChange }) => {
const updateExchangeRatesSpy = jest
.spyOn(controller, 'updateExchangeRates')
.spyOn(controller, 'updateExchangeRatesByChainId')
.mockResolvedValue();
await controller.start();
triggerTokensStateChange({
Expand Down
51 changes: 42 additions & 9 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,46 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async ({ allTokens, allDetectedTokens }) => {
const previousTokenAddresses = this.#getTokenAddresses(this.#chainId);
if (this.#disabled) {
return;
}

const chainIds = [
...new Set([
...Object.keys(allTokens),
...Object.keys(allDetectedTokens),
]),
] as Hex[];

const chainIdsToUpdate = chainIds.filter(
(chainId) =>
!isEqual(this.#allTokens[chainId], allTokens[chainId]) ||
!isEqual(
this.#allDetectedTokens[chainId],
allDetectedTokens[chainId],
),
);

this.#allTokens = allTokens;
this.#allDetectedTokens = allDetectedTokens;

const newTokenAddresses = this.#getTokenAddresses(this.#chainId);
if (
!isEqual(previousTokenAddresses, newTokenAddresses) &&
this.#pollState === PollState.Active
) {
await this.updateExchangeRates();
}
const { networkConfigurationsByChainId } = this.messagingSystem.call(
'NetworkController:getState',
);

await Promise.allSettled(
chainIdsToUpdate.map(async (chainId) => {
const nativeCurrency =
networkConfigurationsByChainId[chainId as Hex]?.nativeCurrency;

if (nativeCurrency) {
await this.updateExchangeRatesByChainId({
chainId: chainId as Hex,
nativeCurrency,
});
}
}),
);
},
({ allTokens, allDetectedTokens }) => {
return { allTokens, allDetectedTokens };
Expand Down Expand Up @@ -591,6 +620,7 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
}

return await this.#fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
chainId,
tokenAddresses,
nativeCurrency,
});
Expand Down Expand Up @@ -695,16 +725,19 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
* API, then convert the prices to our desired native currency.
*
* @param args - The arguments to this function.
* @param args.chainId - The chain id to fetch prices for.
* @param args.tokenAddresses - Addresses for tokens.
* @param args.nativeCurrency - The native currency in which to request
* prices.
* @returns A map of the token addresses (as checksums) to their prices in the
* native currency.
*/
async #fetchAndMapExchangeRatesForUnsupportedNativeCurrency({
chainId,
tokenAddresses,
nativeCurrency,
}: {
chainId: Hex;
tokenAddresses: Hex[];
nativeCurrency: string;
}): Promise<ContractMarketData> {
Expand All @@ -714,7 +747,7 @@ export class TokenRatesController extends StaticIntervalPollingController<TokenR
] = await Promise.all([
this.#fetchAndMapExchangeRatesForSupportedNativeCurrency({
tokenAddresses,
chainId: this.#chainId,
chainId,
nativeCurrency: FALL_BACK_VS_CURRENCY,
}),
getCurrencyConversionRate({
Expand Down
Loading