diff options
-rw-r--r-- | app/scripts/controllers/detect-tokens.js | 93 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 8 | ||||
-rw-r--r-- | test/unit/app/controllers/detect-tokens-test.js | 101 |
3 files changed, 202 insertions, 0 deletions
diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js new file mode 100644 index 000000000..1ea855356 --- /dev/null +++ b/app/scripts/controllers/detect-tokens.js @@ -0,0 +1,93 @@ +const contracts = require('eth-contract-metadata') +const { + MAINNET, + } = 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 + this.contracts = contracts + } + + /** + * For each token in eth-contract-metada, find check selectedAddress balance. + * + */ + 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']) + } + } + // 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 + this.tokens = preferences.store.getState().tokens + + } + + /** + * @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 b4d39031a..9a93cf584 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.store, + }) + this.recentBlocksController = new RecentBlocksController({ blockTracker: this.blockTracker, provider: this.provider, @@ -1275,5 +1282,6 @@ module.exports = class MetamaskController extends EventEmitter { */ 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 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"}]) + }) +}) |