mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #1663 from MetaMask/infura-status
Add Infura Status Information to UI State
This commit is contained in:
commit
8b5b2d8329
42
app/scripts/controllers/infura.js
Normal file
42
app/scripts/controllers/infura.js
Normal file
@ -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
|
@ -15,6 +15,7 @@ const CurrencyController = require('./controllers/currency')
|
|||||||
const NoticeController = require('./notice-controller')
|
const NoticeController = require('./notice-controller')
|
||||||
const ShapeShiftController = require('./controllers/shapeshift')
|
const ShapeShiftController = require('./controllers/shapeshift')
|
||||||
const AddressBookController = require('./controllers/address-book')
|
const AddressBookController = require('./controllers/address-book')
|
||||||
|
const InfuraController = require('./controllers/infura')
|
||||||
const MessageManager = require('./lib/message-manager')
|
const MessageManager = require('./lib/message-manager')
|
||||||
const PersonalMessageManager = require('./lib/personal-message-manager')
|
const PersonalMessageManager = require('./lib/personal-message-manager')
|
||||||
const TransactionController = require('./controllers/transactions')
|
const TransactionController = require('./controllers/transactions')
|
||||||
@ -44,8 +45,8 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.store = new ObservableStore(initState)
|
this.store = new ObservableStore(initState)
|
||||||
|
|
||||||
// network store
|
// network store
|
||||||
|
|
||||||
this.networkController = new NetworkController(initState.NetworkController)
|
this.networkController = new NetworkController(initState.NetworkController)
|
||||||
|
|
||||||
// config manager
|
// config manager
|
||||||
this.configManager = new ConfigManager({
|
this.configManager = new ConfigManager({
|
||||||
store: this.store,
|
store: this.store,
|
||||||
@ -63,6 +64,13 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.currencyController.updateConversionRate()
|
this.currencyController.updateConversionRate()
|
||||||
this.currencyController.scheduleConversionInterval()
|
this.currencyController.scheduleConversionInterval()
|
||||||
|
|
||||||
|
// infura controller
|
||||||
|
this.infuraController = new InfuraController({
|
||||||
|
initState: initState.InfuraController,
|
||||||
|
})
|
||||||
|
this.infuraController.scheduleInfuraNetworkCheck()
|
||||||
|
|
||||||
|
|
||||||
// rpc provider
|
// rpc provider
|
||||||
this.provider = this.initializeProvider()
|
this.provider = this.initializeProvider()
|
||||||
|
|
||||||
@ -147,6 +155,9 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.networkController.store.subscribe((state) => {
|
this.networkController.store.subscribe((state) => {
|
||||||
this.store.updateState({ NetworkController: state })
|
this.store.updateState({ NetworkController: state })
|
||||||
})
|
})
|
||||||
|
this.infuraController.store.subscribe((state) => {
|
||||||
|
this.store.updateState({ InfuraController: state })
|
||||||
|
})
|
||||||
|
|
||||||
// manual mem state subscriptions
|
// manual mem state subscriptions
|
||||||
this.networkController.store.subscribe(this.sendUpdate.bind(this))
|
this.networkController.store.subscribe(this.sendUpdate.bind(this))
|
||||||
@ -160,6 +171,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.currencyController.store.subscribe(this.sendUpdate.bind(this))
|
this.currencyController.store.subscribe(this.sendUpdate.bind(this))
|
||||||
this.noticeController.memStore.subscribe(this.sendUpdate.bind(this))
|
this.noticeController.memStore.subscribe(this.sendUpdate.bind(this))
|
||||||
this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this))
|
this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this))
|
||||||
|
this.infuraController.store.subscribe(this.sendUpdate.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -237,6 +249,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.addressBookController.store.getState(),
|
this.addressBookController.store.getState(),
|
||||||
this.currencyController.store.getState(),
|
this.currencyController.store.getState(),
|
||||||
this.noticeController.memStore.getState(),
|
this.noticeController.memStore.getState(),
|
||||||
|
this.infuraController.store.getState(),
|
||||||
// config manager
|
// config manager
|
||||||
this.configManager.getConfig(),
|
this.configManager.getConfig(),
|
||||||
this.shapeshiftController.store.getState(),
|
this.shapeshiftController.store.getState(),
|
||||||
|
34
test/unit/infura-controller-test.js
Normal file
34
test/unit/infura-controller-test.js
Normal file
@ -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
Block a user