2017-08-08 10:49:45 +02:00
|
|
|
const Component = require('react').Component
|
2018-03-29 17:00:44 +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
|
2018-10-25 11:21:41 +02:00
|
|
|
import TokenBalance from './token-balance'
|
|
|
|
import Identicon from './identicon'
|
2018-10-17 01:03:29 +02:00
|
|
|
import UserPreferencedCurrencyDisplay from './user-preferenced-currency-display'
|
|
|
|
import { PRIMARY, SECONDARY } from '../constants/common'
|
2018-10-26 10:26:43 +02:00
|
|
|
const { getNativeCurrency, getAssetImages, conversionRateSelector, getCurrentCurrency} = require('../selectors')
|
2017-08-08 10:49:45 +02:00
|
|
|
|
2018-10-17 01:03:29 +02:00
|
|
|
const { formatBalance } = require('../util')
|
2017-08-08 10:49:45 +02:00
|
|
|
|
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,
|
2018-10-26 10:26:43 +02:00
|
|
|
nativeCurrency: getNativeCurrency(state),
|
2018-08-30 22:19:10 +02:00
|
|
|
conversionRate: conversionRateSelector(state),
|
|
|
|
currentCurrency: getCurrentCurrency(state),
|
|
|
|
assetImages: getAssetImages(state),
|
2017-08-10 05:17:39 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 10:49:45 +02:00
|
|
|
|
|
|
|
inherits(BalanceComponent, Component)
|
|
|
|
function BalanceComponent () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
BalanceComponent.prototype.render = function () {
|
|
|
|
const props = this.props
|
2018-08-21 17:59:42 +02:00
|
|
|
const { token, network, assetImages } = props
|
2018-08-22 17:06:11 +02:00
|
|
|
const address = token && token.address
|
2018-08-23 20:54:40 +02:00
|
|
|
const image = assetImages && address ? assetImages[token.address] : undefined
|
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, {
|
2018-01-11 06:09:09 +01:00
|
|
|
diameter: 50,
|
2018-08-22 17:06:11 +02:00
|
|
|
address,
|
2017-09-07 08:03:23 +02:00
|
|
|
network,
|
2018-08-23 20:54:40 +02:00
|
|
|
image,
|
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
|
2018-10-26 10:26:43 +02:00
|
|
|
const { account, nativeCurrency } = props
|
2017-09-06 12:17:49 +02:00
|
|
|
const balanceValue = account && account.balance
|
|
|
|
const needsParse = 'needsParse' in props ? props.needsParse : true
|
2018-10-26 10:26:43 +02:00
|
|
|
const formattedBalance = balanceValue ? formatBalance(balanceValue, 6, needsParse, nativeCurrency) : '...'
|
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', {}, [
|
2018-10-21 13:12:40 +02:00
|
|
|
h(UserPreferencedCurrencyDisplay, {
|
|
|
|
className: 'token-amount',
|
2018-10-17 01:03:29 +02:00
|
|
|
value: balanceValue,
|
|
|
|
type: PRIMARY,
|
|
|
|
ethNumberOfDecimals: 3,
|
2018-10-21 13:12:40 +02:00
|
|
|
}),
|
2017-08-08 10:49:45 +02:00
|
|
|
|
2018-10-17 01:03:29 +02:00
|
|
|
showFiat && h(UserPreferencedCurrencyDisplay, {
|
2018-08-24 01:44:38 +02:00
|
|
|
value: balanceValue,
|
2018-10-17 01:03:29 +02:00
|
|
|
type: SECONDARY,
|
|
|
|
ethNumberOfDecimals: 3,
|
2018-08-24 01:44:38 +02:00
|
|
|
}),
|
2017-08-08 10:49:45 +02:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:15:30 +02:00
|
|
|
BalanceComponent.prototype.getFiatDisplayNumber = function (formattedBalance, conversionRate) {
|
|
|
|
if (formattedBalance === 'None') return formattedBalance
|
|
|
|
if (conversionRate === 0) return 'N/A'
|
|
|
|
|
|
|
|
const splitBalance = formattedBalance.split(' ')
|
|
|
|
|
2018-04-09 18:11:40 +02:00
|
|
|
const convertedNumber = (Number(splitBalance[0]) * conversionRate)
|
|
|
|
const wholePart = Math.floor(convertedNumber)
|
|
|
|
const decimalPart = convertedNumber - wholePart
|
|
|
|
|
|
|
|
return wholePart + Number(decimalPart.toPrecision(2))
|
2017-08-08 10:49:45 +02:00
|
|
|
}
|