From bfac9c2c2dfd0c5f55cb794214791cf78cca75c0 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 27 Jun 2018 16:29:24 -0400 Subject: detect tokens polling --- app/scripts/controllers/detect-tokens.js | 97 ++++++++++++++++++++++++++++++++ app/scripts/metamask-controller.js | 8 +++ 2 files changed, 105 insertions(+) create mode 100644 app/scripts/controllers/detect-tokens.js (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js new file mode 100644 index 000000000..a7ddace19 --- /dev/null +++ b/app/scripts/controllers/detect-tokens.js @@ -0,0 +1,97 @@ +const ObservableStore = require('obs-store') +const { warn } = require('loglevel') +const contracts = require('eth-contract-metadata') +const { + ROPSTEN, + RINKEBY, + KOVAN, + MAINNET, + OCALHOST, + } = require('./network/enums') + +// By default, poll every 3 minutes +const DEFAULT_INTERVAL = 180 * 1000 + +/** + * 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 + */ + constructor ({ interval = DEFAULT_INTERVAL, preferences, network } = {}) { + this.preferences = preferences + this.interval = interval + this.network = network + } + + /** + * For each token in eth-contract=metada, find check selectedAddress balance. + * + */ + async exploreNewTokens () { + if (!this.isActive) { return } + if (this._network.getProviderConfig().type !== MAINNET) { return } + var tokens = this._preferences.store.getState().tokens + let detectedTokenAddress, token + for (const address in contracts) { + const contract = contracts[address] + if (contract.erc20 && !(address in tokens)) { + detectedTokenAddress = await this.fetchContractAccountBalance(address) + if (detectedTokenAddress) { + token = contracts[detectedTokenAddress] + this._preferences.addToken(detectedTokenAddress, token['symbol'], token['decimals']) + } + } + // etherscan restriction, 5 request/second, lazy scan + setTimeout(() => {}, 200) + } + } + + /** + * Find if selectedAddress has tokens with contract in contractAddress. + * + * @param {string} contractAddress Hex address of the token contract to explore. + * @returns {string} Contract address to be added to tokens. + * + */ + async fetchContractAccountBalance (contractAddress) { + const address = this._preferences.store.getState().selectedAddress + const response = await fetch(`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=NCKS6GTY41KPHWRJB62ES1MDNRBIT174PV`) + const parsedResponse = await response.json() + if (parsedResponse.result !== '0') { + return contractAddress + } + return null + } + + /** + * @type {Number} + */ + set interval (interval) { + this._handle && clearInterval(this._handle) + if (!interval) { return } + this._handle = setInterval(() => { this.exploreNewTokens() }, interval) + } + + /** + * @type {Object} + */ + set preferences (preferences) { + if (!preferences) { return } + this._preferences = preferences + } + + /** + * @type {Object} + */ + set network (network) { + if (!network) { return } + this._network = network + } +} + +module.exports = DetectTokensController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index d40a351a5..37a31a28c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -35,6 +35,7 @@ const TypedMessageManager = require('./lib/typed-message-manager') const TransactionController = require('./controllers/transactions') const BalancesController = require('./controllers/computed-balances') const TokenRatesController = require('./controllers/token-rates') +const DetectTokensController = require('./controllers/detect-tokens') const ConfigManager = require('./lib/config-manager') const nodeify = require('./lib/nodeify') const accountImporter = require('./account-import-strategies') @@ -112,6 +113,12 @@ module.exports = class MetamaskController extends EventEmitter { preferences: this.preferencesController.store, }) + // detect tokens controller + this.detectTokensController = new DetectTokensController({ + preferences: this.preferencesController, + network: this.networkController, + }) + this.recentBlocksController = new RecentBlocksController({ blockTracker: this.blockTracker, provider: this.provider, @@ -1276,5 +1283,6 @@ module.exports = class MetamaskController extends EventEmitter { */ set isClientOpenAndUnlocked (active) { this.tokenRatesController.isActive = active + this.detectTokensController.isActive = active } } -- cgit v1.2.3 From 0e863d5fab00eb83c908fb49f2939534bdfe1162 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 27 Jun 2018 19:54:43 -0400 Subject: network store to detect token --- app/scripts/controllers/detect-tokens.js | 18 ++++++++++-------- app/scripts/metamask-controller.js | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index a7ddace19..4d364bd37 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -10,7 +10,7 @@ const { } = require('./network/enums') // By default, poll every 3 minutes -const DEFAULT_INTERVAL = 180 * 1000 +const DEFAULT_INTERVAL = 15 * 1000 /** * A controller that polls for token exchange @@ -26,23 +26,23 @@ class DetectTokensController { this.preferences = preferences this.interval = interval this.network = network + this.contracts = contracts } /** - * For each token in eth-contract=metada, find check selectedAddress balance. + * For each token in eth-contract-metada, find check selectedAddress balance. * */ async exploreNewTokens () { if (!this.isActive) { return } - if (this._network.getProviderConfig().type !== MAINNET) { return } - var tokens = this._preferences.store.getState().tokens + if (this._network.getState().provider.type !== MAINNET) { return } let detectedTokenAddress, token - for (const address in contracts) { - const contract = contracts[address] - if (contract.erc20 && !(address in tokens)) { + for (const address in this.contracts) { + const contract = this.contracts[address] + if (contract.erc20 && !(address in this.tokens)) { detectedTokenAddress = await this.fetchContractAccountBalance(address) if (detectedTokenAddress) { - token = contracts[detectedTokenAddress] + token = this.contracts[detectedTokenAddress] this._preferences.addToken(detectedTokenAddress, token['symbol'], token['decimals']) } } @@ -83,6 +83,8 @@ class DetectTokensController { set preferences (preferences) { if (!preferences) { return } this._preferences = preferences + this.tokens = preferences.store.getState().tokens + } /** diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 37a31a28c..7b60a6c6f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -116,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter { // detect tokens controller this.detectTokensController = new DetectTokensController({ preferences: this.preferencesController, - network: this.networkController, + network: this.networkController.store, }) this.recentBlocksController = new RecentBlocksController({ -- cgit v1.2.3 From 6284e664810fe0dcdcb35e55cc57bc11b9298dbb Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 27 Jun 2018 22:18:06 -0400 Subject: tests for spec --- app/scripts/controllers/detect-tokens.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index 4d364bd37..8a6ba9c9a 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -10,7 +10,7 @@ const { } = require('./network/enums') // By default, poll every 3 minutes -const DEFAULT_INTERVAL = 15 * 1000 +const DEFAULT_INTERVAL = 180 * 1000 /** * A controller that polls for token exchange -- cgit v1.2.3 From 03fd4355af8ceae9a1d2cad04e12a4f86a9f36b7 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Tue, 3 Jul 2018 12:53:06 -0400 Subject: passtest-lint --- app/scripts/controllers/detect-tokens.js | 6 ------ 1 file changed, 6 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index 8a6ba9c9a..1ea855356 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -1,12 +1,6 @@ -const ObservableStore = require('obs-store') -const { warn } = require('loglevel') const contracts = require('eth-contract-metadata') const { - ROPSTEN, - RINKEBY, - KOVAN, MAINNET, - OCALHOST, } = require('./network/enums') // By default, poll every 3 minutes -- cgit v1.2.3 From 910713c6b3c5b8f865fdcb989bfe3ee0b14eb364 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 11 Jul 2018 15:59:05 -0400 Subject: improve tests --- app/scripts/controllers/detect-tokens.js | 29 +++++++++++++---------------- app/scripts/metamask-controller.js | 2 +- 2 files changed, 14 insertions(+), 17 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index 1ea855356..fd8412078 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -20,7 +20,6 @@ class DetectTokensController { this.preferences = preferences this.interval = interval this.network = network - this.contracts = contracts } /** @@ -30,18 +29,16 @@ class DetectTokensController { async exploreNewTokens () { if (!this.isActive) { return } if (this._network.getState().provider.type !== MAINNET) { return } - let detectedTokenAddress, token - for (const address in this.contracts) { - const contract = this.contracts[address] - if (contract.erc20 && !(address in this.tokens)) { - detectedTokenAddress = await this.fetchContractAccountBalance(address) - if (detectedTokenAddress) { - token = this.contracts[detectedTokenAddress] - this._preferences.addToken(detectedTokenAddress, token['symbol'], token['decimals']) + let detectedTokenBalance, token + for (const contractAddress in contracts) { + const contract = contracts[contractAddress] + if (contract.erc20 && !(contractAddress in this.tokens)) { + detectedTokenBalance = await this.detectTokenBalance(contractAddress) + if (detectedTokenBalance) { + token = contracts[contractAddress] + this._preferences.addToken(contractAddress, token['symbol'], token['decimals']) } } - // etherscan restriction, 5 request/second, lazy scan - setTimeout(() => {}, 200) } } @@ -49,17 +46,17 @@ class DetectTokensController { * Find if selectedAddress has tokens with contract in contractAddress. * * @param {string} contractAddress Hex address of the token contract to explore. - * @returns {string} Contract address to be added to tokens. + * @returns {boolean} If balance is detected in token contract for address. * */ - async fetchContractAccountBalance (contractAddress) { + async detectTokenBalance (contractAddress) { const address = this._preferences.store.getState().selectedAddress const response = await fetch(`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=NCKS6GTY41KPHWRJB62ES1MDNRBIT174PV`) const parsedResponse = await response.json() if (parsedResponse.result !== '0') { - return contractAddress + return true } - return null + return false } /** @@ -81,7 +78,7 @@ class DetectTokensController { } - /** + /** * @type {Object} */ set network (network) { diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 9a93cf584..eed3fc8e7 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -113,7 +113,7 @@ module.exports = class MetamaskController extends EventEmitter { preferences: this.preferencesController.store, }) - // detect tokens controller + // detect tokens controller this.detectTokensController = new DetectTokensController({ preferences: this.preferencesController, network: this.networkController.store, -- cgit v1.2.3 From 2fffe098736e2461b9238c7dcd91f9ef3d61dcc1 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 12 Jul 2018 20:43:43 -0400 Subject: detect tokens through infura --- app/scripts/controllers/detect-tokens.js | 45 +++++++++++++++++--------------- app/scripts/metamask-controller.js | 2 +- 2 files changed, 25 insertions(+), 22 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index fd8412078..e245a7f9b 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -1,10 +1,12 @@ +const Web3 = require('web3') const contracts = require('eth-contract-metadata') +const { warn } = require('loglevel') const { MAINNET, } = require('./network/enums') - // By default, poll every 3 minutes const DEFAULT_INTERVAL = 180 * 1000 +const ERC20_ABI = [{'constant': true, 'inputs': [{'name': '_owner', 'type': 'address'}], 'name': 'balanceOf', 'outputs': [{'name': 'balance', 'type': 'uint256'}], 'payable': false, 'type': 'function'}] /** * A controller that polls for token exchange @@ -28,17 +30,12 @@ class DetectTokensController { */ async exploreNewTokens () { if (!this.isActive) { return } - if (this._network.getState().provider.type !== MAINNET) { return } - let detectedTokenBalance, token + if (this._network.store.getState().provider.type !== MAINNET) { return } + this.web3.setProvider(this._network._provider) for (const contractAddress in contracts) { - const contract = contracts[contractAddress] - if (contract.erc20 && !(contractAddress in this.tokens)) { - detectedTokenBalance = await this.detectTokenBalance(contractAddress) - if (detectedTokenBalance) { - token = contracts[contractAddress] - this._preferences.addToken(contractAddress, token['symbol'], token['decimals']) - } - } + if (contracts[contractAddress].erc20 && !(this.tokenAddresses.includes(contractAddress.toLowerCase()))) { + this.detectTokenBalance(contractAddress) + } } } @@ -46,17 +43,20 @@ class DetectTokensController { * Find if selectedAddress has tokens with contract in contractAddress. * * @param {string} contractAddress Hex address of the token contract to explore. - * @returns {boolean} If balance is detected in token contract for address. + * @returns {boolean} If balance is detected, token is added. * */ async detectTokenBalance (contractAddress) { - const address = this._preferences.store.getState().selectedAddress - const response = await fetch(`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=NCKS6GTY41KPHWRJB62ES1MDNRBIT174PV`) - const parsedResponse = await response.json() - if (parsedResponse.result !== '0') { - return true - } - return false + 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) + } + }) } /** @@ -74,8 +74,10 @@ class DetectTokensController { set preferences (preferences) { if (!preferences) { return } this._preferences = preferences - this.tokens = preferences.store.getState().tokens - + this.tokenAddresses = preferences.store.getState().tokens.map((obj) => { return obj.address }) + this.selectedAddress = preferences.store.getState().selectedAddress + preferences.store.subscribe(({ tokens = [] }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) }) + preferences.store.subscribe(({ selectedAddress = [] }) => { this.selectedAddress = selectedAddress }) } /** @@ -84,6 +86,7 @@ class DetectTokensController { set network (network) { if (!network) { return } this._network = network + this.web3 = new Web3(network._provider) } } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index eed3fc8e7..39527ae3b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -116,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter { // detect tokens controller this.detectTokensController = new DetectTokensController({ preferences: this.preferencesController, - network: this.networkController.store, + network: this.networkController, }) this.recentBlocksController = new RecentBlocksController({ -- cgit v1.2.3 From 3b97d816ffcaebc7606d4564ea95918f647ba413 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 19 Jul 2018 15:56:38 -0400 Subject: detect tokens when submit password and new account selected --- app/scripts/controllers/detect-tokens.js | 52 ++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index e245a7f9b..db21f7489 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -1,9 +1,7 @@ const Web3 = require('web3') const contracts = require('eth-contract-metadata') const { warn } = require('loglevel') -const { - MAINNET, - } = require('./network/enums') +const { MAINNET } = require('./network/enums') // By default, poll every 3 minutes const DEFAULT_INTERVAL = 180 * 1000 const ERC20_ABI = [{'constant': true, 'inputs': [{'name': '_owner', 'type': 'address'}], 'name': 'balanceOf', 'outputs': [{'name': 'balance', 'type': 'uint256'}], 'payable': false, 'type': 'function'}] @@ -22,14 +20,15 @@ class DetectTokensController { this.preferences = preferences this.interval = interval this.network = network + this._isActive = false } - /** + /** * For each token in eth-contract-metada, find check selectedAddress balance. * */ - async exploreNewTokens () { - if (!this.isActive) { return } + async detectNewTokens () { + if (!this._isActive) { return } if (this._network.store.getState().provider.type !== MAINNET) { return } this.web3.setProvider(this._network._provider) for (const contractAddress in contracts) { @@ -59,28 +58,44 @@ class DetectTokensController { }) } + /** + * Restart token detection polling period and call detectNewTokens + * in case of address change or user session initialization. + * + */ + restartTokenDetection () { + if (this._isActive && this.selectedAddress) { + this.detectNewTokens() + this.interval = DEFAULT_INTERVAL + } + } + /** * @type {Number} */ set interval (interval) { this._handle && clearInterval(this._handle) if (!interval) { return } - this._handle = setInterval(() => { this.exploreNewTokens() }, interval) + this._handle = setInterval(() => { this.detectNewTokens() }, interval) } - /** + /** + * In setter when selectedAddress is changed, detectNewTokens and restart polling * @type {Object} */ set preferences (preferences) { if (!preferences) { return } this._preferences = preferences - this.tokenAddresses = preferences.store.getState().tokens.map((obj) => { return obj.address }) - this.selectedAddress = preferences.store.getState().selectedAddress - preferences.store.subscribe(({ tokens = [] }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) }) - preferences.store.subscribe(({ selectedAddress = [] }) => { this.selectedAddress = selectedAddress }) + 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() + } + }) } - /** + /** * @type {Object} */ set network (network) { @@ -88,6 +103,17 @@ class DetectTokensController { this._network = network this.web3 = new Web3(network._provider) } + + /** + * In setter, when _isActive is changed, detectNewTokens and restart polling + * @type {Object} + */ + set isActive (active) { + if (this._isActive !== active) { + this._isActive = active + this.restartTokenDetection() + } + } } module.exports = DetectTokensController -- cgit v1.2.3 From 009b1cefbe3d19dcad01078927b1a55c3439b22f Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 19 Jul 2018 19:46:46 -0400 Subject: keyring unlocked detect and unit tests --- app/scripts/controllers/detect-tokens.js | 28 ++++++++++++++++------------ app/scripts/metamask-controller.js | 16 +++++++++------- 2 files changed, 25 insertions(+), 19 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index db21f7489..f1810cfa1 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -16,11 +16,11 @@ class DetectTokensController { * * @param {Object} [config] - Options to configure controller */ - constructor ({ interval = DEFAULT_INTERVAL, preferences, network } = {}) { + constructor ({ interval = DEFAULT_INTERVAL, preferences, network, keyringMemStore } = {}) { this.preferences = preferences this.interval = interval this.network = network - this._isActive = false + this.keyringMemStore = keyringMemStore } /** @@ -28,7 +28,7 @@ class DetectTokensController { * */ async detectNewTokens () { - if (!this._isActive) { return } + if (!this.isActive) { return } if (this._network.store.getState().provider.type !== MAINNET) { return } this.web3.setProvider(this._network._provider) for (const contractAddress in contracts) { @@ -64,7 +64,7 @@ class DetectTokensController { * */ restartTokenDetection () { - if (this._isActive && this.selectedAddress) { + if (this.isActive && this.selectedAddress) { this.detectNewTokens() this.interval = DEFAULT_INTERVAL } @@ -105,15 +105,19 @@ class DetectTokensController { } /** - * In setter, when _isActive is changed, detectNewTokens and restart polling - * @type {Object} - */ - set isActive (active) { - if (this._isActive !== active) { - this._isActive = active - this.restartTokenDetection() + * 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) { + if (isUnlocked) { this.restartTokenDetection() } + this.isUnlocked = isUnlocked } - } + }) + } } module.exports = DetectTokensController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 39527ae3b..4e97ce583 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -113,12 +113,6 @@ module.exports = class MetamaskController extends EventEmitter { preferences: this.preferencesController.store, }) - // detect tokens controller - this.detectTokensController = new DetectTokensController({ - preferences: this.preferencesController, - network: this.networkController, - }) - this.recentBlocksController = new RecentBlocksController({ blockTracker: this.blockTracker, provider: this.provider, @@ -151,6 +145,13 @@ module.exports = class MetamaskController extends EventEmitter { this.accountTracker.syncWithAddresses(addresses) }) + // detect tokens controller + this.detectTokensController = new DetectTokensController({ + preferences: this.preferencesController, + network: this.networkController, + keyringMemStore: this.keyringController.memStore, + }) + // address book controller this.addressBookController = new AddressBookController({ initState: initState.AddressBookController, @@ -1276,7 +1277,8 @@ module.exports = class MetamaskController extends EventEmitter { } /** - * A method for activating the retrieval of price data, which should only be fetched when the UI is visible. + * A method for activating the retrieval of price data and auto detect tokens, + * which should only be fetched when the UI is visible. * @private * @param {boolean} active - True if price data should be getting fetched. */ -- cgit v1.2.3 From 9c955549338f49d8b5eb6ca003c2c65c725aa328 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Fri, 20 Jul 2018 19:58:03 -0400 Subject: fix detection on submit password --- app/scripts/controllers/detect-tokens.js | 13 ++++++++----- app/scripts/metamask-controller.js | 5 ++--- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index f1810cfa1..4fe4b4c61 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -64,10 +64,9 @@ class DetectTokensController { * */ restartTokenDetection () { - if (this.isActive && this.selectedAddress) { - this.detectNewTokens() - this.interval = DEFAULT_INTERVAL - } + if (!(this.isActive && this.selectedAddress)) { return } + this.detectNewTokens() + this.interval = DEFAULT_INTERVAL } /** @@ -113,11 +112,15 @@ class DetectTokensController { this._keyringMemStore = keyringMemStore this._keyringMemStore.subscribe(({ isUnlocked }) => { if (this.isUnlocked !== isUnlocked) { - if (isUnlocked) { this.restartTokenDetection() } this.isUnlocked = isUnlocked + if (isUnlocked) { this.restartTokenDetection() } } }) } + + get isActive () { + return this.isOpen && this.isUnlocked + } } module.exports = DetectTokensController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 4e97ce583..d3650815e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1274,16 +1274,15 @@ module.exports = class MetamaskController extends EventEmitter { set isClientOpen (open) { this._isClientOpen = open this.isClientOpenAndUnlocked = this.getState().isUnlocked && open + this.detectTokensController.isOpen = open } /** - * A method for activating the retrieval of price data and auto detect tokens, - * which should only be fetched when the UI is visible. + * A method for activating the retrieval of price data, which should only be fetched when the UI is visible. * @private * @param {boolean} active - True if price data should be getting fetched. */ set isClientOpenAndUnlocked (active) { this.tokenRatesController.isActive = active - this.detectTokensController.isActive = active } } -- cgit v1.2.3 From 9be22775c31699b49873a43820dc315067a567a9 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Fri, 20 Jul 2018 20:15:33 -0400 Subject: fix merge --- app/scripts/metamask-controller.js | 1 - 1 file changed, 1 deletion(-) (limited to 'app') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 18448961d..3b303a95c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1443,6 +1443,5 @@ module.exports = class MetamaskController extends EventEmitter { */ set isClientOpenAndUnlocked (active) { this.tokenRatesController.isActive = active - this.detectTokensController.isActive = active } } -- cgit v1.2.3