@ -11,6 +11,8 @@ const createInfuraClient = require('./createInfuraClient')
const createJsonRpcClient = require ( './createJsonRpcClient' )
const createJsonRpcClient = require ( './createJsonRpcClient' )
const createLocalhostClient = require ( './createLocalhostClient' )
const createLocalhostClient = require ( './createLocalhostClient' )
const { createSwappableProxy , createEventEmitterProxy } = require ( 'swappable-obj-proxy' )
const { createSwappableProxy , createEventEmitterProxy } = require ( 'swappable-obj-proxy' )
const extend = require ( 'extend' )
const networks = { networkList : { } }
const {
const {
ROPSTEN ,
ROPSTEN ,
@ -29,6 +31,10 @@ const defaultProviderConfig = {
type : testMode ? RINKEBY : MAINNET ,
type : testMode ? RINKEBY : MAINNET ,
}
}
const defaultNetworkConfig = {
ticker : 'ETH' ,
}
module . exports = class NetworkController extends EventEmitter {
module . exports = class NetworkController extends EventEmitter {
constructor ( opts = { } ) {
constructor ( opts = { } ) {
@ -39,7 +45,8 @@ module.exports = class NetworkController extends EventEmitter {
// create stores
// create stores
this . providerStore = new ObservableStore ( providerConfig )
this . providerStore = new ObservableStore ( providerConfig )
this . networkStore = new ObservableStore ( 'loading' )
this . networkStore = new ObservableStore ( 'loading' )
this . store = new ComposedStore ( { provider : this . providerStore , network : this . networkStore } )
this . networkConfig = new ObservableStore ( defaultNetworkConfig )
this . store = new ComposedStore ( { provider : this . providerStore , network : this . networkStore , settings : this . networkConfig } )
this . on ( 'networkDidChange' , this . lookupNetwork )
this . on ( 'networkDidChange' , this . lookupNetwork )
// provider and block tracker
// provider and block tracker
this . _provider = null
this . _provider = null
@ -51,8 +58,8 @@ module.exports = class NetworkController extends EventEmitter {
initializeProvider ( providerParams ) {
initializeProvider ( providerParams ) {
this . _baseProviderParams = providerParams
this . _baseProviderParams = providerParams
const { type , rpcTarget } = this . providerStore . getState ( )
const { type , rpcTarget , chainId , ticker , nickname } = this . providerStore . getState ( )
this . _configureProvider ( { type , rpcTarget } )
this . _configureProvider ( { type , rpcTarget , chainId , ticker , nickname } )
this . lookupNetwork ( )
this . lookupNetwork ( )
}
}
@ -72,7 +79,20 @@ module.exports = class NetworkController extends EventEmitter {
return this . networkStore . getState ( )
return this . networkStore . getState ( )
}
}
setNetworkState ( network ) {
getNetworkConfig ( ) {
return this . networkConfig . getState ( )
}
setNetworkState ( network , type ) {
if ( network === 'loading' ) {
return this . networkStore . putState ( network )
}
// type must be defined
if ( ! type ) {
return
}
network = networks . networkList [ type ] && networks . networkList [ type ] . chainId ? networks . networkList [ type ] . chainId : network
return this . networkStore . putState ( network )
return this . networkStore . putState ( network )
}
}
@ -85,18 +105,22 @@ module.exports = class NetworkController extends EventEmitter {
if ( ! this . _provider ) {
if ( ! this . _provider ) {
return log . warn ( 'NetworkController - lookupNetwork aborted due to missing provider' )
return log . warn ( 'NetworkController - lookupNetwork aborted due to missing provider' )
}
}
var { type } = this . providerStore . getState ( )
const ethQuery = new EthQuery ( this . _provider )
const ethQuery = new EthQuery ( this . _provider )
ethQuery . sendAsync ( { method : 'net_version' } , ( err , network ) => {
ethQuery . sendAsync ( { method : 'net_version' } , ( err , network ) => {
if ( err ) return this . setNetworkState ( 'loading' )
if ( err ) return this . setNetworkState ( 'loading' )
log . info ( 'web3.getNetwork returned ' + network )
log . info ( 'web3.getNetwork returned ' + network )
this . setNetworkState ( network )
this . setNetworkState ( network , type )
} )
} )
}
}
setRpcTarget ( rpcTarget ) {
setRpcTarget ( rpcTarget , chainId , ticker = 'ETH' , nickname = '' ) {
const providerConfig = {
const providerConfig = {
type : 'rpc' ,
type : 'rpc' ,
rpcTarget ,
rpcTarget ,
chainId ,
ticker ,
nickname ,
}
}
this . providerConfig = providerConfig
this . providerConfig = providerConfig
}
}
@ -132,7 +156,7 @@ module.exports = class NetworkController extends EventEmitter {
}
}
_configureProvider ( opts ) {
_configureProvider ( opts ) {
const { type , rpcTarget } = opts
const { type , rpcTarget , chainId , ticker , nickname } = opts
// infura type-based endpoints
// infura type-based endpoints
const isInfura = INFURA _PROVIDER _TYPES . includes ( type )
const isInfura = INFURA _PROVIDER _TYPES . includes ( type )
if ( isInfura ) {
if ( isInfura ) {
@ -142,7 +166,7 @@ module.exports = class NetworkController extends EventEmitter {
this . _configureLocalhostProvider ( )
this . _configureLocalhostProvider ( )
// url-based rpc endpoints
// url-based rpc endpoints
} else if ( type === 'rpc' ) {
} else if ( type === 'rpc' ) {
this . _configureStandardProvider ( { rpcUrl : rpcTarget } )
this . _configureStandardProvider ( { rpcUrl : rpcTarget , chainId , ticker , nickname } )
} else {
} else {
throw new Error ( ` NetworkController - _configureProvider - unknown type " ${ type } " ` )
throw new Error ( ` NetworkController - _configureProvider - unknown type " ${ type } " ` )
}
}
@ -152,6 +176,11 @@ module.exports = class NetworkController extends EventEmitter {
log . info ( 'NetworkController - configureInfuraProvider' , type )
log . info ( 'NetworkController - configureInfuraProvider' , type )
const networkClient = createInfuraClient ( { network : type } )
const networkClient = createInfuraClient ( { network : type } )
this . _setNetworkClient ( networkClient )
this . _setNetworkClient ( networkClient )
// setup networkConfig
var settings = {
ticker : 'ETH' ,
}
this . networkConfig . putState ( settings )
}
}
_configureLocalhostProvider ( ) {
_configureLocalhostProvider ( ) {
@ -160,9 +189,22 @@ module.exports = class NetworkController extends EventEmitter {
this . _setNetworkClient ( networkClient )
this . _setNetworkClient ( networkClient )
}
}
_configureStandardProvider ( { rpcUrl } ) {
_configureStandardProvider ( { rpcUrl , chainId , ticker , nickname } ) {
log . info ( 'NetworkController - configureStandardProvider' , rpcUrl )
log . info ( 'NetworkController - configureStandardProvider' , rpcUrl )
const networkClient = createJsonRpcClient ( { rpcUrl } )
const networkClient = createJsonRpcClient ( { rpcUrl } )
// hack to add a 'rpc' network with chainId
networks . networkList [ 'rpc' ] = {
chainId : chainId ,
rpcUrl ,
ticker : ticker || 'ETH' ,
nickname ,
}
// setup networkConfig
var settings = {
network : chainId ,
}
settings = extend ( settings , networks . networkList [ 'rpc' ] )
this . networkConfig . putState ( settings )
this . _setNetworkClient ( networkClient )
this . _setNetworkClient ( networkClient )
}
}