aboutsummaryrefslogblamecommitdiffstats
path: root/packages/instant/src/util/format.ts
blob: 3aaf9a3a7bbe73039d83405a57d97ea0c9ffe6a6 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                               

                            
                                                             

                       

                                      


                                               
                                               

                               
                                                                                        

                                                                  




                                               


                                           
                                                                                         



                                                                                                               
      

                                      

                                  

                                               
                                                                             

                               
                                                                                        





                                                                                    

                                               


                                                                         






                                                                            
      


                                                                 
  
import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';

import { BIG_NUMBER_ZERO, ETH_DECIMALS } from '../constants';

export const format = {
    ethBaseUnitAmount: (
        ethBaseUnitAmount?: BigNumber,
        decimalPlaces: number = 4,
        defaultText: React.ReactNode = '0 ETH',
    ): React.ReactNode => {
        if (_.isUndefined(ethBaseUnitAmount)) {
            return defaultText;
        }
        const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseUnitAmount, ETH_DECIMALS);
        return format.ethUnitAmount(ethUnitAmount, decimalPlaces);
    },
    ethUnitAmount: (
        ethUnitAmount?: BigNumber,
        decimalPlaces: number = 4,
        defaultText: React.ReactNode = '0 ETH',
    ): React.ReactNode => {
        if (_.isUndefined(ethUnitAmount)) {
            return defaultText;
        }
        const roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces);
        // 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
        const displayAmount = roundedAmount.eq(BIG_NUMBER_ZERO) ? ethUnitAmount.toPrecision(1) : roundedAmount;
        return `${displayAmount} ETH`;
    },
    ethBaseUnitAmountInUsd: (
        ethBaseUnitAmount?: BigNumber,
        ethUsdPrice?: BigNumber,
        decimalPlaces: number = 2,
        defaultText: React.ReactNode = '$0.00',
    ): React.ReactNode => {
        if (_.isUndefined(ethBaseUnitAmount) || _.isUndefined(ethUsdPrice)) {
            return defaultText;
        }
        const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseUnitAmount, ETH_DECIMALS);
        return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces);
    },
    ethUnitAmountInUsd: (
        ethUnitAmount?: BigNumber,
        ethUsdPrice?: BigNumber,
        decimalPlaces: number = 2,
        defaultText: React.ReactNode = '$0.00',
    ): React.ReactNode => {
        if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) {
            return defaultText;
        }
        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)}`;
    },
};