aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
-rw-r--r--test/unit/network-contoller-test.js74
-rw-r--r--test/unit/tx-controller-test.js4
-rw-r--r--test/unit/tx-utils-test.js6
8 files changed, 150 insertions, 57 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 })
})
diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js
new file mode 100644
index 000000000..183e69cab
--- /dev/null
+++ b/test/unit/network-contoller-test.js
@@ -0,0 +1,74 @@
+const EventEmitter = require('events')
+const assert = require('assert')
+const NetworkController = require('../../app/scripts/controllers/network')
+
+describe('# Network Controller', function () {
+ let networkController
+
+ beforeEach(function () {
+ networkController = new NetworkController({
+ provider: {
+ type: 'rinkeby',
+ },
+ })
+ // stub out provider
+ networkController._provider = new EventEmitter()
+ networkController.providerInit = {
+ getAccounts: () => {},
+ }
+
+ networkController.ethQuery = new Proxy({}, {
+ get: (obj, name) => {
+ return () => {}
+ },
+ })
+ })
+ describe('network', function () {
+ describe('#provider', function() {
+ it('provider should be updatable without reassignment', function () {
+ networkController.initializeProvider(networkController.providerInit)
+ const provider = networkController.provider
+ networkController._provider = {test: true}
+ assert.ok(provider.test)
+ })
+ })
+ describe('#getNetworkState', function () {
+ it('should return loading when new', function () {
+ let networkState = networkController.getNetworkState()
+ assert.equal(networkState, 'loading', 'network is loading')
+ })
+ })
+
+ describe('#setNetworkState', function () {
+ it('should update the network', function () {
+ networkController.setNetworkState(1)
+ let networkState = networkController.getNetworkState()
+ assert.equal(networkState, 1, 'network is 1')
+ })
+ })
+
+ describe('#getRpcAddressForType', function () {
+ it('should return the right rpc address', function () {
+ let rpcTarget = networkController.getRpcAddressForType('mainnet')
+ assert.equal(rpcTarget, 'https://mainnet.infura.io/metamask', 'returns the right rpcAddress')
+ })
+ })
+ describe('#setProviderType', function () {
+ it('should update provider.type', function () {
+ networkController.setProviderType('mainnet')
+ const type = networkController.getProviderConfig().type
+ assert.equal(type, 'mainnet', 'provider type is updated')
+ })
+ it('should set the network to loading', function () {
+ networkController.setProviderType('mainnet')
+ const loading = networkController.isNetworkLoading()
+ assert.ok(loading, 'network is loading')
+ })
+ it('should set the right rpcTarget', function () {
+ networkController.setProviderType('mainnet')
+ const rpcTarget = networkController.getProviderConfig().rpcTarget
+ assert.equal(rpcTarget, 'https://mainnet.infura.io/metamask', 'returns the right rpcAddress')
+ })
+ })
+ })
+})
diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js
index d4e8d79f0..711e1ea79 100644
--- a/test/unit/tx-controller-test.js
+++ b/test/unit/tx-controller-test.js
@@ -2,6 +2,7 @@ const assert = require('assert')
const EventEmitter = require('events')
const ethUtil = require('ethereumjs-util')
const EthTx = require('ethereumjs-tx')
+const EthQuery = require('eth-query')
const ObservableStore = require('obs-store')
const clone = require('clone')
const sinon = require('sinon')
@@ -16,9 +17,10 @@ describe('Transaction Controller', function () {
beforeEach(function () {
txController = new TransactionController({
- networkStore: new ObservableStore({ network: currentNetworkId }),
+ networkStore: new ObservableStore(currentNetworkId),
txHistoryLimit: 10,
blockTracker: new EventEmitter(),
+ ethQuery: new EthQuery(new EventEmitter()),
signTransaction: (ethTx) => new Promise((resolve) => {
ethTx.sign(privKey)
resolve()
diff --git a/test/unit/tx-utils-test.js b/test/unit/tx-utils-test.js
index 57d4638a0..7ace1f587 100644
--- a/test/unit/tx-utils-test.js
+++ b/test/unit/tx-utils-test.js
@@ -9,7 +9,11 @@ describe('txUtils', function () {
let txUtils
before(function () {
- txUtils = new TxUtils()
+ txUtils = new TxUtils(new Proxy({}, {
+ get: (obj, name) => {
+ return () => {}
+ },
+ }))
})
describe('chain Id', function () {