diff options
Diffstat (limited to 'app/scripts/controllers')
5 files changed, 33 insertions, 13 deletions
diff --git a/app/scripts/controllers/network/createBlockTracker.js b/app/scripts/controllers/network/createBlockTracker.js new file mode 100644 index 000000000..6573b18a1 --- /dev/null +++ b/app/scripts/controllers/network/createBlockTracker.js @@ -0,0 +1,19 @@ +const BlockTracker = require('eth-block-tracker') + +/** + * Creates a block tracker that sends platform events on success and failure + */ +module.exports = function createBlockTracker (args, platform) { + const blockTracker = new BlockTracker(args) + blockTracker.on('latest', () => { + if (platform && platform.sendMessage) { + platform.sendMessage({ action: 'ethereum-ping-success' }) + } + }) + blockTracker.on('error', () => { + if (platform && platform.sendMessage) { + platform.sendMessage({ action: 'ethereum-ping-error' }) + } + }) + return blockTracker +} diff --git a/app/scripts/controllers/network/createInfuraClient.js b/app/scripts/controllers/network/createInfuraClient.js index 5281dc4c1..884b94db3 100644 --- a/app/scripts/controllers/network/createInfuraClient.js +++ b/app/scripts/controllers/network/createInfuraClient.js @@ -7,14 +7,14 @@ const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') const createInfuraMiddleware = require('eth-json-rpc-infura') -const BlockTracker = require('eth-block-tracker') +const createBlockTracker = require('./createBlockTracker') module.exports = createInfuraClient -function createInfuraClient ({ network }) { +function createInfuraClient ({ network, platform }) { const infuraMiddleware = createInfuraMiddleware({ network, maxAttempts: 5, source: 'metamask' }) const infuraProvider = providerFromMiddleware(infuraMiddleware) - const blockTracker = new BlockTracker({ provider: infuraProvider }) + const blockTracker = createBlockTracker({ provider: infuraProvider }, platform) const networkMiddleware = mergeMiddleware([ createNetworkAndChainIdMiddleware({ network }), diff --git a/app/scripts/controllers/network/createJsonRpcClient.js b/app/scripts/controllers/network/createJsonRpcClient.js index a8cbf2aaf..369dcd299 100644 --- a/app/scripts/controllers/network/createJsonRpcClient.js +++ b/app/scripts/controllers/network/createJsonRpcClient.js @@ -5,14 +5,14 @@ const createBlockCacheMiddleware = require('eth-json-rpc-middleware/block-cache' const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache') const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') -const BlockTracker = require('eth-block-tracker') +const createBlockTracker = require('./createBlockTracker') module.exports = createJsonRpcClient -function createJsonRpcClient ({ rpcUrl }) { +function createJsonRpcClient ({ rpcUrl, platform }) { const fetchMiddleware = createFetchMiddleware({ rpcUrl }) const blockProvider = providerFromMiddleware(fetchMiddleware) - const blockTracker = new BlockTracker({ provider: blockProvider }) + const blockTracker = createBlockTracker({ provider: blockProvider }, platform) const networkMiddleware = mergeMiddleware([ createBlockRefRewriteMiddleware({ blockTracker }), diff --git a/app/scripts/controllers/network/createLocalhostClient.js b/app/scripts/controllers/network/createLocalhostClient.js index 09b1d3c1c..36593dc70 100644 --- a/app/scripts/controllers/network/createLocalhostClient.js +++ b/app/scripts/controllers/network/createLocalhostClient.js @@ -3,14 +3,14 @@ const createFetchMiddleware = require('eth-json-rpc-middleware/fetch') const createBlockRefRewriteMiddleware = require('eth-json-rpc-middleware/block-ref-rewrite') const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') -const BlockTracker = require('eth-block-tracker') +const createBlockTracker = require('./createBlockTracker') module.exports = createLocalhostClient -function createLocalhostClient () { +function createLocalhostClient ({ platform }) { const fetchMiddleware = createFetchMiddleware({ rpcUrl: 'http://localhost:8545/' }) const blockProvider = providerFromMiddleware(fetchMiddleware) - const blockTracker = new BlockTracker({ provider: blockProvider, pollingInterval: 1000 }) + const blockTracker = createBlockTracker({ provider: blockProvider, pollingInterval: 1000 }, platform) const networkMiddleware = mergeMiddleware([ createBlockRefRewriteMiddleware({ blockTracker }), diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index 2958ba3b0..0c6327f6e 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -37,8 +37,9 @@ const defaultNetworkConfig = { module.exports = class NetworkController extends EventEmitter { - constructor (opts = {}) { + constructor (opts = {}, platform) { super() + this.platform = platform // parse options const providerConfig = opts.provider || defaultProviderConfig @@ -180,7 +181,7 @@ module.exports = class NetworkController extends EventEmitter { _configureInfuraProvider ({ type }) { log.info('NetworkController - configureInfuraProvider', type) - const networkClient = createInfuraClient({ network: type }) + const networkClient = createInfuraClient({ network: type, platform: this.platform }) this._setNetworkClient(networkClient) // setup networkConfig var settings = { @@ -191,13 +192,13 @@ module.exports = class NetworkController extends EventEmitter { _configureLocalhostProvider () { log.info('NetworkController - configureLocalhostProvider') - const networkClient = createLocalhostClient() + const networkClient = createLocalhostClient({ platform: this.platform }) this._setNetworkClient(networkClient) } _configureStandardProvider ({ rpcUrl, chainId, ticker, nickname }) { log.info('NetworkController - configureStandardProvider', rpcUrl) - const networkClient = createJsonRpcClient({ rpcUrl }) + const networkClient = createJsonRpcClient({ rpcUrl, platform: this.platform }) // hack to add a 'rpc' network with chainId networks.networkList['rpc'] = { chainId: chainId, |