commit
9940ea71df
@ -0,0 +1,44 @@ |
||||
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 |
@ -0,0 +1,48 @@ |
||||
const assert = require('assert') |
||||
const PreferencesController = require('../../app/scripts/controllers/preferences') |
||||
|
||||
describe('preferences controller', function () { |
||||
let preferencesController |
||||
|
||||
before(() => { |
||||
preferencesController = new PreferencesController() |
||||
}) |
||||
|
||||
describe('addToken', function () { |
||||
it('should add that token to its state', async function () { |
||||
const address = '0xabcdef1234567' |
||||
const symbol = 'ABBR' |
||||
const decimals = 5 |
||||
|
||||
await preferencesController.addToken(address, symbol, decimals) |
||||
|
||||
const tokens = preferencesController.getTokens() |
||||
assert.equal(tokens.length, 1, 'one token added') |
||||
|
||||
const added = tokens[0] |
||||
assert.equal(added.address, address, 'set address correctly') |
||||
assert.equal(added.symbol, symbol, 'set symbol correctly') |
||||
assert.equal(added.decimals, decimals, 'set decimals correctly') |
||||
}) |
||||
|
||||
it('should allow updating a token value', async function () { |
||||
const address = '0xabcdef1234567' |
||||
const symbol = 'ABBR' |
||||
const decimals = 5 |
||||
|
||||
await preferencesController.addToken(address, symbol, decimals) |
||||
|
||||
const newDecimals = 6 |
||||
await preferencesController.addToken(address, symbol, newDecimals) |
||||
|
||||
const tokens = preferencesController.getTokens() |
||||
assert.equal(tokens.length, 1, 'one token added') |
||||
|
||||
const added = tokens[0] |
||||
assert.equal(added.address, address, 'set address correctly') |
||||
assert.equal(added.symbol, symbol, 'set symbol correctly') |
||||
assert.equal(added.decimals, newDecimals, 'updated decimals correctly') |
||||
}) |
||||
}) |
||||
}) |
||||
|
Loading…
Reference in new issue