2021-02-04 19:15:23 +01:00
|
|
|
import { warn } from 'loglevel';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { MINUTE } from '../../../shared/constants/time';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { CHAIN_IDS } from '../../../shared/constants/network';
|
2022-08-10 03:26:25 +02:00
|
|
|
import { STATIC_MAINNET_TOKEN_LIST } from '../../../shared/constants/tokens';
|
2022-04-13 18:23:41 +02:00
|
|
|
import { isTokenDetectionEnabledForNetwork } from '../../../shared/modules/network.utils';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';
|
2022-07-19 18:07:13 +02:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
AssetType,
|
|
|
|
TokenStandard,
|
2022-07-19 18:07:13 +02:00
|
|
|
} from '../../../shared/constants/transaction';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
} from '../../../shared/constants/metametrics';
|
2020-08-18 21:18:25 +02:00
|
|
|
|
2018-06-27 22:29:24 +02:00
|
|
|
// By default, poll every 3 minutes
|
2021-06-10 21:27:03 +02:00
|
|
|
const DEFAULT_INTERVAL = MINUTE * 3;
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-06-27 22:29:24 +02:00
|
|
|
/**
|
|
|
|
* A controller that polls for token exchange
|
|
|
|
* rates based on a user's current token list
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class DetectTokensController {
|
2018-06-27 22:29:24 +02:00
|
|
|
/**
|
|
|
|
* Creates a DetectTokensController
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} [config] - Options to configure controller
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param config.interval
|
|
|
|
* @param config.preferences
|
|
|
|
* @param config.network
|
|
|
|
* @param config.keyringMemStore
|
|
|
|
* @param config.tokenList
|
|
|
|
* @param config.tokensController
|
2022-04-13 18:23:41 +02:00
|
|
|
* @param config.assetsContractController
|
2022-05-11 22:27:58 +02:00
|
|
|
* @param config.trackMetaMetricsEvent
|
2023-06-22 20:46:09 +02:00
|
|
|
* @param config.messenger
|
2018-06-27 22:29:24 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor({
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger,
|
2020-11-03 00:41:28 +01:00
|
|
|
interval = DEFAULT_INTERVAL,
|
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList,
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2022-04-13 18:23:41 +02:00
|
|
|
assetsContractController = null,
|
2022-05-11 22:27:58 +02:00
|
|
|
trackMetaMetricsEvent,
|
2020-11-03 00:41:28 +01:00
|
|
|
} = {}) {
|
2023-06-22 20:46:09 +02:00
|
|
|
this.messenger = messenger;
|
2022-04-13 18:23:41 +02:00
|
|
|
this.assetsContractController = assetsContractController;
|
2021-09-10 19:37:19 +02:00
|
|
|
this.tokensController = tokensController;
|
2021-02-04 19:15:23 +01:00
|
|
|
this.preferences = preferences;
|
|
|
|
this.interval = interval;
|
|
|
|
this.network = network;
|
|
|
|
this.keyringMemStore = keyringMemStore;
|
2021-09-09 22:56:27 +02:00
|
|
|
this.tokenList = tokenList;
|
2022-08-10 03:26:25 +02:00
|
|
|
this.useTokenDetection =
|
|
|
|
this.preferences?.store.getState().useTokenDetection;
|
2021-09-10 19:37:19 +02:00
|
|
|
this.selectedAddress = this.preferences?.store.getState().selectedAddress;
|
|
|
|
this.tokenAddresses = this.tokensController?.state.tokens.map((token) => {
|
|
|
|
return token.address;
|
|
|
|
});
|
|
|
|
this.hiddenTokens = this.tokensController?.state.ignoredTokens;
|
2022-08-10 03:26:25 +02:00
|
|
|
this.detectedTokens = this.tokensController?.state.detectedTokens;
|
2023-06-22 20:46:09 +02:00
|
|
|
this.chainId = this.getChainIdFromNetworkStore();
|
2022-05-11 22:27:58 +02:00
|
|
|
this._trackMetaMetricsEvent = trackMetaMetricsEvent;
|
2021-09-10 19:37:19 +02:00
|
|
|
|
|
|
|
preferences?.store.subscribe(({ selectedAddress, useTokenDetection }) => {
|
|
|
|
if (
|
|
|
|
this.selectedAddress !== selectedAddress ||
|
|
|
|
this.useTokenDetection !== useTokenDetection
|
|
|
|
) {
|
|
|
|
this.selectedAddress = selectedAddress;
|
|
|
|
this.useTokenDetection = useTokenDetection;
|
2022-12-14 07:56:08 +01:00
|
|
|
this.restartTokenDetection({ selectedAddress });
|
2021-09-10 19:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
2022-04-13 18:23:41 +02:00
|
|
|
tokensController?.subscribe(
|
|
|
|
({ tokens = [], ignoredTokens = [], detectedTokens = [] }) => {
|
|
|
|
this.tokenAddresses = tokens.map((token) => {
|
|
|
|
return token.address;
|
|
|
|
});
|
|
|
|
this.hiddenTokens = ignoredTokens;
|
2022-08-10 03:26:25 +02:00
|
|
|
this.detectedTokens = detectedTokens;
|
2022-04-13 18:23:41 +02:00
|
|
|
},
|
|
|
|
);
|
2023-06-22 20:46:09 +02:00
|
|
|
messenger.subscribe('NetworkController:stateChange', () => {
|
|
|
|
if (this.chainId !== this.getChainIdFromNetworkStore()) {
|
|
|
|
const chainId = this.getChainIdFromNetworkStore();
|
|
|
|
this.chainId = chainId;
|
|
|
|
this.restartTokenDetection({ chainId: this.chainId });
|
|
|
|
}
|
|
|
|
});
|
2021-09-09 22:56:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
/**
|
2021-09-09 22:56:27 +02:00
|
|
|
* For each token in the tokenlist provided by the TokenListController, check selectedAddress balance.
|
2022-12-14 07:56:08 +01:00
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* @param options.selectedAddress - the selectedAddress against which to detect for token balances
|
|
|
|
* @param options.chainId - the chainId against which to detect for token balances
|
2018-06-27 22:29:24 +02:00
|
|
|
*/
|
2022-12-14 07:56:08 +01:00
|
|
|
async detectNewTokens({ selectedAddress, chainId } = {}) {
|
|
|
|
const addressAgainstWhichToDetect = selectedAddress ?? this.selectedAddress;
|
|
|
|
const chainIdAgainstWhichToDetect =
|
2023-06-22 20:46:09 +02:00
|
|
|
chainId ?? this.getChainIdFromNetworkStore();
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!this.isActive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2022-12-14 07:56:08 +01:00
|
|
|
if (!isTokenDetectionEnabledForNetwork(chainIdAgainstWhichToDetect)) {
|
2022-04-13 18:23:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-29 20:53:56 +02:00
|
|
|
if (
|
2022-08-10 03:26:25 +02:00
|
|
|
!this.useTokenDetection &&
|
2022-12-14 07:56:08 +01:00
|
|
|
chainIdAgainstWhichToDetect !== CHAIN_IDS.MAINNET
|
2021-09-29 20:53:56 +02:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-04-13 22:14:42 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
const isTokenDetectionInactiveInMainnet =
|
|
|
|
!this.useTokenDetection &&
|
2022-12-14 07:56:08 +01:00
|
|
|
chainIdAgainstWhichToDetect === CHAIN_IDS.MAINNET;
|
2022-08-10 03:26:25 +02:00
|
|
|
const { tokenList } = this._tokenList.state;
|
|
|
|
|
|
|
|
const tokenListUsed = isTokenDetectionInactiveInMainnet
|
|
|
|
? STATIC_MAINNET_TOKEN_LIST
|
|
|
|
: tokenList;
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokensToDetect = [];
|
2022-08-10 03:26:25 +02:00
|
|
|
for (const tokenAddress in tokenListUsed) {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
2023-01-17 16:23:04 +01:00
|
|
|
!this.tokenAddresses.find((address) =>
|
2021-09-09 22:56:27 +02:00
|
|
|
isEqualCaseInsensitive(address, tokenAddress),
|
|
|
|
) &&
|
|
|
|
!this.hiddenTokens.find((address) =>
|
|
|
|
isEqualCaseInsensitive(address, tokenAddress),
|
2022-04-13 18:23:41 +02:00
|
|
|
) &&
|
|
|
|
!this.detectedTokens.find(({ address }) =>
|
|
|
|
isEqualCaseInsensitive(address, tokenAddress),
|
2021-09-09 22:56:27 +02:00
|
|
|
)
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-09-09 22:56:27 +02:00
|
|
|
tokensToDetect.push(tokenAddress);
|
2018-07-13 02:43:43 +02:00
|
|
|
}
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
2021-09-09 22:56:27 +02:00
|
|
|
const sliceOfTokensToDetect = [
|
|
|
|
tokensToDetect.slice(0, 1000),
|
|
|
|
tokensToDetect.slice(1000, tokensToDetect.length - 1),
|
|
|
|
];
|
|
|
|
for (const tokensSlice of sliceOfTokensToDetect) {
|
|
|
|
let result;
|
|
|
|
try {
|
2022-08-10 03:26:25 +02:00
|
|
|
result = await this.assetsContractController.getBalancesInSingleCall(
|
2022-12-14 07:56:08 +01:00
|
|
|
addressAgainstWhichToDetect,
|
2022-08-10 03:26:25 +02:00
|
|
|
tokensSlice,
|
|
|
|
);
|
2021-09-09 22:56:27 +02:00
|
|
|
} catch (error) {
|
|
|
|
warn(
|
|
|
|
`MetaMask - DetectTokensController single call balance fetch failed`,
|
|
|
|
error,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-09-09 22:56:27 +02:00
|
|
|
return;
|
2019-01-22 19:23:11 +01:00
|
|
|
}
|
2021-09-10 19:37:19 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
const tokensWithBalance = [];
|
|
|
|
const eventTokensDetails = [];
|
|
|
|
if (result) {
|
|
|
|
const nonZeroTokenAddresses = Object.keys(result);
|
|
|
|
for (const nonZeroTokenAddress of nonZeroTokenAddresses) {
|
2022-09-15 17:23:18 +02:00
|
|
|
const { address, symbol, decimals } =
|
2022-08-10 03:26:25 +02:00
|
|
|
tokenListUsed[nonZeroTokenAddress];
|
2022-05-11 22:27:58 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
eventTokensDetails.push(`${symbol} - ${address}`);
|
2022-05-11 22:27:58 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
tokensWithBalance.push({
|
|
|
|
address,
|
|
|
|
symbol,
|
|
|
|
decimals,
|
|
|
|
});
|
|
|
|
}
|
2022-05-11 22:27:58 +02:00
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
if (tokensWithBalance.length > 0) {
|
|
|
|
this._trackMetaMetricsEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.TokenDetected,
|
|
|
|
category: MetaMetricsEventCategory.Wallet,
|
2022-08-10 03:26:25 +02:00
|
|
|
properties: {
|
|
|
|
tokens: eventTokensDetails,
|
2023-01-18 15:47:29 +01:00
|
|
|
token_standard: TokenStandard.ERC20,
|
|
|
|
asset_type: AssetType.token,
|
2022-08-10 03:26:25 +02:00
|
|
|
},
|
|
|
|
});
|
2022-12-14 07:56:08 +01:00
|
|
|
await this.tokensController.addDetectedTokens(tokensWithBalance, {
|
|
|
|
selectedAddress: addressAgainstWhichToDetect,
|
|
|
|
chainId: chainIdAgainstWhichToDetect,
|
|
|
|
});
|
2022-04-13 18:23:41 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-09 22:56:27 +02:00
|
|
|
}
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
/**
|
|
|
|
* Restart token detection polling period and call detectNewTokens
|
|
|
|
* in case of address change or user session initialization.
|
|
|
|
*
|
2022-12-14 07:56:08 +01:00
|
|
|
* @param options
|
|
|
|
* @param options.selectedAddress - the selectedAddress against which to detect for token balances
|
|
|
|
* @param options.chainId - the chainId against which to detect for token balances
|
2018-07-19 21:56:38 +02:00
|
|
|
*/
|
2022-12-14 07:56:08 +01:00
|
|
|
restartTokenDetection({ selectedAddress, chainId } = {}) {
|
|
|
|
const addressAgainstWhichToDetect = selectedAddress ?? this.selectedAddress;
|
|
|
|
const chainIdAgainstWhichToDetect = chainId ?? this.chainId;
|
|
|
|
if (!(this.isActive && addressAgainstWhichToDetect)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2022-12-14 07:56:08 +01:00
|
|
|
this.detectNewTokens({
|
|
|
|
selectedAddress: addressAgainstWhichToDetect,
|
|
|
|
chainId: chainIdAgainstWhichToDetect,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
this.interval = DEFAULT_INTERVAL;
|
2018-07-19 21:56:38 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 20:46:09 +02:00
|
|
|
getChainIdFromNetworkStore() {
|
|
|
|
return this.network?.state.providerConfig.chainId;
|
2022-08-10 03:26:25 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:06:44 +02:00
|
|
|
/* eslint-disable accessor-pairs */
|
2018-06-27 22:29:24 +02:00
|
|
|
/**
|
2022-01-07 16:57:33 +01:00
|
|
|
* @type {number}
|
2018-06-27 22:29:24 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
set interval(interval) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._handle && clearInterval(this._handle);
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!interval) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
|
|
|
this._handle = setInterval(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.detectNewTokens();
|
|
|
|
}, interval);
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
/**
|
2018-07-20 01:46:46 +02:00
|
|
|
* In setter when isUnlocked is updated to true, detectNewTokens and restart polling
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @type {object}
|
2018-07-20 01:46:46 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
set keyringMemStore(keyringMemStore) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!keyringMemStore) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
this._keyringMemStore = keyringMemStore;
|
2018-07-20 01:46:46 +02:00
|
|
|
this._keyringMemStore.subscribe(({ isUnlocked }) => {
|
|
|
|
if (this.isUnlocked !== isUnlocked) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.isUnlocked = isUnlocked;
|
2019-11-20 01:03:20 +01:00
|
|
|
if (isUnlocked) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.restartTokenDetection();
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-07-19 21:56:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-20 01:46:46 +02:00
|
|
|
}
|
2018-07-21 22:03:31 +02:00
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @type {object}
|
2021-09-09 22:56:27 +02:00
|
|
|
*/
|
|
|
|
set tokenList(tokenList) {
|
|
|
|
if (!tokenList) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._tokenList = tokenList;
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:09:37 +02:00
|
|
|
/**
|
|
|
|
* Internal isActive state
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @type {object}
|
2018-07-21 02:09:37 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
get isActive() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.isOpen && this.isUnlocked;
|
2018-07-21 01:58:03 +02:00
|
|
|
}
|
2020-10-22 19:06:44 +02:00
|
|
|
/* eslint-enable accessor-pairs */
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|