|
|
@ -1,12 +1,11 @@ |
|
|
|
const assert = require('assert') |
|
|
|
const assert = require('assert') |
|
|
|
const EventEmitter = require('events') |
|
|
|
const EventEmitter = require('events') |
|
|
|
|
|
|
|
const createMetamaskProvider = require('web3-provider-engine/zero.js') |
|
|
|
const ObservableStore = require('obs-store') |
|
|
|
const ObservableStore = require('obs-store') |
|
|
|
const ComposedStore = require('obs-store/lib/composed') |
|
|
|
const ComposedStore = require('obs-store/lib/composed') |
|
|
|
const extend = require('xtend') |
|
|
|
const extend = require('xtend') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const createEthRpcClient = require('eth-rpc-client') |
|
|
|
|
|
|
|
const createEventEmitterProxy = require('../lib/events-proxy.js') |
|
|
|
const createEventEmitterProxy = require('../lib/events-proxy.js') |
|
|
|
const createObjectProxy = require('../lib/obj-proxy.js') |
|
|
|
|
|
|
|
const RPC_ADDRESS_LIST = require('../config.js').network |
|
|
|
const RPC_ADDRESS_LIST = require('../config.js').network |
|
|
|
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] |
|
|
|
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] |
|
|
|
|
|
|
|
|
|
|
@ -18,8 +17,7 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
this.networkStore = new ObservableStore('loading') |
|
|
|
this.networkStore = new ObservableStore('loading') |
|
|
|
this.providerStore = new ObservableStore(config.provider) |
|
|
|
this.providerStore = new ObservableStore(config.provider) |
|
|
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) |
|
|
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) |
|
|
|
this.providerProxy = createObjectProxy() |
|
|
|
this._proxy = createEventEmitterProxy() |
|
|
|
this.blockTrackerProxy = createEventEmitterProxy() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.on('networkDidChange', this.lookupNetwork) |
|
|
|
this.on('networkDidChange', this.lookupNetwork) |
|
|
|
} |
|
|
|
} |
|
|
@ -27,11 +25,12 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
initializeProvider (_providerParams) { |
|
|
|
initializeProvider (_providerParams) { |
|
|
|
this._baseProviderParams = _providerParams |
|
|
|
this._baseProviderParams = _providerParams |
|
|
|
const rpcUrl = this.getCurrentRpcAddress() |
|
|
|
const rpcUrl = this.getCurrentRpcAddress() |
|
|
|
this._configureStandardClient({ rpcUrl }) |
|
|
|
this._configureStandardProvider({ rpcUrl }) |
|
|
|
this.blockTrackerProxy.on('block', this._logBlock.bind(this)) |
|
|
|
this._proxy.on('block', this._logBlock.bind(this)) |
|
|
|
this.blockTrackerProxy.on('error', this.verifyNetwork.bind(this)) |
|
|
|
this._proxy.on('error', this.verifyNetwork.bind(this)) |
|
|
|
this.ethQuery = new EthQuery(this.providerProxy) |
|
|
|
this.ethQuery = new EthQuery(this._proxy) |
|
|
|
this.lookupNetwork() |
|
|
|
this.lookupNetwork() |
|
|
|
|
|
|
|
return this._proxy |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
verifyNetwork () { |
|
|
|
verifyNetwork () { |
|
|
@ -77,10 +76,8 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`) |
|
|
|
assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`) |
|
|
|
// skip if type already matches
|
|
|
|
// skip if type already matches
|
|
|
|
if (type === this.getProviderConfig().type) return |
|
|
|
if (type === this.getProviderConfig().type) return |
|
|
|
// lookup rpcTarget for typecreateMetamaskProvider
|
|
|
|
|
|
|
|
const rpcTarget = this.getRpcAddressForType(type) |
|
|
|
const rpcTarget = this.getRpcAddressForType(type) |
|
|
|
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`) |
|
|
|
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`) |
|
|
|
// update connectioncreateMetamaskProvider
|
|
|
|
|
|
|
|
this.providerStore.updateState({ type, rpcTarget }) |
|
|
|
this.providerStore.updateState({ type, rpcTarget }) |
|
|
|
this._switchNetwork({ rpcUrl: rpcTarget }) |
|
|
|
this._switchNetwork({ rpcUrl: rpcTarget }) |
|
|
|
} |
|
|
|
} |
|
|
@ -100,29 +97,32 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
_switchNetwork (providerParams) { |
|
|
|
_switchNetwork (providerParams) { |
|
|
|
this.setNetworkState('loading') |
|
|
|
this.setNetworkState('loading') |
|
|
|
this._configureStandardClient(providerParams) |
|
|
|
this._configureStandardProvider(providerParams) |
|
|
|
this.emit('networkDidChange') |
|
|
|
this.emit('networkDidChange') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_configureStandardClient(_providerParams) { |
|
|
|
_configureStandardProvider(_providerParams) { |
|
|
|
const providerParams = extend(this._baseProviderParams, _providerParams) |
|
|
|
const providerParams = extend(this._baseProviderParams, _providerParams) |
|
|
|
const client = createEthRpcClient(providerParams) |
|
|
|
const provider = createMetamaskProvider(providerParams) |
|
|
|
this._setClient(client) |
|
|
|
this._setProvider(provider) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_setClient (newClient) { |
|
|
|
_setProvider (provider) { |
|
|
|
// teardown old client
|
|
|
|
// collect old block tracker events
|
|
|
|
const oldClient = this._currentClient |
|
|
|
const oldProvider = this._provider |
|
|
|
if (oldClient) { |
|
|
|
let blockTrackerHandlers |
|
|
|
oldClient.blockTracker.stop() |
|
|
|
if (oldProvider) { |
|
|
|
// asyncEventEmitter lacks a "removeAllListeners" method
|
|
|
|
// capture old block handlers
|
|
|
|
// oldClient.blockTracker.removeAllListeners
|
|
|
|
blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers |
|
|
|
oldClient.blockTracker._events = {} |
|
|
|
// tear down
|
|
|
|
} |
|
|
|
oldProvider.removeAllListeners() |
|
|
|
|
|
|
|
oldProvider.stop() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// override block tracler
|
|
|
|
|
|
|
|
provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers) |
|
|
|
// set as new provider
|
|
|
|
// set as new provider
|
|
|
|
this._currentClient = newClient |
|
|
|
this._provider = provider |
|
|
|
this.providerProxy.setTarget(newClient.provider) |
|
|
|
this._proxy.setTarget(provider) |
|
|
|
this.blockTrackerProxy.setTarget(newClient.blockTracker) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_logBlock (block) { |
|
|
|
_logBlock (block) { |
|
|
|