1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
This commit is contained in:
Dan Finlay 2017-01-04 14:05:00 -08:00
parent 0fab22082a
commit 381a60695d

View File

@ -24,12 +24,12 @@ function EthereumStore(engine) {
// public
//
EthereumStore.prototype.getState = function(){
EthereumStore.prototype.getState = function () {
const self = this
return clone(self._currentState)
}
EthereumStore.prototype.addAccount = function(address){
EthereumStore.prototype.addAccount = function (address) {
const self = this
self._currentState.accounts[address] = {}
self._didUpdate()
@ -37,13 +37,13 @@ EthereumStore.prototype.addAccount = function(address){
self._updateAccount(self.currentBlockNumber, address, noop)
}
EthereumStore.prototype.removeAccount = function(address){
EthereumStore.prototype.removeAccount = function (address) {
const self = this
delete self._currentState.accounts[address]
self._didUpdate()
}
EthereumStore.prototype.addTransaction = function(txHash){
EthereumStore.prototype.addTransaction = function (txHash) {
const self = this
self._currentState.transactions[txHash] = {}
self._didUpdate()
@ -51,7 +51,7 @@ EthereumStore.prototype.addTransaction = function(txHash){
self._updateTransaction(self.currentBlockNumber, txHash, noop)
}
EthereumStore.prototype.removeTransaction = function(address){
EthereumStore.prototype.removeTransaction = function (address) {
const self = this
delete self._currentState.transactions[address]
self._didUpdate()
@ -62,36 +62,36 @@ EthereumStore.prototype.removeTransaction = function(address){
// private
//
EthereumStore.prototype._didUpdate = function() {
EthereumStore.prototype._didUpdate = function () {
const self = this
var state = self.getState()
self.emit('update', state)
}
EthereumStore.prototype._updateForBlock = function(block) {
EthereumStore.prototype._updateForBlock = function (block) {
const self = this
var blockNumber = '0x'+block.number.toString('hex')
self.currentBlockNumber = blockNumber
async.parallel([
self._updateAccounts.bind(self),
self._updateTransactions.bind(self, blockNumber),
], function(err){
], function (err) {
if (err) return console.error(err)
self.emit('block', self.getState())
})
}
EthereumStore.prototype._updateAccounts = function(cb) {
EthereumStore.prototype._updateAccounts = function (cb) {
const self = this
var accountsState = self._currentState.accounts
var addresses = Object.keys(accountsState)
async.each(addresses, self._updateAccount.bind(self), cb)
}
EthereumStore.prototype._updateAccount = function(address, cb) {
EthereumStore.prototype._updateAccount = function (address, cb) {
const self = this
var accountsState = self._currentState.accounts
self._query.getAccount(address, function(err, result){
self._query.getAccount(address, function (err, result) {
if (err) return cb(err)
result.address = address
// only populate if the entry is still present
@ -103,7 +103,7 @@ EthereumStore.prototype._updateAccount = function(address, cb) {
})
}
EthereumStore.prototype.getAccount = function(address, cb){
EthereumStore.prototype.getAccount = function (address, cb) {
const block = 'latest'
async.parallel({
balance: this._query.getBalance.bind(this, address, block),
@ -112,21 +112,20 @@ EthereumStore.prototype.getAccount = function(address, cb){
}, cb)
}
EthereumStore.prototype._updateTransactions = function(block, cb) {
EthereumStore.prototype._updateTransactions = function (block, cb) {
const self = this
var transactionsState = self._currentState.transactions
var txHashes = Object.keys(transactionsState)
async.each(txHashes, self._updateTransaction.bind(self, block), cb)
}
EthereumStore.prototype._updateTransaction = function(block, txHash, cb) {
EthereumStore.prototype._updateTransaction = function (block, txHash, cb) {
const self = this
// would use the block here to determine how many confirmations the tx has
var transactionsState = self._currentState.transactions
self._query.getTransaction(txHash, function(err, result){
self._query.getTransaction(txHash, function (err, result) {
if (err) return cb(err)
// only populate if the entry is still present
if (transactionsState[txHash]) {
// only populate if the entry is still present if (transactionsState[txHash]) {
transactionsState[txHash] = result
self._didUpdate()
}
@ -134,8 +133,4 @@ EthereumStore.prototype._updateTransaction = function(block, txHash, cb) {
})
}
function valuesFor(obj){
return Object.keys(obj).map(function(key){ return obj[key] })
}
function noop(){}
function noop() {}