diff --git a/packages/assets-controllers/src/TokenListController.ts b/packages/assets-controllers/src/TokenListController.ts index ab4b709843..0e7826476b 100644 --- a/packages/assets-controllers/src/TokenListController.ts +++ b/packages/assets-controllers/src/TokenListController.ts @@ -5,9 +5,7 @@ import type { } from '@metamask/base-controller'; import { safelyExecute } from '@metamask/controller-utils'; import type { - NetworkClientId, NetworkControllerStateChangeEvent, - NetworkState, NetworkControllerGetNetworkClientByIdAction, } from '@metamask/network-controller'; import { StaticIntervalPollingController } from '@metamask/polling-controller'; @@ -94,7 +92,7 @@ export const getDefaultTokenListState = (): TokenListState => { /** The input to start polling for the {@link TokenListController} */ type TokenListPollingInput = { - networkClientId: NetworkClientId; + chainId: Hex; }; /** @@ -113,16 +111,12 @@ export class TokenListController extends StaticIntervalPollingController void, - ) => void; interval?: number; cacheRefreshThreshold?: number; messenger: TokenListControllerMessenger; @@ -156,67 +144,18 @@ export class TokenListController extends StaticIntervalPollingController { - await this.#onNetworkControllerStateChange(networkControllerState); - }); - } else { - this.messagingSystem.subscribe( - 'NetworkController:stateChange', - // TODO: Either fix this lint violation or explain why it's necessary to ignore. - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async (networkControllerState) => { - await this.#onNetworkControllerStateChange(networkControllerState); - }, - ); - } - } - - /** - * Updates state and restarts polling on changes to the network controller - * state. - * - * @param networkControllerState - The updated network controller state. - */ - async #onNetworkControllerStateChange(networkControllerState: NetworkState) { - const selectedNetworkClient = this.messagingSystem.call( - 'NetworkController:getNetworkClientById', - networkControllerState.selectedNetworkClientId, - ); - const { chainId } = selectedNetworkClient.configuration; - - if (this.chainId !== chainId) { - this.abortController.abort(); - this.abortController = new AbortController(); - this.chainId = chainId; - if (this.state.preventPollingOnNetworkRestart) { - this.clearingTokenListData(); - } else { - // Ensure tokenList is referencing data from correct network - this.update(() => { - return { - ...this.state, - tokenList: this.state.tokensChainsCache[this.chainId]?.data || {}, - }; - }); - await this.restart(); - } - } } /** * Start polling for the token list. */ async start() { - if (!isTokenListSupportedForNetwork(this.chainId)) { + if (!isTokenListSupportedForNetwork(pollingInput.chainId)) { return; } - await this.#startPolling(); + await this.#startPolling(pollingInput.chainId); } /** @@ -224,7 +163,7 @@ export class TokenListController extends StaticIntervalPollingController { - await safelyExecute(() => this.fetchTokenList()); + async #startPolling({ chainId }: TokenListPollingInput): Promise { + await safelyExecute(() => this._executePoll({ chainId })); // TODO: Either fix this lint violation or explain why it's necessary to ignore. // eslint-disable-next-line @typescript-eslint/no-misused-promises this.intervalId = setInterval(async () => { - await safelyExecute(() => this.fetchTokenList()); + await safelyExecute(() => this._executePoll({ chainId })); }, this.intervalDelay); } @@ -267,30 +208,21 @@ export class TokenListController extends StaticIntervalPollingController { - return this.fetchTokenList(networkClientId); + async _executePoll({ chainId }: TokenListPollingInput): Promise { + return this.fetchTokenList(chainId); } /** * Fetching token list from the Token Service API. * - * @param networkClientId - The ID of the network client triggering the fetch. + * @param chainId - The chainId of the network client triggering the fetch. */ - async fetchTokenList(networkClientId?: NetworkClientId): Promise { + async fetchTokenList(chainId: Hex): Promise { const releaseLock = await this.mutex.acquire(); - let networkClient; - if (networkClientId) { - networkClient = this.messagingSystem.call( - 'NetworkController:getNetworkClientById', - networkClientId, - ); - } - const chainId = networkClient?.configuration.chainId ?? this.chainId; + try { const { tokensChainsCache } = this.state; let tokenList: TokenListMap = {};