aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-09-27 12:42:30 +0800
committerkumavis <aaron@kumavis.me>2017-09-27 12:42:30 +0800
commit1877c0766c1f3ab46ec89dad0200fb8f75f52bae (patch)
tree44243d257dfb7f787882578fa14df8f01900fb45 /app/scripts/lib
parent443b1a8eb7883b6799692ddc24d0555d49bd1787 (diff)
parent6ca519e97c4c282023ab6b7788715ff8d7ec8189 (diff)
downloadtangerine-wallet-browser-1877c0766c1f3ab46ec89dad0200fb8f75f52bae.tar.gz
tangerine-wallet-browser-1877c0766c1f3ab46ec89dad0200fb8f75f52bae.tar.zst
tangerine-wallet-browser-1877c0766c1f3ab46ec89dad0200fb8f75f52bae.zip
Merge branch 'master' of github.com:MetaMask/metamask-extension into BreakOutKeyringController
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/account-tracker.js32
-rw-r--r--app/scripts/lib/pending-balance-calculator.js8
2 files changed, 23 insertions, 17 deletions
diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js
index bf949597b..e2892b1ce 100644
--- a/app/scripts/lib/account-tracker.js
+++ b/app/scripts/lib/account-tracker.js
@@ -10,15 +10,21 @@
const async = require('async')
const EthQuery = require('eth-query')
const ObservableStore = require('obs-store')
+const EventEmitter = require('events').EventEmitter
function noop () {}
-class EthereumStore extends ObservableStore {
+class AccountTracker extends EventEmitter {
constructor (opts = {}) {
- super({
+ super()
+
+ const initState = {
accounts: {},
- })
+ currentBlockGasLimit: '',
+ }
+ this.store = new ObservableStore(initState)
+
this._provider = opts.provider
this._query = new EthQuery(this._provider)
this._blockTracker = opts.blockTracker
@@ -33,17 +39,17 @@ class EthereumStore extends ObservableStore {
//
addAccount (address) {
- const accounts = this.getState().accounts
+ const accounts = this.store.getState().accounts
accounts[address] = {}
- this.updateState({ accounts })
+ this.store.updateState({ accounts })
if (!this._currentBlockNumber) return
this._updateAccount(address)
}
removeAccount (address) {
- const accounts = this.getState().accounts
+ const accounts = this.store.getState().accounts
delete accounts[address]
- this.updateState({ accounts })
+ this.store.updateState({ accounts })
}
//
@@ -54,29 +60,31 @@ class EthereumStore extends ObservableStore {
const blockNumber = '0x' + block.number.toString('hex')
this._currentBlockNumber = blockNumber
+ this.store.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())
+ this.emit('block', this.store.getState())
})
}
_updateAccounts (cb = noop) {
- const accounts = this.getState().accounts
+ const accounts = this.store.getState().accounts
const addresses = Object.keys(accounts)
async.each(addresses, this._updateAccount.bind(this), cb)
}
_updateAccount (address, cb = noop) {
- const accounts = this.getState().accounts
this._getAccount(address, (err, result) => {
if (err) return cb(err)
result.address = address
+ const accounts = this.store.getState().accounts
// only populate if the entry is still present
if (accounts[address]) {
accounts[address] = result
- this.updateState({ accounts })
+ this.store.updateState({ accounts })
}
cb(null, result)
})
@@ -93,4 +101,4 @@ class EthereumStore extends ObservableStore {
}
-module.exports = EthereumStore
+module.exports = AccountTracker
diff --git a/app/scripts/lib/pending-balance-calculator.js b/app/scripts/lib/pending-balance-calculator.js
index c66bffbbb..cea642f1a 100644
--- a/app/scripts/lib/pending-balance-calculator.js
+++ b/app/scripts/lib/pending-balance-calculator.js
@@ -19,19 +19,17 @@ class PendingBalanceCalculator {
this.getPendingTransactions(),
])
- const balance = results[0]
- const pending = results[1]
-
+ const [ balance, pending ] = results
if (!balance) return undefined
const pendingValue = pending.reduce((total, tx) => {
- return total.add(this.valueFor(tx))
+ return total.add(this.calculateMaxCost(tx))
}, new BN(0))
return `0x${balance.sub(pendingValue).toString(16)}`
}
- valueFor (tx) {
+ calculateMaxCost (tx) {
const txValue = tx.txParams.value
const value = this.hexToBn(txValue)
const gasPrice = this.hexToBn(tx.txParams.gasPrice)