2021-02-04 19:15:23 +01:00
|
|
|
import Web3 from 'web3';
|
|
|
|
import contracts from '@metamask/contract-metadata';
|
|
|
|
import { warn } from 'loglevel';
|
|
|
|
import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi';
|
|
|
|
import { MAINNET_CHAIN_ID } from '../../../shared/constants/network';
|
|
|
|
import { SINGLE_CALL_BALANCES_ADDRESS } from '../constants/contracts';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { MINUTE } from '../../../shared/constants/time';
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} [config] - Options to configure controller
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor({
|
|
|
|
interval = DEFAULT_INTERVAL,
|
|
|
|
preferences,
|
|
|
|
network,
|
|
|
|
keyringMemStore,
|
|
|
|
} = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.preferences = preferences;
|
|
|
|
this.interval = interval;
|
|
|
|
this.network = network;
|
|
|
|
this.keyringMemStore = keyringMemStore;
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
/**
|
2020-12-01 23:55:01 +01:00
|
|
|
* For each token in @metamask/contract-metadata, find check selectedAddress balance.
|
2018-06-27 22:29:24 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async detectNewTokens() {
|
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
|
|
|
}
|
2021-01-08 15:25:46 +01:00
|
|
|
if (this._network.store.getState().provider.chainId !== MAINNET_CHAIN_ID) {
|
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
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokensToDetect = [];
|
|
|
|
this.web3.setProvider(this._network._provider);
|
2018-07-11 21:59:05 +02:00
|
|
|
for (const contractAddress in contracts) {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
contracts[contractAddress].erc20 &&
|
2020-12-08 21:38:00 +01:00
|
|
|
!this.tokenAddresses.includes(contractAddress.toLowerCase()) &&
|
|
|
|
!this.hiddenTokens.includes(contractAddress.toLowerCase())
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokensToDetect.push(contractAddress);
|
2018-07-13 02:43:43 +02:00
|
|
|
}
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
2019-01-22 19:23:11 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let result;
|
2020-04-13 22:14:42 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
result = await this._getTokenBalances(tokensToDetect);
|
2020-04-13 22:14:42 +02:00
|
|
|
} catch (error) {
|
2020-11-03 00:41:28 +01:00
|
|
|
warn(
|
|
|
|
`MetaMask - DetectTokensController single call balance fetch failed`,
|
|
|
|
error,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return;
|
2020-04-13 22:14:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tokensToDetect.forEach((tokenAddress, index) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const balance = result[index];
|
2020-04-13 22:14:42 +02:00
|
|
|
if (balance && !balance.isZero()) {
|
2020-11-03 00:41:28 +01:00
|
|
|
this._preferences.addToken(
|
|
|
|
tokenAddress,
|
|
|
|
contracts[tokenAddress].symbol,
|
|
|
|
contracts[tokenAddress].decimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-01-22 19:23:11 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async _getTokenBalances(tokens) {
|
|
|
|
const ethContract = this.web3.eth
|
|
|
|
.contract(SINGLE_CALL_BALANCES_ABI)
|
2021-02-04 19:15:23 +01:00
|
|
|
.at(SINGLE_CALL_BALANCES_ADDRESS);
|
2020-04-13 22:14:42 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ethContract.balances([this.selectedAddress], tokens, (error, result) => {
|
|
|
|
if (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return reject(error);
|
2018-07-13 02:43:43 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
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.
|
|
|
|
*
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
restartTokenDetection() {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!(this.isActive && this.selectedAddress)) {
|
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.detectNewTokens();
|
|
|
|
this.interval = DEFAULT_INTERVAL;
|
2018-07-19 21:56:38 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:06:44 +02:00
|
|
|
/* eslint-disable accessor-pairs */
|
2018-06-27 22:29:24 +02:00
|
|
|
/**
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* In setter when selectedAddress is changed, detectNewTokens and restart polling
|
2018-06-27 22:29:24 +02:00
|
|
|
* @type {Object}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
set preferences(preferences) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!preferences) {
|
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._preferences = preferences;
|
|
|
|
const currentTokens = preferences.store.getState().tokens;
|
2020-04-13 22:14:42 +02:00
|
|
|
this.tokenAddresses = currentTokens
|
|
|
|
? currentTokens.map((token) => token.address)
|
2021-02-04 19:15:23 +01:00
|
|
|
: [];
|
|
|
|
this.hiddenTokens = preferences.store.getState().hiddenTokens;
|
2020-12-08 21:38:00 +01:00
|
|
|
preferences.store.subscribe(({ tokens = [], hiddenTokens = [] }) => {
|
2020-04-13 22:14:42 +02:00
|
|
|
this.tokenAddresses = tokens.map((token) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return token.address;
|
|
|
|
});
|
|
|
|
this.hiddenTokens = hiddenTokens;
|
|
|
|
});
|
2018-07-19 21:56:38 +02:00
|
|
|
preferences.store.subscribe(({ selectedAddress }) => {
|
|
|
|
if (this.selectedAddress !== selectedAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.selectedAddress = selectedAddress;
|
|
|
|
this.restartTokenDetection();
|
2018-07-19 21:56:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
/**
|
2018-06-27 22:29:24 +02:00
|
|
|
* @type {Object}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
set network(network) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!network) {
|
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._network = network;
|
|
|
|
this.web3 = new Web3(network._provider);
|
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
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
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
|
|
|
|
2018-07-21 02:09:37 +02:00
|
|
|
/**
|
|
|
|
* Internal isActive state
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
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
|
|
|
}
|