1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/app/scripts/controllers/detect-tokens.js

131 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-07-13 02:43:43 +02:00
const Web3 = require('web3')
2018-06-27 22:29:24 +02:00
const contracts = require('eth-contract-metadata')
2018-07-13 02:43:43 +02:00
const { warn } = require('loglevel')
const { MAINNET } = require('./network/enums')
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
2018-07-13 02:43:43 +02:00
const ERC20_ABI = [{'constant': true, 'inputs': [{'name': '_owner', 'type': 'address'}], 'name': 'balanceOf', 'outputs': [{'name': 'balance', 'type': 'uint256'}], 'payable': false, 'type': 'function'}]
2018-06-27 22:29:24 +02:00
/**
* A controller that polls for token exchange
* rates based on a user's current token list
*/
class DetectTokensController {
/**
* 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-06-28 01:54:43 +02:00
* For each token in eth-contract-metada, find check selectedAddress balance.
2018-06-27 22:29:24 +02:00
*
*/
async detectNewTokens () {
2018-07-20 01:46:46 +02:00
if (!this.isActive) { return }
2018-07-13 02:43:43 +02:00
if (this._network.store.getState().provider.type !== MAINNET) { return }
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()))) {
this.detectTokenBalance(contractAddress)
}
2018-06-27 22:29:24 +02:00
}
}
/**
* Find if selectedAddress has tokens with contract in contractAddress.
*
* @param {string} contractAddress Hex address of the token contract to explore.
2018-07-13 02:43:43 +02:00
* @returns {boolean} If balance is detected, token is added.
2018-06-27 22:29:24 +02:00
*
*/
2018-07-11 21:59:05 +02:00
async detectTokenBalance (contractAddress) {
2018-07-13 02:43:43 +02:00
const ethContract = this.web3.eth.contract(ERC20_ABI).at(contractAddress)
ethContract.balanceOf(this.selectedAddress, (error, result) => {
if (!error) {
if (!result.isZero()) {
this._preferences.addToken(contractAddress, contracts[contractAddress].symbol, contracts[contractAddress].decimals)
}
} else {
warn(`MetaMask - DetectTokensController balance fetch failed for ${contractAddress}.`, error)
}
})
2018-06-27 22:29:24 +02:00
}
/**
* Restart token detection polling period and call detectNewTokens
* in case of address change or user session initialization.
*
*/
restartTokenDetection () {
2018-07-21 01:58:03 +02:00
if (!(this.isActive && this.selectedAddress)) { return }
this.detectNewTokens()
this.interval = DEFAULT_INTERVAL
}
2018-06-27 22:29:24 +02:00
/**
* @type {Number}
*/
set interval (interval) {
this._handle && clearInterval(this._handle)
if (!interval) { return }
this._handle = setInterval(() => { this.detectNewTokens() }, interval)
2018-06-27 22:29:24 +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) {
if (!preferences) { return }
this._preferences = preferences
preferences.store.subscribe(({ tokens }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) })
preferences.store.subscribe(({ selectedAddress }) => {
if (this.selectedAddress !== selectedAddress) {
this.selectedAddress = selectedAddress
this.restartTokenDetection()
}
})
2018-06-27 22:29:24 +02:00
}
/**
2018-06-27 22:29:24 +02:00
* @type {Object}
*/
set network (network) {
if (!network) { return }
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-20 01:46:46 +02:00
* In setter when isUnlocked is updated to true, detectNewTokens and restart polling
* @type {Object}
*/
set keyringMemStore (keyringMemStore) {
if (!keyringMemStore) { return }
this._keyringMemStore = keyringMemStore
this._keyringMemStore.subscribe(({ isUnlocked }) => {
if (this.isUnlocked !== isUnlocked) {
this.isUnlocked = isUnlocked
2018-07-21 01:58:03 +02:00
if (isUnlocked) { this.restartTokenDetection() }
}
2018-07-20 01:46:46 +02:00
})
}
2018-07-21 22:03:31 +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
}
module.exports = DetectTokensController