From 9d577ea0231802279ea0070a598f7dea9637f652 Mon Sep 17 00:00:00 2001 From: Frankie Date: Wed, 6 Jul 2016 17:04:09 -0700 Subject: [PATCH] Add decimal hendeling to ETH balance --- test/unit/util_test.js | 22 +++++---------------- ui/app/account-detail.js | 1 + ui/app/components/eth-balance.js | 34 ++++++++++++++++++-------------- ui/app/util.js | 33 +++++++++++++++++++------------ 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/test/unit/util_test.js b/test/unit/util_test.js index e2390c8d4..514a2a432 100644 --- a/test/unit/util_test.js +++ b/test/unit/util_test.js @@ -142,35 +142,23 @@ describe('util', function() { it('when given nothing', function() { var result = util.formatBalance() - assert.equal(result, 'None', 'should return "None"') - }) - - it('should return eth as string followed by ETH', function() { - var input = new ethUtil.BN(ethInWei, 10).toJSON() - var result = util.formatBalance(input, 4) - assert.equal(result, '1.0000 ETH') - }) - - it('should return eth as string followed by ETH', function() { - var input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON() - var result = util.formatBalance(input, 3) - assert.equal(result, '0.500 ETH') + assert.equal(result.formatted, 'None', 'should return "None"') }) it('should display specified decimal points', function() { var input = "0x128dfa6a90b28000" var result = util.formatBalance(input, 2) - assert.equal(result, '1.33 ETH') + assert.equal(result.formatted, '1.33 ETH') }) - it('should default to 3 decimal points', function() { + it('should default to 2 decimal points', function() { var input = "0x128dfa6a90b28000" var result = util.formatBalance(input) - assert.equal(result, '1.337 ETH') + assert.equal(result.formatted, '1.33 ETH') }) it('should show 2 significant digits for tiny balances', function() { var input = "0x1230fa6a90b28" var result = util.formatBalance(input) - assert.equal(result, '0.00032 ETH') + assert.equal(result.formatted, '0.00032 ETH') }) }) diff --git a/ui/app/account-detail.js b/ui/app/account-detail.js index 62b9d80ae..6d50dbd71 100644 --- a/ui/app/account-detail.js +++ b/ui/app/account-detail.js @@ -163,6 +163,7 @@ AccountDetailScreen.prototype.render = function () { h(EtherBalance, { value: account && account.balance, + mainBalance: true, style: { lineHeight: '7px', marginTop: '10px', diff --git a/ui/app/components/eth-balance.js b/ui/app/components/eth-balance.js index c7240ea21..0294d0d9f 100644 --- a/ui/app/components/eth-balance.js +++ b/ui/app/components/eth-balance.js @@ -2,6 +2,7 @@ const Component = require('react').Component const h = require('react-hyperscript') const inherits = require('util').inherits const formatBalance = require('../util').formatBalance +const Tooltip = require('./tooltip') module.exports = EthBalanceComponent @@ -30,28 +31,31 @@ EthBalanceComponent.prototype.render = function () { ) } EthBalanceComponent.prototype.renderBalance = function (value) { - if (value === 'None') return value - var balance = value.split(' ')[0] - var label = value.split(' ')[1] + var balance = value.formatted.split(' ')[0] + var label = value.formatted.split(' ')[1] return ( - h('.flex-column', { - style: { - alignItems: 'flex-end', - lineHeight: '13px', - fontFamily: 'Montserrat Thin', - textRendering: 'geometricPrecision', - }, + h(Tooltip, { + title: value.balance, }, [ - h('div', balance), - h('div', { + h('.flex-column', { style: { - color: ' #AEAEAE', - fontSize: '12px', + alignItems: 'flex-end', + lineHeight: '13px', + fontFamily: 'Montserrat Light', + textRendering: 'geometricPrecision', }, - }, label), + }, [ + h('div', balance), + h('div', { + style: { + color: ' #AEAEAE', + fontSize: '12px', + }, + }, label), + ]), ]) ) } diff --git a/ui/app/util.js b/ui/app/util.js index db12a1282..d9afa1bba 100644 --- a/ui/app/util.js +++ b/ui/app/util.js @@ -99,22 +99,29 @@ function formatBalance (balance, decimalsToKeep) { var parsed = parseBalance(balance) var beforeDecimal = parsed[0] var afterDecimal = parsed[1] - var formatted = 'None' - if (decimalsToKeep === undefined) { - if (beforeDecimal === '0') { - if (afterDecimal !== '0') { - var sigFigs = afterDecimal.match(/^0*(.{2})/) // default: grabs 2 most significant digits - if (sigFigs) { afterDecimal = sigFigs[0] } - formatted = '0.' + afterDecimal + ' ETH' - } - } else { - formatted = beforeDecimal + '.' + afterDecimal.slice(0, 3) + ' ETH' + var formatted, formattedBalance + + if (beforeDecimal === '0') { + if (afterDecimal !== '0') { + var sigFigs = afterDecimal.match(/^0*(.{2})/) // default: grabs 2 most significant digits + if (sigFigs) { afterDecimal = sigFigs[0] } + formattedBalance = `0.${afterDecimal.slice(0, 6)}` } } else { - afterDecimal += Array(decimalsToKeep).join('0') - formatted = beforeDecimal + '.' + afterDecimal.slice(0, decimalsToKeep) + ' ETH' + formattedBalance = `${beforeDecimal}.${afterDecimal.slice(0, 2)}` + } + if (decimalsToKeep) { + formattedBalance = `${beforeDecimal}.${afterDecimal.slice(0, decimalsToKeep)}` + } + + formatted = `${formattedBalance} ETH` + + if (formattedBalance === '0.0' || formattedBalance === undefined) { + formatted = 'None' + formattedBalance = 'None' } - return formatted + + return {formattedBalance, balance: parsed.join('.'), formatted} } function dataSize (data) {