1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add computed balance to account detail view

This commit is contained in:
Dan Finlay 2017-09-13 15:02:05 -07:00
parent 0ba6493175
commit a01921758b
6 changed files with 20 additions and 10 deletions

View File

@ -1,6 +1,4 @@
const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
const extend = require('xtend')
const PendingBalanceCalculator = require('../lib/pending-balance-calculator')
const BN = require('ethereumjs-util').BN
@ -12,12 +10,11 @@ class BalanceController {
this.ethStore = ethStore
this.txController = txController
const initState = extend({
const initState = {
ethBalance: undefined,
}, opts.initState)
}
this.store = new ObservableStore(initState)
const { getBalance, getPendingTransactions } = opts
this.balanceCalc = new PendingBalanceCalculator({
getBalance: () => Promise.resolve(this._getBalance()),
getPendingTransactions: this._getPendingTransactions.bind(this),
@ -46,7 +43,7 @@ class BalanceController {
const balances = store.accounts
const entry = balances[this.address]
const balance = entry.balance
return balance ? new BN(balance.substring(2), 16) : new BN(0)
return balance ? new BN(balance.substring(2), 16) : undefined
}
_getPendingTransactions () {

View File

@ -1,5 +1,4 @@
const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
const extend = require('xtend')
const BalanceController = require('./balance')
@ -14,10 +13,17 @@ class BalancesController {
computedBalances: {},
}, opts.initState)
this.store = new ObservableStore(initState)
this.balances = {}
this._initBalanceUpdating()
}
updateAllBalances () {
for (let address in this.balances) {
this.balances[address].updateBalance()
}
}
_initBalanceUpdating () {
const store = this.ethStore.getState()
this.addAnyAccountsFromStore(store)
@ -50,6 +56,7 @@ class BalancesController {
newState.computedBalances[address] = accountBalance
this.store.updateState(newState)
})
this.balances[address] = updater
updater.updateBalance()
}
}

View File

@ -22,6 +22,8 @@ class PendingBalanceCalculator {
const balance = results[0]
const pending = results[1]
if (!balance) return undefined
const pendingValue = pending.reduce((total, tx) => {
return total.add(this.valueFor(tx))
}, new BN(0))

View File

@ -121,6 +121,10 @@ module.exports = class MetamaskController extends EventEmitter {
ethStore: this.ethStore,
txController: this.txController,
})
this.networkController.on('networkDidChange', () => {
this.balancesController.updateAllBalances()
})
this.balancesController.updateAllBalances()
// notices
this.noticeController = new NoticeController({

View File

@ -32,6 +32,7 @@ function mapStateToProps (state) {
currentCurrency: state.metamask.currentCurrency,
currentAccountTab: state.metamask.currentAccountTab,
tokens: state.metamask.tokens,
computedBalances: state.metamask.computedBalances,
}
}
@ -45,7 +46,7 @@ AccountDetailScreen.prototype.render = function () {
var selected = props.address || Object.keys(props.accounts)[0]
var checksumAddress = selected && ethUtil.toChecksumAddress(selected)
var identity = props.identities[selected]
var account = props.accounts[selected]
var account = props.computedBalances[selected]
const { network, conversionRate, currentCurrency } = props
return (
@ -180,7 +181,7 @@ AccountDetailScreen.prototype.render = function () {
}, [
h(EthBalance, {
value: account && account.balance,
value: account && account.ethBalance,
conversionRate,
currentCurrency,
style: {

View File

@ -49,7 +49,6 @@ ConfirmTxScreen.prototype.render = function () {
var txParams = txData.params || {}
var isNotification = isPopupOrNotification() === 'notification'
log.info(`rendering a combined ${unconfTxList.length} unconf msg & txs`)
if (unconfTxList.length === 0) return h(Loading, { isLoading: true })