From 702b7568820d7a695f191df6bf44c76b37fdc7d8 Mon Sep 17 00:00:00 2001 From: Alexander Tseung Date: Fri, 31 Aug 2018 12:14:06 -0700 Subject: Allow denominations in CurrencyDisplay component --- ui/app/helpers/confirm-transaction/util.js | 2 ++ ui/app/helpers/conversions.util.js | 38 ++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) (limited to 'ui/app/helpers') diff --git a/ui/app/helpers/confirm-transaction/util.js b/ui/app/helpers/confirm-transaction/util.js index d1a4994e4..bcac22500 100644 --- a/ui/app/helpers/confirm-transaction/util.js +++ b/ui/app/helpers/confirm-transaction/util.js @@ -58,6 +58,7 @@ export function getValueFromWeiHex ({ toCurrency, conversionRate, numberOfDecimals, + toDenomination, }) { return conversionUtil(value, { fromNumericBase: 'hex', @@ -66,6 +67,7 @@ export function getValueFromWeiHex ({ toCurrency, numberOfDecimals, fromDenomination: 'WEI', + toDenomination, conversionRate, }) } diff --git a/ui/app/helpers/conversions.util.js b/ui/app/helpers/conversions.util.js index 1dec216fa..74802c86b 100644 --- a/ui/app/helpers/conversions.util.js +++ b/ui/app/helpers/conversions.util.js @@ -1,4 +1,5 @@ import { conversionUtil } from '../conversion-util' +import { ETH, GWEI, WEI } from '../constants/common' export function hexToDecimal (hexValue) { return conversionUtil(hexValue, { @@ -7,16 +8,27 @@ export function hexToDecimal (hexValue) { }) } -export function getEthFromWeiHex ({ - value, - conversionRate, -}) { - return getValueFromWeiHex({ - value, - conversionRate, - toCurrency: 'ETH', - numberOfDecimals: 6, - }) +export function getEthConversionFromWeiHex ({ value, conversionRate, numberOfDecimals = 6 }) { + const denominations = [ETH, GWEI] + + let nonZeroDenomination + + for (let i = 0; i < denominations.length; i++) { + const convertedValue = getValueFromWeiHex({ + value, + conversionRate, + toCurrency: ETH, + numberOfDecimals, + toDenomination: denominations[i], + }) + + if (convertedValue !== '0' || i === denominations.length - 1) { + nonZeroDenomination = `${convertedValue} ${denominations[i]}` + break + } + } + + return nonZeroDenomination } export function getValueFromWeiHex ({ @@ -24,14 +36,16 @@ export function getValueFromWeiHex ({ toCurrency, conversionRate, numberOfDecimals, + toDenomination, }) { return conversionUtil(value, { fromNumericBase: 'hex', toNumericBase: 'dec', - fromCurrency: 'ETH', + fromCurrency: ETH, toCurrency, numberOfDecimals, - fromDenomination: 'WEI', + fromDenomination: WEI, + toDenomination, conversionRate, }) } -- cgit v1.2.3