Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[detectTokens] Extract tokenList into class field
Browse files Browse the repository at this point in the history
MajorLift committed Feb 21, 2024
1 parent 7ccedb8 commit 05611e1
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
@@ -510,37 +510,25 @@ export class TokenDetectionController extends StaticIntervalPollingController<
) {
return;
}
const { tokenList, slicesOfTokensToDetect } =
this.#getTokenListAndSlicesOfTokensToDetect();
for (const tokensSlice of slicesOfTokensToDetect) {
if (tokensSlice.length === 0) {
break;
}
await this.#addDetectedTokens({
tokenList,
tokensSlice,
});
}
}

#getTokenListAndSlicesOfTokensToDetect(): {
tokenList: Record<
string,
Pick<TokenListToken, 'address' | 'decimals' | 'symbol'>
>;
slicesOfTokensToDetect: string[][];
} {
const isTokenDetectionInactiveInMainnet =
!this.#isDetectionEnabledFromPreferences &&
this.#chainIdAgainstWhichToDetect === ChainId.mainnet;
const { tokensChainsCache } = this.messagingSystem.call(
'TokenListController:getState',
);

const tokenList = isTokenDetectionInactiveInMainnet
this.#tokenList = isTokenDetectionInactiveInMainnet
? STATIC_MAINNET_TOKEN_LIST
: tokensChainsCache[this.#chainIdAgainstWhichToDetect]?.data ?? {};

for (const tokensSlice of this.#getSlicesOfTokensToDetect()) {
if (tokensSlice.length === 0) {
break;
}
await this.#addDetectedTokens(tokensSlice);
}
}

#getSlicesOfTokensToDetect(): string[][] {
const { allTokens, allDetectedTokens, allIgnoredTokens } =
this.messagingSystem.call('TokensController:getState');
const [tokensAddresses, detectedTokensAddresses, ignoredTokensAddresses] = [
@@ -556,7 +544,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
);

const tokensToDetect: string[] = [];
for (const tokenAddress of Object.keys(tokenList)) {
for (const tokenAddress of Object.keys(this.#tokenList)) {
if (
[
tokensAddresses,
@@ -580,19 +568,10 @@ export class TokenDetectionController extends StaticIntervalPollingController<
tokensToDetect.length - 1,
);

return { tokenList, slicesOfTokensToDetect };
return slicesOfTokensToDetect;
}

async #addDetectedTokens({
tokenList,
tokensSlice,
}: {
tokenList: Record<
string,
Partial<TokenListToken> & Pick<Token, 'address' | 'symbol' | 'decimals'>
>;
tokensSlice: string[];
}): Promise<void> {
async #addDetectedTokens(tokensSlice: string[]): Promise<void> {
await safelyExecute(async () => {
const balances = await this.#getBalancesInSingleCall(
this.#addressAgainstWhichToDetect,
@@ -604,7 +583,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
const eventTokensDetails: string[] = [];
for (const nonZeroTokenAddress of Object.keys(balances)) {
const { decimals, symbol, aggregators, iconUrl, name } =
tokenList[nonZeroTokenAddress];
this.#tokenList[nonZeroTokenAddress];
eventTokensDetails.push(`${symbol} - ${nonZeroTokenAddress}`);
tokensWithBalance.push({
address: nonZeroTokenAddress,

0 comments on commit 05611e1

Please sign in to comment.