You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
219 lines
6.5 KiB
219 lines
6.5 KiB
7 years ago
|
const assert = require('assert')
|
||
8 years ago
|
const EventEmitter = require('events')
|
||
7 years ago
|
const createMetamaskProvider = require('web3-provider-engine/zero.js')
|
||
7 years ago
|
const SubproviderFromProvider = require('web3-provider-engine/subproviders/web3.js')
|
||
7 years ago
|
const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider')
|
||
8 years ago
|
const ObservableStore = require('obs-store')
|
||
8 years ago
|
const ComposedStore = require('obs-store/lib/composed')
|
||
8 years ago
|
const extend = require('xtend')
|
||
|
const EthQuery = require('eth-query')
|
||
7 years ago
|
const createEventEmitterProxy = require('../../lib/events-proxy.js')
|
||
7 years ago
|
const log = require('loglevel')
|
||
7 years ago
|
const {
|
||
|
ROPSTEN,
|
||
|
RINKEBY,
|
||
|
KOVAN,
|
||
|
MAINNET,
|
||
|
OLD_UI_NETWORK_TYPE,
|
||
|
DEFAULT_NETWORK,
|
||
|
} = require('./enums')
|
||
|
const { getNetworkEndpoints } = require('./util')
|
||
|
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET]
|
||
8 years ago
|
|
||
|
module.exports = class NetworkController extends EventEmitter {
|
||
7 years ago
|
|
||
8 years ago
|
constructor (config) {
|
||
8 years ago
|
super()
|
||
7 years ago
|
|
||
|
this._networkEndpointVersion = OLD_UI_NETWORK_TYPE
|
||
7 years ago
|
this._networkEndpoints = getNetworkEndpoints(OLD_UI_NETWORK_TYPE)
|
||
|
this._defaultRpc = this._networkEndpoints[DEFAULT_NETWORK]
|
||
7 years ago
|
|
||
8 years ago
|
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
|
||
7 years ago
|
this.networkStore = new ObservableStore('loading')
|
||
8 years ago
|
this.providerStore = new ObservableStore(config.provider)
|
||
|
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
|
||
7 years ago
|
this._proxy = createEventEmitterProxy()
|
||
8 years ago
|
|
||
8 years ago
|
this.on('networkDidChange', this.lookupNetwork)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
async setNetworkEndpoints (version) {
|
||
|
if (version === this._networkEndpointVersion) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
this._networkEndpointVersion = version
|
||
7 years ago
|
this._networkEndpoints = getNetworkEndpoints(version)
|
||
|
this._defaultRpc = this._networkEndpoints[DEFAULT_NETWORK]
|
||
7 years ago
|
const { type } = this.getProviderConfig()
|
||
|
|
||
|
return this.setProviderType(type, true)
|
||
|
}
|
||
|
|
||
7 years ago
|
initializeProvider (_providerParams) {
|
||
|
this._baseProviderParams = _providerParams
|
||
7 years ago
|
const { type, rpcTarget } = this.providerStore.getState()
|
||
|
// map rpcTarget to rpcUrl
|
||
|
const opts = {
|
||
|
type,
|
||
|
rpcUrl: rpcTarget,
|
||
|
}
|
||
|
this._configureProvider(opts)
|
||
7 years ago
|
this._proxy.on('block', this._logBlock.bind(this))
|
||
|
this._proxy.on('error', this.verifyNetwork.bind(this))
|
||
|
this.ethQuery = new EthQuery(this._proxy)
|
||
8 years ago
|
this.lookupNetwork()
|
||
7 years ago
|
return this._proxy
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
verifyNetwork () {
|
||
7 years ago
|
// Check network when restoring connectivity:
|
||
8 years ago
|
if (this.isNetworkLoading()) this.lookupNetwork()
|
||
|
}
|
||
|
|
||
|
getNetworkState () {
|
||
8 years ago
|
return this.networkStore.getState()
|
||
8 years ago
|
}
|
||
|
|
||
|
setNetworkState (network) {
|
||
8 years ago
|
return this.networkStore.putState(network)
|
||
8 years ago
|
}
|
||
|
|
||
|
isNetworkLoading () {
|
||
|
return this.getNetworkState() === 'loading'
|
||
|
}
|
||
|
|
||
8 years ago
|
lookupNetwork () {
|
||
7 years ago
|
// Prevent firing when provider is not defined.
|
||
|
if (!this.ethQuery || !this.ethQuery.sendAsync) {
|
||
7 years ago
|
return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery')
|
||
7 years ago
|
}
|
||
8 years ago
|
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({
|
||
8 years ago
|
type: 'rpc',
|
||
|
rpcTarget: rpcUrl,
|
||
8 years ago
|
})
|
||
7 years ago
|
this._switchNetwork({ rpcUrl })
|
||
8 years ago
|
}
|
||
|
|
||
|
getCurrentRpcAddress () {
|
||
8 years ago
|
const provider = this.getProviderConfig()
|
||
8 years ago
|
if (!provider) return null
|
||
|
return this.getRpcAddressForType(provider.type)
|
||
|
}
|
||
|
|
||
7 years ago
|
async setProviderType (type, forceUpdate = false) {
|
||
7 years ago
|
assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`)
|
||
|
// skip if type already matches
|
||
7 years ago
|
if (type === this.getProviderConfig().type && !forceUpdate) {
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
const rpcTarget = this.getRpcAddressForType(type)
|
||
7 years ago
|
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`)
|
||
|
this.providerStore.updateState({ type, rpcTarget })
|
||
7 years ago
|
this._switchNetwork({ type })
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
getProviderConfig () {
|
||
|
return this.providerStore.getState()
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
getRpcAddressForType (type, provider = this.getProviderConfig()) {
|
||
7 years ago
|
if (this._networkEndpoints[type]) {
|
||
|
return this._networkEndpoints[type]
|
||
|
}
|
||
|
|
||
|
return provider && provider.rpcTarget ? provider.rpcTarget : this._defaultRpc
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
//
|
||
|
// Private
|
||
|
//
|
||
|
|
||
7 years ago
|
_switchNetwork (opts) {
|
||
7 years ago
|
this.setNetworkState('loading')
|
||
7 years ago
|
this._configureProvider(opts)
|
||
7 years ago
|
this.emit('networkDidChange')
|
||
|
}
|
||
|
|
||
7 years ago
|
_configureProvider (opts) {
|
||
|
// type-based rpc endpoints
|
||
|
const { type } = opts
|
||
|
if (type) {
|
||
|
// type-based infura rpc endpoints
|
||
|
const isInfura = INFURA_PROVIDER_TYPES.includes(type)
|
||
|
opts.rpcUrl = this.getRpcAddressForType(type)
|
||
|
if (isInfura) {
|
||
|
this._configureInfuraProvider(opts)
|
||
|
// other type-based rpc endpoints
|
||
|
} else {
|
||
|
this._configureStandardProvider(opts)
|
||
|
}
|
||
|
// url-based rpc endpoints
|
||
|
} else {
|
||
|
this._configureStandardProvider(opts)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_configureInfuraProvider (opts) {
|
||
7 years ago
|
log.info('_configureInfuraProvider', opts)
|
||
7 years ago
|
const infuraProvider = createInfuraProvider({
|
||
7 years ago
|
network: opts.type,
|
||
|
})
|
||
7 years ago
|
const infuraSubprovider = new SubproviderFromProvider(infuraProvider)
|
||
7 years ago
|
const providerParams = extend(this._baseProviderParams, {
|
||
|
rpcUrl: opts.rpcUrl,
|
||
|
engineParams: {
|
||
|
pollingInterval: 8000,
|
||
7 years ago
|
blockTrackerProvider: infuraProvider,
|
||
7 years ago
|
},
|
||
7 years ago
|
dataSubprovider: infuraSubprovider,
|
||
7 years ago
|
})
|
||
|
const provider = createMetamaskProvider(providerParams)
|
||
|
this._setProvider(provider)
|
||
|
}
|
||
|
|
||
|
_configureStandardProvider ({ rpcUrl }) {
|
||
|
const providerParams = extend(this._baseProviderParams, {
|
||
|
rpcUrl,
|
||
7 years ago
|
engineParams: {
|
||
|
pollingInterval: 8000,
|
||
|
},
|
||
|
})
|
||
7 years ago
|
const provider = createMetamaskProvider(providerParams)
|
||
|
this._setProvider(provider)
|
||
|
}
|
||
|
|
||
|
_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()
|
||
7 years ago
|
}
|
||
7 years ago
|
// override block tracler
|
||
|
provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers)
|
||
7 years ago
|
// set as new provider
|
||
7 years ago
|
this._provider = provider
|
||
|
this._proxy.setTarget(provider)
|
||
7 years ago
|
}
|
||
|
|
||
8 years ago
|
_logBlock (block) {
|
||
|
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`)
|
||
|
this.verifyNetwork()
|
||
8 years ago
|
}
|
||
|
}
|