aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/network/enums.js26
-rw-r--r--app/scripts/controllers/network/network.js121
-rw-r--r--app/scripts/controllers/network/util.js36
-rw-r--r--app/scripts/controllers/transactions/index.js15
-rw-r--r--app/scripts/controllers/transactions/lib/tx-state-history-helper.js21
-rw-r--r--app/scripts/controllers/transactions/tx-state-manager.js4
6 files changed, 73 insertions, 150 deletions
diff --git a/app/scripts/controllers/network/enums.js b/app/scripts/controllers/network/enums.js
index 4f29e301b..9da7f309c 100644
--- a/app/scripts/controllers/network/enums.js
+++ b/app/scripts/controllers/network/enums.js
@@ -13,20 +13,6 @@ const RINKEBY_DISPLAY_NAME = 'Rinkeby'
const KOVAN_DISPLAY_NAME = 'Kovan'
const MAINNET_DISPLAY_NAME = 'Main Ethereum Network'
-const MAINNET_RPC_URL = 'https://mainnet.infura.io/metamask'
-const ROPSTEN_RPC_URL = 'https://ropsten.infura.io/metamask'
-const KOVAN_RPC_URL = 'https://kovan.infura.io/metamask'
-const RINKEBY_RPC_URL = 'https://rinkeby.infura.io/metamask'
-const LOCALHOST_RPC_URL = 'http://localhost:8545'
-
-const MAINNET_RPC_URL_BETA = 'https://mainnet.infura.io/metamask2'
-const ROPSTEN_RPC_URL_BETA = 'https://ropsten.infura.io/metamask2'
-const KOVAN_RPC_URL_BETA = 'https://kovan.infura.io/metamask2'
-const RINKEBY_RPC_URL_BETA = 'https://rinkeby.infura.io/metamask2'
-
-const DEFAULT_NETWORK = 'rinkeby'
-const OLD_UI_NETWORK_TYPE = 'network'
-const BETA_UI_NETWORK_TYPE = 'networkBeta'
module.exports = {
ROPSTEN,
@@ -41,16 +27,4 @@ module.exports = {
RINKEBY_DISPLAY_NAME,
KOVAN_DISPLAY_NAME,
MAINNET_DISPLAY_NAME,
- MAINNET_RPC_URL,
- ROPSTEN_RPC_URL,
- KOVAN_RPC_URL,
- RINKEBY_RPC_URL,
- LOCALHOST_RPC_URL,
- MAINNET_RPC_URL_BETA,
- ROPSTEN_RPC_URL_BETA,
- KOVAN_RPC_URL_BETA,
- RINKEBY_RPC_URL_BETA,
- DEFAULT_NETWORK,
- OLD_UI_NETWORK_TYPE,
- BETA_UI_NETWORK_TYPE,
}
diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js
index 2f5b81cd2..93fde7c57 100644
--- a/app/scripts/controllers/network/network.js
+++ b/app/scripts/controllers/network/network.js
@@ -14,52 +14,40 @@ const {
RINKEBY,
KOVAN,
MAINNET,
- OLD_UI_NETWORK_TYPE,
- DEFAULT_NETWORK,
+ LOCALHOST,
} = require('./enums')
-const { getNetworkEndpoints } = require('./util')
+const LOCALHOST_RPC_URL = 'http://localhost:8545'
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET]
+const env = process.env.METAMASK_ENV
+const METAMASK_DEBUG = process.env.METAMASK_DEBUG
+const testMode = (METAMASK_DEBUG || env === 'test')
+
+const defaultProviderConfig = {
+ type: testMode ? RINKEBY : MAINNET,
+}
+
module.exports = class NetworkController extends EventEmitter {
- constructor (config) {
+ constructor (opts = {}) {
super()
- this._networkEndpointVersion = OLD_UI_NETWORK_TYPE
- this._networkEndpoints = getNetworkEndpoints(OLD_UI_NETWORK_TYPE)
- this._defaultRpc = this._networkEndpoints[DEFAULT_NETWORK]
-
- config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
+ // parse options
+ const providerConfig = opts.provider || defaultProviderConfig
+ // create stores
+ this.providerStore = new ObservableStore(providerConfig)
this.networkStore = new ObservableStore('loading')
- this.providerStore = new ObservableStore(config.provider)
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
+ // create event emitter proxy
this._proxy = createEventEmitterProxy()
this.on('networkDidChange', this.lookupNetwork)
}
- async setNetworkEndpoints (version) {
- if (version === this._networkEndpointVersion) {
- return
- }
-
- this._networkEndpointVersion = version
- this._networkEndpoints = getNetworkEndpoints(version)
- this._defaultRpc = this._networkEndpoints[DEFAULT_NETWORK]
- const { type } = this.getProviderConfig()
-
- return this.setProviderType(type, true)
- }
-
initializeProvider (_providerParams) {
this._baseProviderParams = _providerParams
const { type, rpcTarget } = this.providerStore.getState()
- // map rpcTarget to rpcUrl
- const opts = {
- type,
- rpcUrl: rpcTarget,
- }
- this._configureProvider(opts)
+ this._configureProvider({ type, rpcTarget })
this._proxy.on('block', this._logBlock.bind(this))
this._proxy.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this._proxy)
@@ -96,45 +84,27 @@ module.exports = class NetworkController extends EventEmitter {
})
}
- setRpcTarget (rpcUrl) {
- this.providerStore.updateState({
+ setRpcTarget (rpcTarget) {
+ const providerConfig = {
type: 'rpc',
- rpcTarget: rpcUrl,
- })
- this._switchNetwork({ rpcUrl })
- }
-
- getCurrentRpcAddress () {
- const provider = this.getProviderConfig()
- if (!provider) return null
- return this.getRpcAddressForType(provider.type)
- }
-
- async setProviderType (type, forceUpdate = false) {
- assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`)
- // skip if type already matches
- if (type === this.getProviderConfig().type && !forceUpdate) {
- return
+ rpcTarget,
}
+ this.providerStore.updateState(providerConfig)
+ this._switchNetwork(providerConfig)
+ }
- const rpcTarget = this.getRpcAddressForType(type)
- assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`)
- this.providerStore.updateState({ type, rpcTarget })
- this._switchNetwork({ type })
+ async setProviderType (type) {
+ assert.notEqual(type, 'rpc', `NetworkController - cannot call "setProviderType" with type 'rpc'. use "setRpcTarget"`)
+ assert(INFURA_PROVIDER_TYPES.includes(type) || type === LOCALHOST, `NetworkController - Unknown rpc type "${type}"`)
+ const providerConfig = { type }
+ this.providerStore.updateState(providerConfig)
+ this._switchNetwork(providerConfig)
}
getProviderConfig () {
return this.providerStore.getState()
}
- getRpcAddressForType (type, provider = this.getProviderConfig()) {
- if (this._networkEndpoints[type]) {
- return this._networkEndpoints[type]
- }
-
- return provider && provider.rpcTarget ? provider.rpcTarget : this._defaultRpc
- }
-
//
// Private
//
@@ -146,32 +116,27 @@ module.exports = class NetworkController extends EventEmitter {
}
_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)
- }
+ const { type, rpcTarget } = opts
+ // infura type-based endpoints
+ const isInfura = INFURA_PROVIDER_TYPES.includes(type)
+ if (isInfura) {
+ this._configureInfuraProvider(opts)
+ // other type-based rpc endpoints
+ } else if (type === LOCALHOST) {
+ this._configureStandardProvider({ rpcUrl: LOCALHOST_RPC_URL })
// url-based rpc endpoints
+ } else if (type === 'rpc'){
+ this._configureStandardProvider({ rpcUrl: rpcTarget })
} else {
- this._configureStandardProvider(opts)
+ throw new Error(`NetworkController - _configureProvider - unknown type "${type}"`)
}
}
- _configureInfuraProvider (opts) {
- log.info('_configureInfuraProvider', opts)
- const infuraProvider = createInfuraProvider({
- network: opts.type,
- })
+ _configureInfuraProvider ({ type }) {
+ log.info('_configureInfuraProvider', type)
+ const infuraProvider = createInfuraProvider({ network: type })
const infuraSubprovider = new SubproviderFromProvider(infuraProvider)
const providerParams = extend(this._baseProviderParams, {
- rpcUrl: opts.rpcUrl,
engineParams: {
pollingInterval: 8000,
blockTrackerProvider: infuraProvider,
diff --git a/app/scripts/controllers/network/util.js b/app/scripts/controllers/network/util.js
index 4f38ccda4..261dae721 100644
--- a/app/scripts/controllers/network/util.js
+++ b/app/scripts/controllers/network/util.js
@@ -3,7 +3,6 @@ const {
RINKEBY,
KOVAN,
MAINNET,
- LOCALHOST,
ROPSTEN_CODE,
RINKEYBY_CODE,
KOVAN_CODE,
@@ -11,17 +10,6 @@ const {
RINKEBY_DISPLAY_NAME,
KOVAN_DISPLAY_NAME,
MAINNET_DISPLAY_NAME,
- MAINNET_RPC_URL,
- ROPSTEN_RPC_URL,
- KOVAN_RPC_URL,
- RINKEBY_RPC_URL,
- LOCALHOST_RPC_URL,
- MAINNET_RPC_URL_BETA,
- ROPSTEN_RPC_URL_BETA,
- KOVAN_RPC_URL_BETA,
- RINKEBY_RPC_URL_BETA,
- OLD_UI_NETWORK_TYPE,
- BETA_UI_NETWORK_TYPE,
} = require('./enums')
const networkToNameMap = {
@@ -34,32 +22,8 @@ const networkToNameMap = {
[KOVAN_CODE]: KOVAN_DISPLAY_NAME,
}
-const networkEndpointsMap = {
- [OLD_UI_NETWORK_TYPE]: {
- [LOCALHOST]: LOCALHOST_RPC_URL,
- [MAINNET]: MAINNET_RPC_URL,
- [ROPSTEN]: ROPSTEN_RPC_URL,
- [KOVAN]: KOVAN_RPC_URL,
- [RINKEBY]: RINKEBY_RPC_URL,
- },
- [BETA_UI_NETWORK_TYPE]: {
- [LOCALHOST]: LOCALHOST_RPC_URL,
- [MAINNET]: MAINNET_RPC_URL_BETA,
- [ROPSTEN]: ROPSTEN_RPC_URL_BETA,
- [KOVAN]: KOVAN_RPC_URL_BETA,
- [RINKEBY]: RINKEBY_RPC_URL_BETA,
- },
-}
-
const getNetworkDisplayName = key => networkToNameMap[key]
-const getNetworkEndpoints = (networkType = OLD_UI_NETWORK_TYPE) => {
- return {
- ...networkEndpointsMap[networkType],
- }
-}
-
module.exports = {
getNetworkDisplayName,
- getNetworkEndpoints,
}
diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js
index 541f1db73..3886db104 100644
--- a/app/scripts/controllers/transactions/index.js
+++ b/app/scripts/controllers/transactions/index.js
@@ -112,6 +112,21 @@ class TransactionController extends EventEmitter {
}
/**
+ Check if a txMeta in the list with the same nonce has been confirmed in a block
+ if the txParams dont have a nonce will return false
+ @returns {boolean} whether the nonce has been used in a transaction confirmed in a block
+ @param {object} txMeta - the txMeta object
+ */
+ async isNonceTaken (txMeta) {
+ const { from, nonce } = txMeta.txParams
+ if ('nonce' in txMeta.txParams) {
+ const sameNonceTxList = this.txStateManager.getFilteredTxList({from, nonce, status: 'confirmed'})
+ return (sameNonceTxList.length >= 1)
+ }
+ return false
+ }
+
+ /**
add a new unapproved transaction to the pipeline
@returns {Promise<string>} the hash of the transaction after being submitted to the network
diff --git a/app/scripts/controllers/transactions/lib/tx-state-history-helper.js b/app/scripts/controllers/transactions/lib/tx-state-history-helper.js
index 59a4b562c..4562568e9 100644
--- a/app/scripts/controllers/transactions/lib/tx-state-history-helper.js
+++ b/app/scripts/controllers/transactions/lib/tx-state-history-helper.js
@@ -25,26 +25,31 @@ function migrateFromSnapshotsToDiffs (longHistory) {
}
/**
- generates an array of history objects sense the previous state.
- The object has the keys opp(the operation preformed),
- path(the key and if a nested object then each key will be seperated with a `/`)
- value
- with the first entry having the note
+ Generates an array of history objects sense the previous state.
+ The object has the keys
+ op (the operation performed),
+ path (the key and if a nested object then each key will be seperated with a `/`)
+ value
+ with the first entry having the note and a timestamp when the change took place
@param previousState {object} - the previous state of the object
@param newState {object} - the update object
@param note {string} - a optional note for the state change
- @reurns {array}
+ @returns {array}
*/
function generateHistoryEntry (previousState, newState, note) {
const entry = jsonDiffer.compare(previousState, newState)
// Add a note to the first op, since it breaks if we append it to the entry
- if (note && entry[0]) entry[0].note = note
+ if (entry[0]) {
+ if (note) entry[0].note = note
+
+ entry[0].timestamp = Date.now()
+ }
return entry
}
/**
Recovers previous txMeta state obj
- @return {object}
+ @returns {object}
*/
function replayHistory (_shortHistory) {
const shortHistory = clone(_shortHistory)
diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js
index f05c7d095..0aae4774b 100644
--- a/app/scripts/controllers/transactions/tx-state-manager.js
+++ b/app/scripts/controllers/transactions/tx-state-manager.js
@@ -159,7 +159,7 @@ class TransactionStateManager extends EventEmitter {
/**
updates the txMeta in the list and adds a history entry
@param txMeta {Object} - the txMeta to update
- @param [note] {string} - a not about the update for history
+ @param [note] {string} - a note about the update for history
*/
updateTx (txMeta, note) {
// validate txParams
@@ -263,7 +263,7 @@ class TransactionStateManager extends EventEmitter {
*/
getTxsByMetaData (key, value, txList = this.getTxList()) {
return txList.filter((txMeta) => {
- if (txMeta.txParams[key]) {
+ if (key in txMeta.txParams) {
return txMeta.txParams[key] === value
} else {
return txMeta[key] === value