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

View File

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

View File

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

View File

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

View File

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

View File

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