2017-06-22 21:32:08 +02:00
|
|
|
const ObservableStore = require('obs-store')
|
|
|
|
const extend = require('xtend')
|
2017-07-26 21:10:42 +02:00
|
|
|
const recentBlacklist = require('etheraddresslookup/blacklists/domains.json')
|
2017-06-22 21:32:08 +02:00
|
|
|
|
|
|
|
// every ten minutes
|
|
|
|
const POLLING_INTERVAL = 300000
|
|
|
|
|
|
|
|
class InfuraController {
|
|
|
|
|
|
|
|
constructor (opts = {}) {
|
|
|
|
const initState = extend({
|
|
|
|
infuraNetworkStatus: {},
|
2017-07-26 21:10:42 +02:00
|
|
|
blacklist: recentBlacklist,
|
2017-06-22 21:32:08 +02:00
|
|
|
}, 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,
|
|
|
|
})
|
2017-07-14 19:34:03 +02:00
|
|
|
return parsedResponse
|
2017-06-22 21:32:08 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:10:42 +02:00
|
|
|
updateLocalBlacklist () {
|
|
|
|
return fetch('https://api.infura.io/v1/blacklist')
|
|
|
|
.then(response => response.json())
|
|
|
|
.then((parsedResponse) => {
|
|
|
|
this.store.updateState({
|
|
|
|
blacklist: parsedResponse,
|
|
|
|
})
|
|
|
|
return parsedResponse
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-22 21:32:34 +02:00
|
|
|
scheduleInfuraNetworkCheck () {
|
2017-06-22 21:32:08 +02:00
|
|
|
if (this.conversionInterval) {
|
|
|
|
clearInterval(this.conversionInterval)
|
|
|
|
}
|
|
|
|
this.conversionInterval = setInterval(() => {
|
|
|
|
this.checkInfuraNetworkStatus()
|
2017-07-26 21:10:42 +02:00
|
|
|
this.updateLocalBlacklist()
|
2017-06-22 21:32:08 +02:00
|
|
|
}, POLLING_INTERVAL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = InfuraController
|