2017-05-18 23:54:02 +02:00
|
|
|
const EventEmitter = require('events')
|
|
|
|
const MetaMaskProvider = require('web3-provider-engine/zero.js')
|
|
|
|
const ObservableStore = require('obs-store')
|
2017-05-23 08:12:28 +02:00
|
|
|
const ComposedStore = require('obs-store/lib/composed')
|
2017-05-18 23:54:02 +02:00
|
|
|
const extend = require('xtend')
|
|
|
|
const EthQuery = require('eth-query')
|
2017-05-23 07:56:10 +02:00
|
|
|
const RPC_ADDRESS_LIST = require('../config.js').network
|
2017-05-23 08:12:28 +02:00
|
|
|
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
|
2017-05-18 23:54:02 +02:00
|
|
|
|
|
|
|
module.exports = class NetworkController extends EventEmitter {
|
2017-05-23 08:12:28 +02:00
|
|
|
constructor (config) {
|
2017-05-18 23:54:02 +02:00
|
|
|
super()
|
2017-05-23 08:12:28 +02:00
|
|
|
this.networkStore = new ObservableStore('loading')
|
|
|
|
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
|
|
|
|
this.providerStore = new ObservableStore(config.provider)
|
|
|
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
|
|
|
|
this._providerListeners = {}
|
2017-05-23 07:56:10 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
this.on('networkDidChange', this.lookupNetwork)
|
|
|
|
this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget}))
|
2017-05-23 07:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get provider () {
|
|
|
|
return this._proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
set provider (provider) {
|
|
|
|
this._provider = provider
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 23:36:19 +02:00
|
|
|
initializeProvider (opts, providerContructor = MetaMaskProvider) {
|
2017-05-23 08:12:28 +02:00
|
|
|
this.providerInit = opts
|
2017-07-25 23:36:19 +02:00
|
|
|
this._provider = providerContructor(opts)
|
2017-05-23 07:56:10 +02:00
|
|
|
this._proxy = new Proxy(this._provider, {
|
|
|
|
get: (obj, name) => {
|
|
|
|
if (name === 'on') return this._on.bind(this)
|
|
|
|
return this._provider[name]
|
|
|
|
},
|
|
|
|
set: (obj, name, value) => {
|
|
|
|
this._provider[name] = value
|
2017-07-25 23:36:19 +02:00
|
|
|
return value
|
2017-05-23 07:56:10 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
this.provider.on('block', this._logBlock.bind(this))
|
|
|
|
this.provider.on('error', this.verifyNetwork.bind(this))
|
2017-05-18 23:54:02 +02:00
|
|
|
this.ethQuery = new EthQuery(this.provider)
|
|
|
|
this.lookupNetwork()
|
2017-05-23 07:56:10 +02:00
|
|
|
return this.provider
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
2017-05-23 07:56:10 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
switchNetwork (providerInit) {
|
|
|
|
this.setNetworkState('loading')
|
|
|
|
const newInit = extend(this.providerInit, providerInit)
|
|
|
|
this.providerInit = newInit
|
2017-05-23 07:56:10 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
this._provider.removeAllListeners()
|
2017-05-24 17:52:18 +02:00
|
|
|
this._provider.stop()
|
2017-05-23 08:12:28 +02:00
|
|
|
this.provider = MetaMaskProvider(newInit)
|
2017-05-23 07:56:10 +02:00
|
|
|
// apply the listners created by other controllers
|
2017-05-23 08:12:28 +02:00
|
|
|
Object.keys(this._providerListeners).forEach((key) => {
|
|
|
|
this._providerListeners[key].forEach((handler) => this._provider.addListener(key, handler))
|
2017-05-23 07:56:10 +02:00
|
|
|
})
|
2017-05-23 08:12:28 +02:00
|
|
|
this.emit('networkDidChange')
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
verifyNetwork () {
|
|
|
|
// Check network when restoring connectivity:
|
|
|
|
if (this.isNetworkLoading()) this.lookupNetwork()
|
|
|
|
}
|
|
|
|
|
|
|
|
getNetworkState () {
|
2017-05-23 08:12:28 +02:00
|
|
|
return this.networkStore.getState()
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setNetworkState (network) {
|
2017-05-23 08:12:28 +02:00
|
|
|
return this.networkStore.putState(network)
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isNetworkLoading () {
|
|
|
|
return this.getNetworkState() === 'loading'
|
|
|
|
}
|
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
lookupNetwork () {
|
2017-05-18 23:54:02 +02:00
|
|
|
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
|
|
|
|
if (err) return this.setNetworkState('loading')
|
|
|
|
log.info('web3.getNetwork returned ' + network)
|
|
|
|
this.setNetworkState(network)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setRpcTarget (rpcUrl) {
|
|
|
|
this.providerStore.updateState({
|
2017-05-23 08:12:28 +02:00
|
|
|
type: 'rpc',
|
|
|
|
rpcTarget: rpcUrl,
|
2017-05-18 23:54:02 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentRpcAddress () {
|
2017-05-23 08:12:28 +02:00
|
|
|
const provider = this.getProviderConfig()
|
2017-05-18 23:54:02 +02:00
|
|
|
if (!provider) return null
|
|
|
|
return this.getRpcAddressForType(provider.type)
|
|
|
|
}
|
|
|
|
|
|
|
|
setProviderType (type) {
|
2017-05-23 08:12:28 +02:00
|
|
|
if (type === this.getProviderConfig().type) return
|
2017-05-18 23:54:02 +02:00
|
|
|
const rpcTarget = this.getRpcAddressForType(type)
|
2017-05-23 08:12:28 +02:00
|
|
|
this.providerStore.updateState({type, rpcTarget})
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
getProviderConfig () {
|
|
|
|
return this.providerStore.getState()
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
getRpcAddressForType (type, provider = this.getProviderConfig()) {
|
|
|
|
if (RPC_ADDRESS_LIST[type]) return RPC_ADDRESS_LIST[type]
|
|
|
|
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
|
2017-05-23 07:56:10 +02:00
|
|
|
}
|
2017-05-18 23:54:02 +02:00
|
|
|
|
2017-05-23 07:56:10 +02:00
|
|
|
_logBlock (block) {
|
|
|
|
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`)
|
|
|
|
this.verifyNetwork()
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 07:56:10 +02:00
|
|
|
_on (event, handler) {
|
2017-05-23 08:12:28 +02:00
|
|
|
if (!this._providerListeners[event]) this._providerListeners[event] = []
|
|
|
|
this._providerListeners[event].push(handler)
|
2017-05-23 07:56:10 +02:00
|
|
|
this._provider.on(event, handler)
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
}
|