aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/infura.js
blob: 97b2ab7e3dc0932b4159f01d27f5a7889f277a9e (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
const ObservableStore = require('obs-store')
const extend = require('xtend')
const recentBlacklist = require('etheraddresslookup/blacklists/domains.json')

// every ten minutes
const POLLING_INTERVAL = 300000

class InfuraController {

  constructor (opts = {}) {
    const initState = extend({
      infuraNetworkStatus: {},
      blacklist: recentBlacklist,
    }, opts.initState)
    this.store = new ObservableStore(initState)
  }

  //
  // PUBLIC METHODS
  //

  // Responsible for retrieving the status of Infura's nodes. Can return either
  // ok, degraded, or down.
  checkInfuraNetworkStatus () {
    return fetch('https://api.infura.io/v1/status/metamask')
      .then(response => response.json())
      .then((parsedResponse) => {
        this.store.updateState({
          infuraNetworkStatus: parsedResponse,
        })
        return parsedResponse
      })
  }

  updateLocalBlacklist () {
    return fetch('https://api.infura.io/v1/blacklist')
      .then(response => response.json())
      .then((parsedResponse) => {
        this.store.updateState({
          blacklist: parsedResponse,
        })
        return parsedResponse
      })
  }

  scheduleInfuraNetworkCheck () {
    if (this.conversionInterval) {
      clearInterval(this.conversionInterval)
    }
    this.conversionInterval = setInterval(() => {
      this.checkInfuraNetworkStatus()
      this.updateLocalBlacklist()
    }, POLLING_INTERVAL)
  }
}

module.exports = InfuraController