Merge pull request #1663 from MetaMask/infura-status
Add Infura Status Information to UI Statefeature/default_network_editable
commit
8b5b2d8329
@ -0,0 +1,42 @@ |
||||
const ObservableStore = require('obs-store') |
||||
const extend = require('xtend') |
||||
|
||||
// every ten minutes
|
||||
const POLLING_INTERVAL = 300000 |
||||
|
||||
class InfuraController { |
||||
|
||||
constructor (opts = {}) { |
||||
const initState = extend({ |
||||
infuraNetworkStatus: {}, |
||||
}, 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, |
||||
}) |
||||
}) |
||||
} |
||||
|
||||
scheduleInfuraNetworkCheck () { |
||||
if (this.conversionInterval) { |
||||
clearInterval(this.conversionInterval) |
||||
} |
||||
this.conversionInterval = setInterval(() => { |
||||
this.checkInfuraNetworkStatus() |
||||
}, POLLING_INTERVAL) |
||||
} |
||||
} |
||||
|
||||
module.exports = InfuraController |
@ -0,0 +1,34 @@ |
||||
// polyfill fetch
|
||||
global.fetch = function () {return Promise.resolve({ |
||||
json: () => { return Promise.resolve({"mainnet": "ok", "ropsten": "degraded", "kovan": "down", "rinkeby": "ok"}) }, |
||||
}) |
||||
} |
||||
const assert = require('assert') |
||||
const InfuraController = require('../../app/scripts/controllers/infura') |
||||
|
||||
describe('infura-controller', function () { |
||||
var infuraController |
||||
|
||||
beforeEach(function () { |
||||
infuraController = new InfuraController() |
||||
}) |
||||
|
||||
describe('network status queries', function () { |
||||
describe('#checkInfuraNetworkStatus', function () { |
||||
it('should return an object reflecting the network statuses', function (done) { |
||||
this.timeout(15000) |
||||
infuraController.checkInfuraNetworkStatus() |
||||
.then(() => { |
||||
const networkStatus = infuraController.store.getState().infuraNetworkStatus |
||||
assert.equal(Object.keys(networkStatus).length, 4) |
||||
assert.equal(networkStatus.mainnet, 'ok') |
||||
assert.equal(networkStatus.ropsten, 'degraded') |
||||
assert.equal(networkStatus.kovan, 'down') |
||||
}) |
||||
.then(() => done()) |
||||
.catch(done) |
||||
|
||||
}) |
||||
}) |
||||
}) |
||||
}) |
Loading…
Reference in new issue