diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index d5a230765431..203541df338f 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -65,12 +65,7 @@ export default class DetectTokensController extends StaticIntervalPollingControl this.useTokenDetection = this.preferences?.store.getState().useTokenDetection; this.selectedAddress = getCurrentSelectedAccount().address; - this.tokenAddresses = this.tokensController?.state.tokens.map((token) => { - return token.address; - }); this.setIntervalLength(interval); - this.hiddenTokens = this.tokensController?.state.ignoredTokens; - this.detectedTokens = this.tokensController?.state.detectedTokens; this.chainId = this.getChainIdFromNetworkStore(); this._trackMetaMetricsEvent = trackMetaMetricsEvent; @@ -101,15 +96,6 @@ export default class DetectTokensController extends StaticIntervalPollingControl } }); - tokensController?.subscribe( - ({ tokens = [], ignoredTokens = [], detectedTokens = [] }) => { - this.tokenAddresses = tokens.map((token) => { - return token.address; - }); - this.hiddenTokens = ignoredTokens; - this.detectedTokens = detectedTokens; - }, - ); messenger.subscribe('NetworkController:stateChange', () => { if (this.chainId !== this.getChainIdFromNetworkStore()) { const chainId = this.getChainIdFromNetworkStore(); @@ -169,13 +155,19 @@ export default class DetectTokensController extends StaticIntervalPollingControl const tokensToDetect = []; for (const tokenAddress in tokenListUsed) { if ( - !this.tokenAddresses.find((address) => + !this.tokensController.state.allTokens?.[chainIdAgainstWhichToDetect]?.[ + addressAgainstWhichToDetect + ]?.find(({ address }) => isEqualCaseInsensitive(address, tokenAddress), ) && - !this.hiddenTokens.find((address) => + !this.tokensController.state.allIgnoredTokens?.[ + chainIdAgainstWhichToDetect + ]?.[addressAgainstWhichToDetect]?.find((address) => isEqualCaseInsensitive(address, tokenAddress), ) && - !this.detectedTokens.find(({ address }) => + !this.tokensController.state.allDetectedTokens?.[ + chainIdAgainstWhichToDetect + ]?.[addressAgainstWhichToDetect]?.find(({ address }) => isEqualCaseInsensitive(address, tokenAddress), ) ) { @@ -192,6 +184,9 @@ export default class DetectTokensController extends StaticIntervalPollingControl result = await this.assetsContractController.getBalancesInSingleCall( addressAgainstWhichToDetect, tokensSlice, + this.network.findNetworkClientIdByChainId( + chainIdAgainstWhichToDetect, + ), ); } catch (error) { warn( @@ -247,7 +242,8 @@ export default class DetectTokensController extends StaticIntervalPollingControl */ restartTokenDetection({ selectedAddress, chainId } = {}) { const addressAgainstWhichToDetect = selectedAddress ?? this.selectedAddress; - const chainIdAgainstWhichToDetect = chainId ?? this.chainId; + const chainIdAgainstWhichToDetect = + chainId ?? this.getChainIdFromNetworkStore(); if (!(this.isActive && addressAgainstWhichToDetect)) { return; }