Skip to content

Commit 129c82e

Browse files
committed
Use legacy token list from contract-metadata repo to perform auto-detection if active network is mainnet and token detection is disabled in preferences
1 parent 0f46fdc commit 129c82e

1 file changed

Lines changed: 46 additions & 14 deletions

File tree

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import type {
44
ControllerGetStateAction,
55
ControllerStateChangeEvent,
66
} from '@metamask/base-controller';
7+
import contractMap from '@metamask/contract-metadata';
78
import {
9+
ChainId,
810
safelyExecute,
911
toChecksumHexAddress,
1012
} from '@metamask/controller-utils';
@@ -28,12 +30,38 @@ import { isTokenDetectionSupportedForNetwork } from './assetsUtil';
2830
import type {
2931
GetTokenListState,
3032
TokenListStateChange,
33+
TokenListToken,
3134
} from './TokenListController';
3235
import type { Token } from './TokenRatesController';
3336
import type { TokensController, TokensState } from './TokensController';
3437

3538
const DEFAULT_INTERVAL = 180000;
3639

40+
type LegacyToken = Omit<
41+
Token,
42+
'aggregators' | 'image' | 'balanceError' | 'isERC721'
43+
> & {
44+
name: string;
45+
logo: string;
46+
erc20?: boolean;
47+
erc721?: boolean;
48+
};
49+
50+
export const STATIC_MAINNET_TOKEN_LIST = Object.entries<LegacyToken>(
51+
contractMap,
52+
).reduce<Record<string, Partial<TokenListToken>>>((acc, [base, contract]) => {
53+
const { logo, ...tokenMetadata } = contract;
54+
return {
55+
...acc,
56+
[base.toLowerCase()]: {
57+
...tokenMetadata,
58+
address: base.toLowerCase(),
59+
iconUrl: `images/contract/${logo}`,
60+
aggregators: [],
61+
},
62+
};
63+
}, {});
64+
3765
export const controllerName = 'TokenDetectionController';
3866

3967
export type TokenDetectionState = Record<never, never>;
@@ -390,11 +418,12 @@ export class TokenDetectionController extends StaticIntervalPollingController<
390418
}
391419

392420
/**
393-
* Triggers asset ERC20 token auto detection for each contract address in contract metadata on mainnet.
421+
* For each token in the token list provided by the TokenListController, checks the token's balance for the selected account address on the active network.
422+
* On mainnet, if token detection is disabled in preferences, ERC20 token auto detection will be triggered for each contract address in the legacy token list from the @metamask/contract-metadata repo.
394423
*
395-
* @param options - Options to detect tokens.
424+
* @param options - Options for token detection.
396425
* @param options.networkClientId - The ID of the network client to use.
397-
* @param options.accountAddress - The account address to use.
426+
* @param options.accountAddress - the selectedAddress against which to detect for token balances.
398427
*/
399428
async detectTokens({
400429
networkClientId,
@@ -403,25 +432,28 @@ export class TokenDetectionController extends StaticIntervalPollingController<
403432
networkClientId?: NetworkClientId;
404433
accountAddress?: string;
405434
} = {}): Promise<void> {
406-
if (
407-
!this.isActive ||
408-
!this.#isDetectionEnabledForNetwork ||
409-
!this.#isDetectionEnabledFromPreferences
410-
) {
435+
if (!this.isActive || !this.#isDetectionEnabledForNetwork) {
411436
return;
412437
}
413-
const { tokens } = this.#getTokensState();
414438
const selectedAddress = accountAddress ?? this.#selectedAddress;
415439
const chainId = this.#getCorrectChainId(networkClientId);
416440

417-
const tokensAddresses = tokens.map(
418-
/* istanbul ignore next*/ (token) => token.address.toLowerCase(),
419-
);
441+
if (
442+
!this.#isDetectionEnabledFromPreferences &&
443+
chainId !== ChainId.mainnet
444+
) {
445+
return;
446+
}
447+
const isTokenDetectionInactiveInMainnet =
448+
!this.#isDetectionEnabledFromPreferences && chainId === ChainId.mainnet;
420449
const { tokenList } = this.messagingSystem.call(
421450
'TokenListController:getState',
422451
);
452+
const tokenListUsed = isTokenDetectionInactiveInMainnet
453+
? STATIC_MAINNET_TOKEN_LIST
454+
: tokenList;
423455
const tokensToDetect: string[] = [];
424-
for (const address of Object.keys(tokenList)) {
456+
for (const address of Object.keys(tokenListUsed)) {
425457
if (!tokensAddresses.includes(address)) {
426458
tokensToDetect.push(address);
427459
}
@@ -461,7 +493,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
461493
);
462494
}
463495
const caseInsensitiveTokenKey =
464-
Object.keys(tokenList).find(
496+
Object.keys(tokenListUsed).find(
465497
(i) => i.toLowerCase() === tokenAddress.toLowerCase(),
466498
) ?? '';
467499

0 commit comments

Comments
 (0)