aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/scripts/controllers/network.js84
-rw-r--r--app/scripts/controllers/transactions.js4
-rw-r--r--app/scripts/first-time-state.js3
-rw-r--r--app/scripts/lib/config-manager.js29
-rw-r--r--app/scripts/metamask-controller.js3
5 files changed, 68 insertions, 55 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js
index 97c2ccbc2..4fdd92921 100644
--- a/app/scripts/controllers/network.js
+++ b/app/scripts/controllers/network.js
@@ -1,23 +1,23 @@
const EventEmitter = require('events')
const MetaMaskProvider = require('web3-provider-engine/zero.js')
const ObservableStore = require('obs-store')
+const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
const EthQuery = require('eth-query')
const RPC_ADDRESS_LIST = require('../config.js').network
+const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
module.exports = class NetworkController extends EventEmitter {
- constructor (providerOpts) {
+ constructor (config) {
super()
- this.networkStore = new ObservableStore({ network: 'loading' })
- providerOpts.provider.rpcTarget = this.getRpcAddressForType(providerOpts.provider.type, providerOpts.provider)
- this.providerStore = new ObservableStore(providerOpts)
- this.store = new ObservableStore(extend(this.networkStore.getState(), this.providerStore.getState()))
+ 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 = {}
- this._providerListners = {}
-
- this.networkStore.subscribe((state) => this.store.updateState(state))
- this.providerStore.subscribe((state) => this.store.updateState(state))
- this.on('networkSwitch', this.lookupNetwork)
+ this.on('networkDidChange', this.lookupNetwork)
+ this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget}))
}
get provider () {
@@ -28,15 +28,8 @@ module.exports = class NetworkController extends EventEmitter {
this._provider = provider
}
- getState () {
- return extend({},
- this.networkStore.getState(),
- this.providerStore.getState()
- )
- }
-
initializeProvider (opts) {
- this.providerConfig = opts
+ this.providerInit = opts
this._provider = MetaMaskProvider(opts)
this._proxy = new Proxy(this._provider, {
get: (obj, name) => {
@@ -54,16 +47,18 @@ module.exports = class NetworkController extends EventEmitter {
return this.provider
}
- switchNetwork (providerConfig) {
- const newConfig = extend(this.providerConfig, providerConfig)
- this.providerConfig = newConfig
+ switchNetwork (providerInit) {
+ this.setNetworkState('loading')
+ const newInit = extend(this.providerInit, providerInit)
+ this.providerInit = newInit
- this.provider = MetaMaskProvider(newConfig)
+ this._provider.removeAllListeners()
+ this.provider = MetaMaskProvider(newInit)
// apply the listners created by other controllers
- Object.keys(this._providerListners).forEach((key) => {
- this._providerListners[key].forEach((handler) => this._provider.addListener(key, handler))
+ Object.keys(this._providerListeners).forEach((key) => {
+ this._providerListeners[key].forEach((handler) => this._provider.addListener(key, handler))
})
- this.emit('networkSwitch', this.provider)
+ this.emit('networkDidChange')
}
@@ -73,20 +68,18 @@ module.exports = class NetworkController extends EventEmitter {
}
getNetworkState () {
- return this.networkStore.getState().network
+ return this.networkStore.getState()
}
setNetworkState (network) {
- return this.networkStore.updateState({ network })
+ return this.networkStore.putState(network)
}
isNetworkLoading () {
return this.getNetworkState() === 'loading'
}
- lookupNetwork (err) {
- if (err) this.setNetworkState('loading')
-
+ lookupNetwork () {
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
if (err) return this.setNetworkState('loading')
log.info('web3.getNetwork returned ' + network)
@@ -96,37 +89,30 @@ module.exports = class NetworkController extends EventEmitter {
setRpcTarget (rpcUrl) {
this.providerStore.updateState({
- provider: {
- type: 'rpc',
- rpcTarget: rpcUrl,
- },
+ type: 'rpc',
+ rpcTarget: rpcUrl,
})
}
getCurrentRpcAddress () {
- var provider = this.getProvider()
+ const provider = this.getProviderConfig()
if (!provider) return null
return this.getRpcAddressForType(provider.type)
}
setProviderType (type) {
- if (type === this.getProvider().type) return
+ if (type === this.getProviderConfig().type) return
const rpcTarget = this.getRpcAddressForType(type)
- this.networkStore.updateState({network: 'loading'})
- this.switchNetwork({
- rpcUrl: rpcTarget,
- })
- this.providerStore.updateState({provider: {type, rpcTarget}})
+ this.providerStore.updateState({type, rpcTarget})
}
- getProvider () {
- return this.providerStore.getState().provider
+ getProviderConfig () {
+ return this.providerStore.getState()
}
- getRpcAddressForType (type, provider = this.getProvider()) {
- console.log(`#getRpcAddressForType: ${type}`)
- if (type in RPC_ADDRESS_LIST) return RPC_ADDRESS_LIST[type]
- return provider && provider.rpcTarget ? provider.rpcTarget : RPC_ADDRESS_LIST['rinkeby']
+ getRpcAddressForType (type, provider = this.getProviderConfig()) {
+ if (RPC_ADDRESS_LIST[type]) return RPC_ADDRESS_LIST[type]
+ return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
}
_logBlock (block) {
@@ -135,8 +121,8 @@ module.exports = class NetworkController extends EventEmitter {
}
_on (event, handler) {
- if (!this._providerListners[event]) this._providerListners[event] = []
- this._providerListners[event].push(handler)
+ if (!this._providerListeners[event]) this._providerListeners[event] = []
+ this._providerListeners[event].push(handler)
this._provider.on(event, handler)
}
}
diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js
index b9bea2f1c..2ebeed3ab 100644
--- a/app/scripts/controllers/transactions.js
+++ b/app/scripts/controllers/transactions.js
@@ -21,9 +21,7 @@ module.exports = class TransactionManager extends EventEmitter {
this.blockTracker = opts.blockTracker
this.query = opts.ethQuery
this.txProviderUtils = new TxProviderUtil(this.query)
- this.networkStore.subscribe((_) => this.blockTracker.on('block', this.checkForTxInBlock.bind(this)))
this.blockTracker.on('block', this.checkForTxInBlock.bind(this))
-
this.signEthTx = opts.signTransaction
this.nonceLock = Semaphore(1)
@@ -39,7 +37,7 @@ module.exports = class TransactionManager extends EventEmitter {
}
getNetwork () {
- return this.networkStore.getState().network
+ return this.networkStore.getState()
}
getSelectedAddress () {
diff --git a/app/scripts/first-time-state.js b/app/scripts/first-time-state.js
index 29ec1d8d3..dc7788311 100644
--- a/app/scripts/first-time-state.js
+++ b/app/scripts/first-time-state.js
@@ -3,7 +3,8 @@
//
module.exports = {
- config: {
+ config: {},
+ NetworkController: {
provider: {
type: 'rinkeby',
},
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js
index fee8423fa..9c0dffe9c 100644
--- a/app/scripts/lib/config-manager.js
+++ b/app/scripts/lib/config-manager.js
@@ -107,6 +107,35 @@ ConfigManager.prototype.getSeedWords = function () {
var data = this.getData()
return data.seedWords
}
+ConfigManager.prototype.setRpcTarget = function (rpcUrl) {
+ var config = this.getConfig()
+ config.provider = {
+ type: 'rpc',
+ rpcTarget: rpcUrl,
+ }
+ this.setConfig(config)
+}
+
+ConfigManager.prototype.setProviderType = function (type) {
+ var config = this.getConfig()
+ config.provider = {
+ type: type,
+ }
+ this.setConfig(config)
+}
+
+ConfigManager.prototype.useEtherscanProvider = function () {
+ var config = this.getConfig()
+ config.provider = {
+ type: 'etherscan',
+ }
+ this.setConfig(config)
+}
+
+ConfigManager.prototype.getProvider = function () {
+ var config = this.getConfig()
+ return config.provider
+}
ConfigManager.prototype.getCurrentRpcAddress = function () {
var provider = this.getProvider()
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index ef82da0d3..c0ad1e93b 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -65,7 +65,6 @@ module.exports = class MetamaskController extends EventEmitter {
// eth data query tools
this.ethQuery = new EthQuery(this.provider)
this.ethStore = new EthStore({
- network: this.networkController.networkStore,
provider: this.provider,
blockTracker: this.provider,
})
@@ -139,7 +138,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.shapeshiftController.store.subscribe((state) => {
this.store.updateState({ ShapeShiftController: state })
})
- this.networkController.providerStore.subscribe((state) => {
+ this.networkController.store.subscribe((state) => {
this.store.updateState({ NetworkController: state })
})