|
|
@ -11,52 +11,36 @@ const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] |
|
|
|
module.exports = class NetworkController extends EventEmitter { |
|
|
|
module.exports = class NetworkController extends EventEmitter { |
|
|
|
constructor (config) { |
|
|
|
constructor (config) { |
|
|
|
super() |
|
|
|
super() |
|
|
|
this.networkStore = new ObservableStore('loading') |
|
|
|
|
|
|
|
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider) |
|
|
|
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider) |
|
|
|
|
|
|
|
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._providerListeners = {} |
|
|
|
this._proxy = createEventEmitterProxy() |
|
|
|
|
|
|
|
|
|
|
|
this.on('networkDidChange', this.lookupNetwork) |
|
|
|
this.on('networkDidChange', this.lookupNetwork) |
|
|
|
this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget})) |
|
|
|
this.providerStore.subscribe((state) => this.switchNetwork({ rpcUrl: state.rpcTarget })) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get provider () { |
|
|
|
|
|
|
|
return this._proxy |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set provider (provider) { |
|
|
|
|
|
|
|
this._provider = provider |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
initializeProvider (opts, providerContructor = MetaMaskProvider) { |
|
|
|
initializeProvider (opts, providerContructor = MetaMaskProvider) { |
|
|
|
this.providerInit = opts |
|
|
|
this._baseProviderParams = opts |
|
|
|
this._provider = providerContructor(opts) |
|
|
|
const provider = providerContructor(opts) |
|
|
|
this._proxy = createEventEmitterProxy(this._provider) |
|
|
|
this._setProvider(provider) |
|
|
|
this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker) |
|
|
|
this._proxy.on('block', this._logBlock.bind(this)) |
|
|
|
this.provider.on('block', this._logBlock.bind(this)) |
|
|
|
this._proxy.on('error', this.verifyNetwork.bind(this)) |
|
|
|
this.provider.on('error', this.verifyNetwork.bind(this)) |
|
|
|
this.ethQuery = new EthQuery(this._proxy) |
|
|
|
this.ethQuery = new EthQuery(this.provider) |
|
|
|
|
|
|
|
this.lookupNetwork() |
|
|
|
this.lookupNetwork() |
|
|
|
return this.provider |
|
|
|
return this._proxy |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switchNetwork (providerInit) { |
|
|
|
switchNetwork (opts) { |
|
|
|
this.setNetworkState('loading') |
|
|
|
this.setNetworkState('loading') |
|
|
|
const newInit = extend(this.providerInit, providerInit) |
|
|
|
const providerParams = extend(this._baseProviderParams, opts) |
|
|
|
this.providerInit = newInit |
|
|
|
this._baseProviderParams = providerParams |
|
|
|
|
|
|
|
const provider = MetaMaskProvider(providerParams) |
|
|
|
this._provider.removeAllListeners() |
|
|
|
this._setProvider(provider) |
|
|
|
this._provider.stop() |
|
|
|
|
|
|
|
this._provider = MetaMaskProvider(newInit) |
|
|
|
|
|
|
|
// apply the listners created by other controllers
|
|
|
|
|
|
|
|
const blockTrackerHandlers = this.provider._blockTracker.proxyEventHandlers |
|
|
|
|
|
|
|
this.provider.setTarget(this._provider) |
|
|
|
|
|
|
|
this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers) |
|
|
|
|
|
|
|
this.emit('networkDidChange') |
|
|
|
this.emit('networkDidChange') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verifyNetwork () { |
|
|
|
verifyNetwork () { |
|
|
|
// Check network when restoring connectivity:
|
|
|
|
// Check network when restoring connectivity:
|
|
|
|
if (this.isNetworkLoading()) this.lookupNetwork() |
|
|
|
if (this.isNetworkLoading()) this.lookupNetwork() |
|
|
@ -110,6 +94,24 @@ module.exports = class NetworkController extends EventEmitter { |
|
|
|
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC |
|
|
|
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_setProvider (provider) { |
|
|
|
|
|
|
|
// collect old block tracker events
|
|
|
|
|
|
|
|
const oldProvider = this._provider |
|
|
|
|
|
|
|
let blockTrackerHandlers |
|
|
|
|
|
|
|
if (oldProvider) { |
|
|
|
|
|
|
|
// capture old block handlers
|
|
|
|
|
|
|
|
blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers |
|
|
|
|
|
|
|
// tear down
|
|
|
|
|
|
|
|
oldProvider.removeAllListeners() |
|
|
|
|
|
|
|
oldProvider.stop() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// override block tracler
|
|
|
|
|
|
|
|
provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers) |
|
|
|
|
|
|
|
// set as new provider
|
|
|
|
|
|
|
|
this._provider = provider |
|
|
|
|
|
|
|
this._proxy.setTarget(provider) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_logBlock (block) { |
|
|
|
_logBlock (block) { |
|
|
|
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`) |
|
|
|
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`) |
|
|
|
this.verifyNetwork() |
|
|
|
this.verifyNetwork() |
|
|
|