aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-12-14 05:51:36 +0800
committerkumavis <aaron@kumavis.me>2017-12-14 05:51:36 +0800
commit33e6befdaa1c3f969581117b3ad6136ee263e353 (patch)
treee66c9c952ebdf7d983f71a7091ba8937e5b2335f /app/scripts/controllers
parentc12886a3d500d1570bc7328fd6432a3d741a1028 (diff)
downloadtangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.tar
tangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.tar.gz
tangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.tar.bz2
tangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.tar.lz
tangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.tar.xz
tangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.tar.zst
tangerine-wallet-browser-33e6befdaa1c3f969581117b3ad6136ee263e353.zip
network - for infura networks use the infura block tracker provider
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/network.js57
1 files changed, 50 insertions, 7 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js
index cee918796..65d58008a 100644
--- a/app/scripts/controllers/network.js
+++ b/app/scripts/controllers/network.js
@@ -1,6 +1,7 @@
const assert = require('assert')
const EventEmitter = require('events')
const createMetamaskProvider = require('web3-provider-engine/zero.js')
+const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider')
const ObservableStore = require('obs-store')
const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
@@ -8,6 +9,7 @@ const EthQuery = require('eth-query')
const createEventEmitterProxy = require('../lib/events-proxy.js')
const RPC_ADDRESS_LIST = require('../config.js').network
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
+const INFURA_PROVIDER_TYPES = ['ropsten', 'rinkeby', 'kovan', 'mainnet']
module.exports = class NetworkController extends EventEmitter {
@@ -24,8 +26,13 @@ module.exports = class NetworkController extends EventEmitter {
initializeProvider (_providerParams) {
this._baseProviderParams = _providerParams
- const rpcUrl = this.getCurrentRpcAddress()
- this._configureStandardProvider({ rpcUrl })
+ const { type, rpcTarget } = this.providerStore.getState()
+ // map rpcTarget to rpcUrl
+ const opts = {
+ type,
+ rpcUrl: rpcTarget,
+ }
+ this._configureProvider(opts)
this._proxy.on('block', this._logBlock.bind(this))
this._proxy.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this._proxy)
@@ -83,7 +90,7 @@ module.exports = class NetworkController extends EventEmitter {
const rpcTarget = this.getRpcAddressForType(type)
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`)
this.providerStore.updateState({ type, rpcTarget })
- this._switchNetwork({ rpcUrl: rpcTarget })
+ this._switchNetwork({ type })
}
getProviderConfig () {
@@ -99,14 +106,50 @@ module.exports = class NetworkController extends EventEmitter {
// Private
//
- _switchNetwork (providerParams) {
+ _switchNetwork (opts) {
this.setNetworkState('loading')
- this._configureStandardProvider(providerParams)
+ this._configureProvider(opts)
this.emit('networkDidChange')
}
- _configureStandardProvider (_providerParams) {
- const providerParams = extend(this._baseProviderParams, _providerParams, {
+ _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) {
+ console.log('_configureInfuraProvider', opts)
+ const blockTrackerProvider = createInfuraProvider({
+ network: opts.type,
+ })
+ const providerParams = extend(this._baseProviderParams, {
+ rpcUrl: opts.rpcUrl,
+ engineParams: {
+ pollingInterval: 8000,
+ blockTrackerProvider,
+ },
+ })
+ const provider = createMetamaskProvider(providerParams)
+ this._setProvider(provider)
+ }
+
+ _configureStandardProvider ({ rpcUrl }) {
+ const providerParams = extend(this._baseProviderParams, {
+ rpcUrl,
engineParams: {
pollingInterval: 8000,
},