From bfac9c2c2dfd0c5f55cb794214791cf78cca75c0 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 27 Jun 2018 16:29:24 -0400 Subject: [PATCH 01/12] 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 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 } } From 0e863d5fab00eb83c908fb49f2939534bdfe1162 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 27 Jun 2018 19:54:43 -0400 Subject: [PATCH 02/12] 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(-) 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({ From 6284e664810fe0dcdcb35e55cc57bc11b9298dbb Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 27 Jun 2018 22:18:06 -0400 Subject: [PATCH 03/12] tests for spec --- app/scripts/controllers/detect-tokens.js | 2 +- .../app/controllers/detect-tokens-test.js | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 test/unit/app/controllers/detect-tokens-test.js 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 diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js new file mode 100644 index 000000000..f9f5c902d --- /dev/null +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -0,0 +1,101 @@ +const assert = require('assert') +const sinon = require('sinon') +const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens') +const PreferencesController = require('../../../../app/scripts/controllers/preferences') +const ObservableStore = require('obs-store') + +describe('DetectTokensController', () => { + it('should poll on correct interval', async () => { + const stub = sinon.stub(global, 'setInterval') + new DetectTokensController({ interval: 1337 }) // eslint-disable-line no-new + assert.strictEqual(stub.getCall(0).args[1], 1337) + stub.restore() + }) + + it('should not check tokens while in test network', async () => { + var network = new ObservableStore({provider: {type:'rinkeby'}}) + const preferences = new PreferencesController() + const controller = new DetectTokensController({preferences: preferences, network: network}) + controller.isActive = true + controller.contracts = { + "0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4": { + "name": "JET8 Token", + "logo": "J8T.svg", + "erc20": true, + "symbol": "J8T", + "decimals": 8 + }, + "0xBC86727E770de68B1060C91f6BB6945c73e10388": { + "name": "Ink Protocol", + "logo": "ink_protocol.svg", + "erc20": true, + "symbol": "XNK", + "decimals": 18 + } + } + controller.fetchContractAccountBalance = address => address + + await controller.exploreNewTokens() + assert.deepEqual(preferences.store.getState().tokens, []) + + }) + + it('should only check and add tokens while in main network', async () => { + const network = new ObservableStore({provider: {type:'mainnet'}}) + const preferences = new PreferencesController() + const controller = new DetectTokensController({preferences: preferences, network: network}) + controller.isActive = true + controller.contracts = { + "0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4": { + "name": "JET8 Token", + "logo": "J8T.svg", + "erc20": true, + "symbol": "J8T", + "decimals": 8 + }, + "0xBC86727E770de68B1060C91f6BB6945c73e10388": { + "name": "Ink Protocol", + "logo": "ink_protocol.svg", + "erc20": true, + "symbol": "XNK", + "decimals": 18 + } + } + controller.fetchContractAccountBalance = address => address + + await controller.exploreNewTokens() + assert.deepEqual(preferences.store.getState().tokens, + [{address: "0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4", decimals: 8, symbol: "J8T"}, + {address: "0xbc86727e770de68b1060c91f6bb6945c73e10388", decimals: 18, symbol: "XNK"}]) + }) + + it('should not detect same token while in main network', async () => { + const network = new ObservableStore({provider: {type:'mainnet'}}) + const preferences = new PreferencesController() + preferences.addToken("0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4", 8, "J8T") + const controller = new DetectTokensController({preferences: preferences, network: network}) + controller.isActive = true + controller.contracts = { + "0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4": { + "name": "JET8 Token", + "logo": "J8T.svg", + "erc20": true, + "symbol": "J8T", + "decimals": 8 + }, + "0xBC86727E770de68B1060C91f6BB6945c73e10388": { + "name": "Ink Protocol", + "logo": "ink_protocol.svg", + "erc20": true, + "symbol": "XNK", + "decimals": 18 + } + } + controller.fetchContractAccountBalance = address => address + + await controller.exploreNewTokens() + assert.deepEqual(preferences.store.getState().tokens, + [{address: "0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4", decimals: 8, symbol: "J8T"}, + {address: "0xbc86727e770de68b1060c91f6bb6945c73e10388", decimals: 18, symbol: "XNK"}]) + }) +}) From 03fd4355af8ceae9a1d2cad04e12a4f86a9f36b7 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Tue, 3 Jul 2018 12:53:06 -0400 Subject: [PATCH 04/12] passtest-lint --- app/scripts/controllers/detect-tokens.js | 6 ------ 1 file changed, 6 deletions(-) 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 From 63b9c8796bf744f05a3b7d88319460eed772efa9 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Tue, 3 Jul 2018 15:24:23 -0400 Subject: [PATCH 05/12] fix lint --- .../app/controllers/detect-tokens-test.js | 100 ++++++++---------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index f9f5c902d..82e86bfcc 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -13,89 +13,81 @@ describe('DetectTokensController', () => { }) it('should not check tokens while in test network', async () => { - var network = new ObservableStore({provider: {type:'rinkeby'}}) + var network = new ObservableStore({provider: {type: 'rinkeby'}}) const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true controller.contracts = { - "0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4": { - "name": "JET8 Token", - "logo": "J8T.svg", - "erc20": true, - "symbol": "J8T", - "decimals": 8 + '0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { + 'name': 'JET8 Token', + 'logo': 'J8T.svg', + 'erc20': true, + 'symbol': 'J8T', + 'decimals': 8, + }, + '0xBC86727E770de68B1060C91f6BB6945c73e10388': { + 'name': 'Ink Protocol', + 'logo': 'ink_protocol.svg', + 'erc20': true, + 'symbol': 'XNK', + 'decimals': 18, }, - "0xBC86727E770de68B1060C91f6BB6945c73e10388": { - "name": "Ink Protocol", - "logo": "ink_protocol.svg", - "erc20": true, - "symbol": "XNK", - "decimals": 18 - } } controller.fetchContractAccountBalance = address => address - await controller.exploreNewTokens() assert.deepEqual(preferences.store.getState().tokens, []) - - }) +}) it('should only check and add tokens while in main network', async () => { - const network = new ObservableStore({provider: {type:'mainnet'}}) + const network = new ObservableStore({provider: {type: 'mainnet'}}) const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true controller.contracts = { - "0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4": { - "name": "JET8 Token", - "logo": "J8T.svg", - "erc20": true, - "symbol": "J8T", - "decimals": 8 + '0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { + 'name': 'JET8 Token', + 'logo': 'J8T.svg', + 'erc20': true, + 'symbol': 'J8T', + 'decimals': 8, + }, + '0xBC86727E770de68B1060C91f6BB6945c73e10388': { + 'name': 'Ink Protocol', + 'logo': 'ink_protocol.svg', + 'erc20': true, + 'symbol': 'XNK', + 'decimals': 18, }, - "0xBC86727E770de68B1060C91f6BB6945c73e10388": { - "name": "Ink Protocol", - "logo": "ink_protocol.svg", - "erc20": true, - "symbol": "XNK", - "decimals": 18 - } } controller.fetchContractAccountBalance = address => address - await controller.exploreNewTokens() - assert.deepEqual(preferences.store.getState().tokens, - [{address: "0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4", decimals: 8, symbol: "J8T"}, - {address: "0xbc86727e770de68b1060c91f6bb6945c73e10388", decimals: 18, symbol: "XNK"}]) + assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) it('should not detect same token while in main network', async () => { - const network = new ObservableStore({provider: {type:'mainnet'}}) + const network = new ObservableStore({provider: {type: 'mainnet'}}) const preferences = new PreferencesController() - preferences.addToken("0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4", 8, "J8T") + preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 8, 'J8T') const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true controller.contracts = { - "0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4": { - "name": "JET8 Token", - "logo": "J8T.svg", - "erc20": true, - "symbol": "J8T", - "decimals": 8 + '0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { + 'name': 'JET8 Token', + 'logo': 'J8T.svg', + 'erc20': true, + 'symbol': 'J8T', + 'decimals': 8, + }, + '0xBC86727E770de68B1060C91f6BB6945c73e10388': { + 'name': 'Ink Protocol', + 'logo': 'ink_protocol.svg', + 'erc20': true, + 'symbol': 'XNK', + 'decimals': 18, }, - "0xBC86727E770de68B1060C91f6BB6945c73e10388": { - "name": "Ink Protocol", - "logo": "ink_protocol.svg", - "erc20": true, - "symbol": "XNK", - "decimals": 18 - } } controller.fetchContractAccountBalance = address => address - await controller.exploreNewTokens() - assert.deepEqual(preferences.store.getState().tokens, - [{address: "0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4", decimals: 8, symbol: "J8T"}, - {address: "0xbc86727e770de68b1060c91f6bb6945c73e10388", decimals: 18, symbol: "XNK"}]) + assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) }) From 910713c6b3c5b8f865fdcb989bfe3ee0b14eb364 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Wed, 11 Jul 2018 15:59:05 -0400 Subject: [PATCH 06/12] improve tests --- app/scripts/controllers/detect-tokens.js | 29 +++-- app/scripts/metamask-controller.js | 2 +- .../app/controllers/detect-tokens-test.js | 105 ++++++++---------- 3 files changed, 63 insertions(+), 73 deletions(-) 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, diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index 82e86bfcc..dca48c6bb 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -5,6 +5,14 @@ const PreferencesController = require('../../../../app/scripts/controllers/prefe const ObservableStore = require('obs-store') describe('DetectTokensController', () => { + const sandbox = sinon.createSandbox() + let clock + before(async () => { + }) + after(() => { + sandbox.restore() + }) + it('should poll on correct interval', async () => { const stub = sinon.stub(global, 'setInterval') new DetectTokensController({ interval: 1337 }) // eslint-disable-line no-new @@ -12,82 +20,67 @@ describe('DetectTokensController', () => { stub.restore() }) + it('should be called on every polling period', async () => { + clock = sandbox.useFakeTimers() + const network = new ObservableStore({provider: {type: 'mainnet'}}) + const preferences = new PreferencesController() + const controller = new DetectTokensController({preferences: preferences, network: network}) + controller.isActive = true + + var stub = sandbox.stub(controller, 'exploreNewTokens') + + clock.tick(1) + sandbox.assert.notCalled(stub) + clock.tick(180000) + sandbox.assert.called(stub) + clock.tick(180000) + sandbox.assert.calledTwice(stub) + clock.tick(180000) + sandbox.assert.calledThrice(stub) + }) + it('should not check tokens while in test network', async () => { var network = new ObservableStore({provider: {type: 'rinkeby'}}) const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true - controller.contracts = { - '0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { - 'name': 'JET8 Token', - 'logo': 'J8T.svg', - 'erc20': true, - 'symbol': 'J8T', - 'decimals': 8, - }, - '0xBC86727E770de68B1060C91f6BB6945c73e10388': { - 'name': 'Ink Protocol', - 'logo': 'ink_protocol.svg', - 'erc20': true, - 'symbol': 'XNK', - 'decimals': 18, - }, - } - controller.fetchContractAccountBalance = address => address + + var stub = sandbox.stub(controller, 'detectTokenBalance') + .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) + .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true) + await controller.exploreNewTokens() - assert.deepEqual(preferences.store.getState().tokens, []) -}) + sandbox.assert.notCalled(stub) + }) it('should only check and add tokens while in main network', async () => { const network = new ObservableStore({provider: {type: 'mainnet'}}) const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true - controller.contracts = { - '0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { - 'name': 'JET8 Token', - 'logo': 'J8T.svg', - 'erc20': true, - 'symbol': 'J8T', - 'decimals': 8, - }, - '0xBC86727E770de68B1060C91f6BB6945c73e10388': { - 'name': 'Ink Protocol', - 'logo': 'ink_protocol.svg', - 'erc20': true, - 'symbol': 'XNK', - 'decimals': 18, - }, - } - controller.fetchContractAccountBalance = address => address + + sandbox.stub(controller, 'detectTokenBalance') + .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) + .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true) + await controller.exploreNewTokens() - assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) + assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, + {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) it('should not detect same token while in main network', async () => { const network = new ObservableStore({provider: {type: 'mainnet'}}) const preferences = new PreferencesController() - preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 8, 'J8T') + preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8) const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true - controller.contracts = { - '0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4': { - 'name': 'JET8 Token', - 'logo': 'J8T.svg', - 'erc20': true, - 'symbol': 'J8T', - 'decimals': 8, - }, - '0xBC86727E770de68B1060C91f6BB6945c73e10388': { - 'name': 'Ink Protocol', - 'logo': 'ink_protocol.svg', - 'erc20': true, - 'symbol': 'XNK', - 'decimals': 18, - }, - } - controller.fetchContractAccountBalance = address => address + + sandbox.stub(controller, 'detectTokenBalance') + .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) + .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true) + await controller.exploreNewTokens() - assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) + assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, + {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) }) From 2fffe098736e2461b9238c7dcd91f9ef3d61dcc1 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 12 Jul 2018 20:43:43 -0400 Subject: [PATCH 07/12] detect tokens through infura --- app/scripts/controllers/detect-tokens.js | 45 ++++++++++--------- app/scripts/metamask-controller.js | 2 +- .../app/controllers/detect-tokens-test.js | 26 +++++++---- 3 files changed, 42 insertions(+), 31 deletions(-) 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({ diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index dca48c6bb..860ed7050 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -1,8 +1,8 @@ const assert = require('assert') const sinon = require('sinon') const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens') +const NetworkController = require('../../../../app/scripts/controllers/network/network') const PreferencesController = require('../../../../app/scripts/controllers/preferences') -const ObservableStore = require('obs-store') describe('DetectTokensController', () => { const sandbox = sinon.createSandbox() @@ -22,7 +22,8 @@ describe('DetectTokensController', () => { it('should be called on every polling period', async () => { clock = sandbox.useFakeTimers() - const network = new ObservableStore({provider: {type: 'mainnet'}}) + const network = new NetworkController() + network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true @@ -40,7 +41,8 @@ describe('DetectTokensController', () => { }) it('should not check tokens while in test network', async () => { - var network = new ObservableStore({provider: {type: 'rinkeby'}}) + const network = new NetworkController() + network.setProviderType('rinkeby') const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true @@ -54,14 +56,17 @@ describe('DetectTokensController', () => { }) it('should only check and add tokens while in main network', async () => { - const network = new ObservableStore({provider: {type: 'mainnet'}}) + const network = new NetworkController() + network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true sandbox.stub(controller, 'detectTokenBalance') - .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) - .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true) + .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4') + .returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)) + .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388') + .returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18)) await controller.exploreNewTokens() assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, @@ -69,15 +74,18 @@ describe('DetectTokensController', () => { }) it('should not detect same token while in main network', async () => { - const network = new ObservableStore({provider: {type: 'mainnet'}}) + const network = new NetworkController() + network.setProviderType('mainnet') const preferences = new PreferencesController() preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8) const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true sandbox.stub(controller, 'detectTokenBalance') - .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) - .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true) + .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4') + .returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)) + .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388') + .returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18)) await controller.exploreNewTokens() assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, From 3b97d816ffcaebc7606d4564ea95918f647ba413 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 19 Jul 2018 15:56:38 -0400 Subject: [PATCH 08/12] detect tokens when submit password and new account selected --- app/scripts/controllers/detect-tokens.js | 52 ++++++++++++++----- .../app/controllers/detect-tokens-test.js | 8 +-- 2 files changed, 43 insertions(+), 17 deletions(-) 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 diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index 860ed7050..49492c543 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -28,7 +28,7 @@ describe('DetectTokensController', () => { const controller = new DetectTokensController({preferences: preferences, network: network}) controller.isActive = true - var stub = sandbox.stub(controller, 'exploreNewTokens') + var stub = sandbox.stub(controller, 'detectNewTokens') clock.tick(1) sandbox.assert.notCalled(stub) @@ -51,7 +51,7 @@ describe('DetectTokensController', () => { .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true) - await controller.exploreNewTokens() + await controller.detectNewTokens() sandbox.assert.notCalled(stub) }) @@ -68,7 +68,7 @@ describe('DetectTokensController', () => { .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388') .returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18)) - await controller.exploreNewTokens() + await controller.detectNewTokens() assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) @@ -87,7 +87,7 @@ describe('DetectTokensController', () => { .withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388') .returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18)) - await controller.exploreNewTokens() + await controller.detectNewTokens() assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) From 009b1cefbe3d19dcad01078927b1a55c3439b22f Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 19 Jul 2018 19:46:46 -0400 Subject: [PATCH 09/12] keyring unlocked detect and unit tests --- app/scripts/controllers/detect-tokens.js | 28 ++++++++------ app/scripts/metamask-controller.js | 16 ++++---- .../app/controllers/detect-tokens-test.js | 38 ++++++++++++++++--- 3 files changed, 57 insertions(+), 25 deletions(-) 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. */ diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index 49492c543..dcb3c431f 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -1,14 +1,17 @@ const assert = require('assert') const sinon = require('sinon') +const ObservableStore = require('obs-store') const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens') const NetworkController = require('../../../../app/scripts/controllers/network/network') const PreferencesController = require('../../../../app/scripts/controllers/preferences') describe('DetectTokensController', () => { - const sandbox = sinon.createSandbox() + const sandbox = sinon.createSandbox() let clock + let keyringMemStore before(async () => { - }) + keyringMemStore = new ObservableStore({ isUnlocked: false}) + }) after(() => { sandbox.restore() }) @@ -25,7 +28,7 @@ describe('DetectTokensController', () => { const network = new NetworkController() network.setProviderType('mainnet') const preferences = new PreferencesController() - const controller = new DetectTokensController({preferences: preferences, network: network}) + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isActive = true var stub = sandbox.stub(controller, 'detectNewTokens') @@ -44,7 +47,7 @@ describe('DetectTokensController', () => { const network = new NetworkController() network.setProviderType('rinkeby') const preferences = new PreferencesController() - const controller = new DetectTokensController({preferences: preferences, network: network}) + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isActive = true var stub = sandbox.stub(controller, 'detectTokenBalance') @@ -59,7 +62,7 @@ describe('DetectTokensController', () => { const network = new NetworkController() network.setProviderType('mainnet') const preferences = new PreferencesController() - const controller = new DetectTokensController({preferences: preferences, network: network}) + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isActive = true sandbox.stub(controller, 'detectTokenBalance') @@ -78,7 +81,7 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8) - const controller = new DetectTokensController({preferences: preferences, network: network}) + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isActive = true sandbox.stub(controller, 'detectTokenBalance') @@ -91,4 +94,27 @@ describe('DetectTokensController', () => { assert.deepEqual(preferences.store.getState().tokens, [{address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T'}, {address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK'}]) }) + + it('should trigger detect new tokens when change address', async () => { + const network = new NetworkController() + network.setProviderType('mainnet') + const preferences = new PreferencesController() + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) + controller.isActive = true + var stub = sandbox.stub(controller, 'detectNewTokens') + await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388') + sandbox.assert.called(stub) + }) + + it('should trigger detect new tokens when submit password', async () => { + const network = new NetworkController() + network.setProviderType('mainnet') + const preferences = new PreferencesController() + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) + controller.isActive = true + controller.selectedAddress = '0x0' + var stub = sandbox.stub(controller, 'detectNewTokens') + await controller._keyringMemStore.updateState({ isUnlocked: true }) + sandbox.assert.called(stub) + }) }) From e6ca7948e75af8e9a8b8ceb62de9ebb573fe33b3 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Fri, 20 Jul 2018 12:14:03 -0400 Subject: [PATCH 10/12] add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d86fcd713..c156bfe0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Current Master +- Add new tokens auto detection + ## 4.8.0 Thur Jun 14 2018 - [#4513](https://github.com/MetaMask/metamask-extension/pull/4513): Attempting to import an empty private key will now show a clear error. From 9c955549338f49d8b5eb6ca003c2c65c725aa328 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Fri, 20 Jul 2018 19:58:03 -0400 Subject: [PATCH 11/12] fix detection on submit password --- app/scripts/controllers/detect-tokens.js | 13 +++++--- app/scripts/metamask-controller.js | 5 ++- .../app/controllers/detect-tokens-test.js | 33 +++++++++++++++---- 3 files changed, 37 insertions(+), 14 deletions(-) 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 } } diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index dcb3c431f..426ffe23a 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -29,7 +29,8 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectNewTokens') @@ -48,7 +49,8 @@ describe('DetectTokensController', () => { network.setProviderType('rinkeby') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectTokenBalance') .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true) @@ -63,7 +65,8 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true sandbox.stub(controller, 'detectTokenBalance') .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4') @@ -82,7 +85,8 @@ describe('DetectTokensController', () => { const preferences = new PreferencesController() preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8) const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true sandbox.stub(controller, 'detectTokenBalance') .withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4') @@ -100,7 +104,8 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true + controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectNewTokens') await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388') sandbox.assert.called(stub) @@ -111,10 +116,26 @@ describe('DetectTokensController', () => { network.setProviderType('mainnet') const preferences = new PreferencesController() const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) - controller.isActive = true + controller.isOpen = true controller.selectedAddress = '0x0' var stub = sandbox.stub(controller, 'detectNewTokens') await controller._keyringMemStore.updateState({ isUnlocked: true }) sandbox.assert.called(stub) }) + + it('should not trigger detect new tokens when not open or not unlocked', async () => { + const network = new NetworkController() + network.setProviderType('mainnet') + const preferences = new PreferencesController() + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) + controller.isOpen = true + controller.isUnlocked = false + var stub = sandbox.stub(controller, 'detectTokenBalance') + clock.tick(180000) + sandbox.assert.notCalled(stub) + controller.isOpen = false + controller.isUnlocked = true + clock.tick(180000) + sandbox.assert.notCalled(stub) + }) }) From 9be22775c31699b49873a43820dc315067a567a9 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Fri, 20 Jul 2018 20:15:33 -0400 Subject: [PATCH 12/12] fix merge --- app/scripts/metamask-controller.js | 1 - 1 file changed, 1 deletion(-) 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 } }