diff options
author | Steve Klebanoff <steve@0xproject.com> | 2018-12-15 09:33:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-15 09:33:56 +0800 |
commit | 737d1dc54d72872e24abce5a1dbe1b66d35fa21a (patch) | |
tree | 608b52c60fb24777274de78174e69b88a75362e0 /packages/instant/src/util/format.ts | |
parent | 6d45beccad44e86ddd692d0cf54c09c29c5d9daf (diff) | |
parent | 8c5ff663a9521b3dcefbfd1bc9ef3fa2f4cfe023 (diff) | |
download | dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.tar dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.tar.gz dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.tar.bz2 dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.tar.lz dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.tar.xz dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.tar.zst dexon-sol-tools-737d1dc54d72872e24abce5a1dbe1b66d35fa21a.zip |
Merge pull request #1424 from 0xProject/feature/instant/usd-eth-toggle
[instant] ETH/USD toggle
Diffstat (limited to 'packages/instant/src/util/format.ts')
-rw-r--r-- | packages/instant/src/util/format.ts | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts index e9c432b2f..4adb63e21 100644 --- a/packages/instant/src/util/format.ts +++ b/packages/instant/src/util/format.ts @@ -2,7 +2,7 @@ import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; -import { ETH_DECIMALS } from '../constants'; +import { BIG_NUMBER_ZERO, ETH_DECIMALS } from '../constants'; export const format = { ethBaseUnitAmount: ( @@ -20,24 +20,38 @@ export const format = { ethUnitAmount?: BigNumber, decimalPlaces: number = 4, defaultText: React.ReactNode = '0 ETH', + minUnitAmountToDisplay: BigNumber = new BigNumber('0.00001'), ): React.ReactNode => { if (_.isUndefined(ethUnitAmount)) { return defaultText; } - const roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces); - return `${roundedAmount} ETH`; + let roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces); + + if (roundedAmount.eq(BIG_NUMBER_ZERO) && ethUnitAmount.greaterThan(BIG_NUMBER_ZERO)) { + // Sometimes for small ETH amounts (i.e. 0.000045) the amount rounded to 4 decimalPlaces is 0 + // If that is the case, show to 1 significant digit + roundedAmount = new BigNumber(ethUnitAmount.toPrecision(1)); + } + + const displayAmount = + roundedAmount.greaterThan(BIG_NUMBER_ZERO) && roundedAmount.lessThan(minUnitAmountToDisplay) + ? `< ${minUnitAmountToDisplay.toString()}` + : roundedAmount.toString(); + + return `${displayAmount} ETH`; }, ethBaseUnitAmountInUsd: ( ethBaseUnitAmount?: BigNumber, ethUsdPrice?: BigNumber, decimalPlaces: number = 2, defaultText: React.ReactNode = '$0.00', + minUnitAmountToDisplay: BigNumber = new BigNumber('0.00001'), ): React.ReactNode => { if (_.isUndefined(ethBaseUnitAmount) || _.isUndefined(ethUsdPrice)) { return defaultText; } const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseUnitAmount, ETH_DECIMALS); - return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces); + return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces, minUnitAmountToDisplay); }, ethUnitAmountInUsd: ( ethUnitAmount?: BigNumber, @@ -48,7 +62,13 @@ export const format = { if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) { return defaultText; } - return `$${ethUnitAmount.mul(ethUsdPrice).toFixed(decimalPlaces)}`; + const rawUsdPrice = ethUnitAmount.mul(ethUsdPrice); + const roundedUsdPrice = rawUsdPrice.toFixed(decimalPlaces); + if (roundedUsdPrice === '0.00' && rawUsdPrice.gt(BIG_NUMBER_ZERO)) { + return '<$0.01'; + } else { + return `$${roundedUsdPrice}`; + } }, ethAddress: (address: string): string => { return `0x${address.slice(2, 7)}…${address.slice(-5)}`; |