mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-30 16:18:07 +01:00
30b45c8a38
Only record gas prices, because that has a current use.
45 lines
990 B
JavaScript
45 lines
990 B
JavaScript
const ObservableStore = require('obs-store')
|
|
const extend = require('xtend')
|
|
|
|
class RecentBlocksController {
|
|
|
|
constructor (opts = {}) {
|
|
const { blockTracker } = opts
|
|
this.blockTracker = blockTracker
|
|
this.historyLength = opts.historyLength || 40
|
|
|
|
const initState = extend({
|
|
recentBlocks: [],
|
|
}, opts.initState)
|
|
this.store = new ObservableStore(initState)
|
|
|
|
this.blockTracker.on('block', this.processBlock.bind(this))
|
|
}
|
|
|
|
resetState () {
|
|
this.store.updateState({
|
|
recentBlocks: [],
|
|
})
|
|
}
|
|
|
|
processBlock (newBlock) {
|
|
const block = extend(newBlock, {
|
|
gasPrices: newBlock.transactions.map((tx) => {
|
|
return tx.gasPrice
|
|
}),
|
|
})
|
|
delete block.transactions
|
|
|
|
const state = this.store.getState()
|
|
state.recentBlocks.push(block)
|
|
|
|
while (state.recentBlocks.length > this.historyLength) {
|
|
state.recentBlocks.shift()
|
|
}
|
|
|
|
this.store.updateState(state)
|
|
}
|
|
}
|
|
|
|
module.exports = RecentBlocksController
|