2017-08-08 10:49:45 +02:00
|
|
|
const Component = require('react').Component
|
2017-08-10 05:17:39 +02:00
|
|
|
const connect = require('react-redux').connect
|
2017-08-08 10:49:45 +02:00
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2017-09-06 12:17:49 +02:00
|
|
|
const TokenBalance = require('./token-balance')
|
2017-09-07 08:03:23 +02:00
|
|
|
const Identicon = require('./identicon')
|
2017-08-08 10:49:45 +02:00
|
|
|
|
|
|
|
const { formatBalance, generateBalanceObject } = require('../util')
|
|
|
|
|
2017-08-10 05:17:39 +02:00
|
|
|
module.exports = connect(mapStateToProps)(BalanceComponent)
|
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
2017-09-06 12:17:49 +02:00
|
|
|
const accounts = state.metamask.accounts
|
2017-09-07 08:03:23 +02:00
|
|
|
const network = state.metamask.network
|
2017-09-06 12:17:49 +02:00
|
|
|
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
|
|
|
|
const account = accounts[selectedAddress]
|
|
|
|
|
2017-08-10 05:17:39 +02:00
|
|
|
return {
|
2017-09-06 12:17:49 +02:00
|
|
|
account,
|
2017-09-07 08:03:23 +02:00
|
|
|
network,
|
2017-08-10 05:17:39 +02:00
|
|
|
conversionRate: state.metamask.conversionRate,
|
|
|
|
currentCurrency: state.metamask.currentCurrency,
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 10:49:45 +02:00
|
|
|
|
|
|
|
inherits(BalanceComponent, Component)
|
|
|
|
function BalanceComponent () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
BalanceComponent.prototype.render = function () {
|
|
|
|
const props = this.props
|
2017-09-07 08:03:23 +02:00
|
|
|
const { token, network } = props
|
2017-08-08 10:49:45 +02:00
|
|
|
|
|
|
|
return h('div.balance-container', {}, [
|
|
|
|
|
|
|
|
// TODO: balance icon needs to be passed in
|
2017-09-07 08:03:23 +02:00
|
|
|
// h('img.balance-icon', {
|
|
|
|
// src: '../images/eth_logo.svg',
|
|
|
|
// style: {},
|
|
|
|
// }),
|
|
|
|
h(Identicon, {
|
|
|
|
diameter: 45,
|
|
|
|
address: token && token.address,
|
|
|
|
network,
|
2017-08-08 10:49:45 +02:00
|
|
|
}),
|
|
|
|
|
2017-09-06 12:17:49 +02:00
|
|
|
token ? this.renderTokenBalance() : this.renderBalance(),
|
2017-08-08 10:49:45 +02:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2017-09-06 12:17:49 +02:00
|
|
|
BalanceComponent.prototype.renderTokenBalance = function () {
|
|
|
|
const { token } = this.props
|
|
|
|
|
|
|
|
return h('div.flex-column.balance-display', [
|
|
|
|
h('div.token-amount', [ h(TokenBalance, { token }) ]),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
BalanceComponent.prototype.renderBalance = function () {
|
2017-08-08 10:49:45 +02:00
|
|
|
const props = this.props
|
2017-09-06 12:17:49 +02:00
|
|
|
const { shorten, account } = props
|
|
|
|
const balanceValue = account && account.balance
|
|
|
|
const needsParse = 'needsParse' in props ? props.needsParse : true
|
|
|
|
const formattedBalance = balanceValue ? formatBalance(balanceValue, 6, needsParse) : '...'
|
2017-08-08 10:49:45 +02:00
|
|
|
const showFiat = 'showFiat' in props ? props.showFiat : true
|
|
|
|
|
|
|
|
if (formattedBalance === 'None' || formattedBalance === '...') {
|
|
|
|
return h('div.flex-column.balance-display', {}, [
|
|
|
|
h('div.token-amount', {
|
|
|
|
style: {},
|
|
|
|
}, formattedBalance),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
return h('div.flex-column.balance-display', {}, [
|
|
|
|
h('div.token-amount', {
|
|
|
|
style: {},
|
2017-08-08 14:15:30 +02:00
|
|
|
}, this.getTokenBalance(formattedBalance, shorten)),
|
2017-08-08 10:49:45 +02:00
|
|
|
|
|
|
|
showFiat ? this.renderFiatValue(formattedBalance) : null,
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
BalanceComponent.prototype.renderFiatValue = function (formattedBalance) {
|
|
|
|
|
2017-08-10 05:17:39 +02:00
|
|
|
const { conversionRate, currentCurrency } = this.props
|
2017-08-08 10:49:45 +02:00
|
|
|
|
2017-08-08 14:15:30 +02:00
|
|
|
const fiatDisplayNumber = this.getFiatDisplayNumber(formattedBalance, conversionRate)
|
2017-08-08 10:49:45 +02:00
|
|
|
|
2017-08-08 14:15:30 +02:00
|
|
|
return this.renderFiatAmount(fiatDisplayNumber, currentCurrency)
|
|
|
|
}
|
|
|
|
|
|
|
|
BalanceComponent.prototype.renderFiatAmount = function (fiatDisplayNumber, fiatSuffix) {
|
|
|
|
if (fiatDisplayNumber === 'N/A') return null
|
2017-08-08 10:49:45 +02:00
|
|
|
|
2017-08-08 14:15:30 +02:00
|
|
|
return h('div.fiat-amount', {
|
|
|
|
style: {},
|
|
|
|
}, `${fiatDisplayNumber} ${fiatSuffix}`)
|
2017-08-08 10:49:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 14:15:30 +02:00
|
|
|
BalanceComponent.prototype.getTokenBalance = function (formattedBalance, shorten) {
|
|
|
|
const balanceObj = generateBalanceObject(formattedBalance, shorten ? 1 : 3)
|
|
|
|
|
|
|
|
const balanceValue = shorten ? balanceObj.shortBalance : balanceObj.balance
|
|
|
|
const label = balanceObj.label
|
|
|
|
|
|
|
|
return `${balanceValue} ${label}`
|
|
|
|
}
|
|
|
|
|
|
|
|
BalanceComponent.prototype.getFiatDisplayNumber = function (formattedBalance, conversionRate) {
|
|
|
|
if (formattedBalance === 'None') return formattedBalance
|
|
|
|
if (conversionRate === 0) return 'N/A'
|
|
|
|
|
|
|
|
const splitBalance = formattedBalance.split(' ')
|
|
|
|
|
|
|
|
return (Number(splitBalance[0]) * conversionRate).toFixed(2)
|
2017-08-08 10:49:45 +02:00
|
|
|
}
|