2020-01-09 04:34:58 +01:00
|
|
|
import Web3 from 'web3'
|
|
|
|
import contracts from 'eth-contract-metadata'
|
|
|
|
import { warn } from 'loglevel'
|
2020-08-18 21:18:25 +02:00
|
|
|
import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi'
|
2020-01-09 04:34:58 +01:00
|
|
|
import { MAINNET } from './network/enums'
|
2020-08-18 21:18:25 +02:00
|
|
|
|
2018-06-27 22:29:24 +02:00
|
|
|
// By default, poll every 3 minutes
|
2018-06-28 04:18:06 +02:00
|
|
|
const DEFAULT_INTERVAL = 180 * 1000
|
2019-01-22 19:23:11 +01:00
|
|
|
const SINGLE_CALL_BALANCES_ADDRESS = '0xb1f8e55c7f64d203c1400b9d8555d050f94adf39'
|
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 {
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-06-27 22:29:24 +02:00
|
|
|
/**
|
|
|
|
* Creates a DetectTokensController
|
|
|
|
*
|
|
|
|
* @param {Object} [config] - Options to configure controller
|
|
|
|
*/
|
2018-07-20 01:46:46 +02:00
|
|
|
constructor ({ interval = DEFAULT_INTERVAL, preferences, network, keyringMemStore } = {}) {
|
2018-06-27 22:29:24 +02:00
|
|
|
this.preferences = preferences
|
|
|
|
this.interval = interval
|
|
|
|
this.network = network
|
2018-07-20 01:46:46 +02:00
|
|
|
this.keyringMemStore = keyringMemStore
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
/**
|
2020-07-20 19:02:49 +02:00
|
|
|
* For each token in eth-contract-metadata, find check selectedAddress balance.
|
2018-06-27 22:29:24 +02:00
|
|
|
*/
|
2018-07-19 21:56:38 +02:00
|
|
|
async detectNewTokens () {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!this.isActive) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (this._network.store.getState().provider.type !== MAINNET) {
|
|
|
|
return
|
|
|
|
}
|
2020-04-13 22:14:42 +02:00
|
|
|
|
2019-01-22 19:23:11 +01:00
|
|
|
const tokensToDetect = []
|
2018-07-13 02:43:43 +02:00
|
|
|
this.web3.setProvider(this._network._provider)
|
2018-07-11 21:59:05 +02:00
|
|
|
for (const contractAddress in contracts) {
|
2018-07-13 02:43:43 +02:00
|
|
|
if (contracts[contractAddress].erc20 && !(this.tokenAddresses.includes(contractAddress.toLowerCase()))) {
|
2019-01-22 19:23:11 +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
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await this._getTokenBalances(tokensToDetect)
|
|
|
|
} catch (error) {
|
|
|
|
warn(`MetaMask - DetectTokensController single call balance fetch failed`, error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tokensToDetect.forEach((tokenAddress, index) => {
|
|
|
|
const balance = result[index]
|
|
|
|
if (balance && !balance.isZero()) {
|
|
|
|
this._preferences.addToken(tokenAddress, contracts[tokenAddress].symbol, contracts[tokenAddress].decimals)
|
2019-01-22 19:23:11 +01:00
|
|
|
}
|
|
|
|
})
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:14:42 +02:00
|
|
|
async _getTokenBalances (tokens) {
|
|
|
|
const ethContract = this.web3.eth.contract(SINGLE_CALL_BALANCES_ABI).at(SINGLE_CALL_BALANCES_ADDRESS)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ethContract.balances([this.selectedAddress], tokens, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error)
|
2018-07-13 02:43:43 +02:00
|
|
|
}
|
2020-04-13 22:14:42 +02:00
|
|
|
return resolve(result)
|
|
|
|
})
|
2018-07-13 02:43:43 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
restartTokenDetection () {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!(this.isActive && this.selectedAddress)) {
|
|
|
|
return
|
|
|
|
}
|
2018-07-21 01:58:03 +02:00
|
|
|
this.detectNewTokens()
|
|
|
|
this.interval = DEFAULT_INTERVAL
|
2018-07-19 21:56:38 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 22:29:24 +02:00
|
|
|
/**
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
|
|
|
set interval (interval) {
|
|
|
|
this._handle && clearInterval(this._handle)
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!interval) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this._handle = setInterval(() => {
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
set preferences (preferences) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!preferences) {
|
|
|
|
return
|
|
|
|
}
|
2018-06-27 22:29:24 +02:00
|
|
|
this._preferences = preferences
|
2020-04-13 22:14:42 +02:00
|
|
|
const currentTokens = preferences.store.getState().tokens
|
|
|
|
this.tokenAddresses = currentTokens
|
|
|
|
? currentTokens.map((token) => token.address)
|
|
|
|
: []
|
2019-11-20 01:03:20 +01:00
|
|
|
preferences.store.subscribe(({ tokens = [] }) => {
|
2020-04-13 22:14:42 +02:00
|
|
|
this.tokenAddresses = tokens.map((token) => {
|
|
|
|
return token.address
|
2019-11-20 01:03:20 +01:00
|
|
|
})
|
|
|
|
})
|
2018-07-19 21:56:38 +02:00
|
|
|
preferences.store.subscribe(({ selectedAddress }) => {
|
|
|
|
if (this.selectedAddress !== selectedAddress) {
|
|
|
|
this.selectedAddress = selectedAddress
|
|
|
|
this.restartTokenDetection()
|
|
|
|
}
|
|
|
|
})
|
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}
|
|
|
|
*/
|
|
|
|
set network (network) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!network) {
|
|
|
|
return
|
|
|
|
}
|
2018-06-27 22:29:24 +02:00
|
|
|
this._network = network
|
2018-07-13 02:43:43 +02:00
|
|
|
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}
|
|
|
|
*/
|
|
|
|
set keyringMemStore (keyringMemStore) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!keyringMemStore) {
|
|
|
|
return
|
|
|
|
}
|
2018-07-20 01:46:46 +02:00
|
|
|
this._keyringMemStore = keyringMemStore
|
|
|
|
this._keyringMemStore.subscribe(({ isUnlocked }) => {
|
|
|
|
if (this.isUnlocked !== isUnlocked) {
|
|
|
|
this.isUnlocked = isUnlocked
|
2019-11-20 01:03:20 +01:00
|
|
|
if (isUnlocked) {
|
|
|
|
this.restartTokenDetection()
|
|
|
|
}
|
2018-07-19 21:56:38 +02: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}
|
|
|
|
*/
|
2018-07-21 01:58:03 +02:00
|
|
|
get isActive () {
|
|
|
|
return this.isOpen && this.isUnlocked
|
|
|
|
}
|
2018-06-27 22:29:24 +02:00
|
|
|
}
|