From 2fffe098736e2461b9238c7dcd91f9ef3d61dcc1 Mon Sep 17 00:00:00 2001
From: Esteban MIno <efmino@uc.cl>
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 +-
 test/unit/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'},
-- 
cgit v1.2.3