aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/detect-tokens.js
blob: 4d364bd378db1996b04a955d1187354b1cee2362 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 = 15 * 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