nifty-wallet/app/scripts/lib/account-tracker.js

100 lines
2.5 KiB
JavaScript
Raw Normal View History

/* Account Tracker
2017-01-04 14:07:08 -08:00
*
* This module is responsible for tracking any number of accounts
* and caching their current balances & transaction counts.
*
* It also tracks transaction hashes, and checks their inclusion status
* on each new block.
*/
const async = require('async')
const EthQuery = require('eth-query')
const ObservableStore = require('obs-store')
2017-04-26 21:05:45 -07:00
function noop () {}
class EthereumStore extends ObservableStore {
constructor (opts = {}) {
super({
accounts: {},
currentBlockGasLimit: '',
})
this._provider = opts.provider
this._query = new EthQuery(this._provider)
this._blockTracker = opts.blockTracker
// subscribe to latest block
this._blockTracker.on('block', this._updateForBlock.bind(this))
2017-02-03 12:35:01 -08:00
// blockTracker.currentBlock may be null
this._currentBlockNumber = this._blockTracker.currentBlock
}
//
// public
//
addAccount (address) {
const accounts = this.getState().accounts
accounts[address] = {}
this.updateState({ accounts })
if (!this._currentBlockNumber) return
this._updateAccount(address)
}
removeAccount (address) {
const accounts = this.getState().accounts
delete accounts[address]
this.updateState({ accounts })
}
//
// private
//
_updateForBlock (block) {
const blockNumber = '0x' + block.number.toString('hex')
this._currentBlockNumber = blockNumber
2017-09-22 13:33:53 -07:00
this.updateState({ currentBlockGasLimit: `0x${block.gasLimit.toString('hex')}` })
async.parallel([
this._updateAccounts.bind(this),
], (err) => {
if (err) return console.error(err)
this.emit('block', this.getState())
})
}
2017-02-02 22:32:00 -08:00
_updateAccounts (cb = noop) {
const accounts = this.getState().accounts
const addresses = Object.keys(accounts)
async.each(addresses, this._updateAccount.bind(this), cb)
}
2017-02-02 22:32:00 -08:00
_updateAccount (address, cb = noop) {
const accounts = this.getState().accounts
this._getAccount(address, (err, result) => {
if (err) return cb(err)
result.address = address
// only populate if the entry is still present
if (accounts[address]) {
accounts[address] = result
this.updateState({ accounts })
}
cb(null, result)
})
}
2017-02-02 22:32:00 -08:00
_getAccount (address, cb = noop) {
const query = this._query
async.parallel({
balance: query.getBalance.bind(query, address),
nonce: query.getTransactionCount.bind(query, address),
code: query.getCode.bind(query, address),
}, cb)
}
}
module.exports = EthereumStore