|
|
@ -3,6 +3,14 @@ const extend = require('xtend') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const log = require('loglevel') |
|
|
|
const log = require('loglevel') |
|
|
|
const pify = require('pify') |
|
|
|
const pify = require('pify') |
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
ROPSTEN, |
|
|
|
|
|
|
|
RINKEBY, |
|
|
|
|
|
|
|
KOVAN, |
|
|
|
|
|
|
|
MAINNET, |
|
|
|
|
|
|
|
} = require('./network/enums') |
|
|
|
|
|
|
|
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RecentBlocksController { |
|
|
|
class RecentBlocksController { |
|
|
|
|
|
|
|
|
|
|
@ -24,7 +32,7 @@ class RecentBlocksController { |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
constructor (opts = {}) { |
|
|
|
constructor (opts = {}) { |
|
|
|
const { blockTracker, provider } = opts |
|
|
|
const { blockTracker, provider, networkController } = opts |
|
|
|
this.blockTracker = blockTracker |
|
|
|
this.blockTracker = blockTracker |
|
|
|
this.ethQuery = new EthQuery(provider) |
|
|
|
this.ethQuery = new EthQuery(provider) |
|
|
|
this.historyLength = opts.historyLength || 40 |
|
|
|
this.historyLength = opts.historyLength || 40 |
|
|
@ -33,13 +41,30 @@ class RecentBlocksController { |
|
|
|
recentBlocks: [], |
|
|
|
recentBlocks: [], |
|
|
|
}, opts.initState) |
|
|
|
}, opts.initState) |
|
|
|
this.store = new ObservableStore(initState) |
|
|
|
this.store = new ObservableStore(initState) |
|
|
|
|
|
|
|
const blockListner = async (newBlockNumberHex) => { |
|
|
|
this.blockTracker.on('latest', async (newBlockNumberHex) => { |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
await this.processBlock(newBlockNumberHex) |
|
|
|
await this.processBlock(newBlockNumberHex) |
|
|
|
} catch (err) { |
|
|
|
} catch (err) { |
|
|
|
log.error(err) |
|
|
|
log.error(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let isListening = false |
|
|
|
|
|
|
|
const { type } = networkController.getProviderConfig() |
|
|
|
|
|
|
|
if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') { |
|
|
|
|
|
|
|
this.blockTracker.on('latest', blockListner) |
|
|
|
|
|
|
|
isListening = true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
networkController.on('networkDidChange', (newType) => { |
|
|
|
|
|
|
|
if (INFURA_PROVIDER_TYPES.includes(newType) && isListening) { |
|
|
|
|
|
|
|
this.blockTracker.removeListener('latest', blockListner) |
|
|
|
|
|
|
|
} else if ( |
|
|
|
|
|
|
|
!INFURA_PROVIDER_TYPES.includes(type) && |
|
|
|
|
|
|
|
type !== 'loading' && |
|
|
|
|
|
|
|
!isListening |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
this.blockTracker.on('latest', blockListner) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
this.backfill() |
|
|
|
this.backfill() |
|
|
|
} |
|
|
|
} |
|
|
|