1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 16:18:07 +01:00
metamask-extension/app/scripts/lib/eth-store.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-01-04 23:07:08 +01:00
/* Ethereum Store
*
* 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.
*/
2017-01-04 22:56:21 +01:00
const async = require('async')
const EthQuery = require('eth-query')
const ObservableStore = require('obs-store')
function noop() {}
2017-01-04 22:56:21 +01:00
class EthereumStore extends ObservableStore {
2017-01-04 22:56:21 +01:00
constructor (opts = {}) {
super({
accounts: {},
transactions: {},
})
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 21:35:01 +01:00
// blockTracker.currentBlock may be null
this._currentBlockNumber = this._blockTracker.currentBlock
2017-01-04 22:56:21 +01:00
}
//
// public
//
2017-01-04 22:56:21 +01:00
addAccount (address) {
const accounts = this.getState().accounts
accounts[address] = {}
this.updateState({ accounts })
if (!this._currentBlockNumber) return
this._updateAccount(address)
}
2017-01-04 22:56:21 +01:00
removeAccount (address) {
const accounts = this.getState().accounts
delete accounts[address]
this.updateState({ accounts })
}
2017-01-04 22:56:21 +01:00
addTransaction (txHash) {
const transactions = this.getState().transactions
transactions[txHash] = {}
this.updateState({ transactions })
if (!this._currentBlockNumber) return
this._updateTransaction(this._currentBlockNumber, txHash, noop)
}
2017-01-04 22:56:21 +01:00
removeTransaction (txHash) {
const transactions = this.getState().transactions
delete transactions[txHash]
this.updateState({ transactions })
}
2017-01-04 22:56:21 +01:00
//
// private
//
_updateForBlock (block) {
const blockNumber = '0x' + block.number.toString('hex')
this._currentBlockNumber = blockNumber
async.parallel([
this._updateAccounts.bind(this),
this._updateTransactions.bind(this, blockNumber),
], (err) => {
if (err) return console.error(err)
this.emit('block', this.getState())
})
}
2017-01-04 22:56:21 +01:00
2017-02-03 07:32:00 +01:00
_updateAccounts (cb = noop) {
const accounts = this.getState().accounts
const addresses = Object.keys(accounts)
async.each(addresses, this._updateAccount.bind(this), cb)
}
2017-01-04 22:56:21 +01:00
2017-02-03 07:32:00 +01: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-01-04 22:56:21 +01:00
2017-02-03 07:32:00 +01:00
_updateTransactions (block, cb = noop) {
const transactions = this.getState().transactions
const txHashes = Object.keys(transactions)
async.each(txHashes, this._updateTransaction.bind(this, block), cb)
}
2017-01-04 22:56:21 +01:00
2017-02-03 07:32:00 +01:00
_updateTransaction (block, txHash, cb = noop) {
// would use the block here to determine how many confirmations the tx has
const transactions = this.getState().transactions
this._query.getTransaction(txHash, (err, result) => {
if (err) return cb(err)
// only populate if the entry is still present
if (transactions[txHash]) {
transactions[txHash] = result
this.updateState({ transactions })
}
cb(null, result)
})
}
2017-02-03 07:32:00 +01: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)
}
2017-01-04 22:56:21 +01:00
}
module.exports = EthereumStore