import { NetworkDefinition } from "@anthem/utils" ; import BigNumber from "bignumber.js" ; import { trimZeroes } from "./client-utils" ; import { divide , GenericNumberType , isGreaterThanOrEqualTo , multiply , subtract , toBigNumber , valueToBigNumber , } from "./math-utils" ; /**
 * Get a token formatter for the number.
 */ const getTokenFormatter = ( maximumFractionDigits = 2 ) => { return new Intl . NumberFormat ( "en-US" , { style : "decimal" , maximumFractionDigits , minimumFractionDigits : maximumFractionDigits , } ) ; } ; /**
 * Convert and format a Cosmos coin denomination into a human readable
 * format.
 */ export const formatCurrencyAmount = ( amount , maximumFractionDigits ? ) : string => { const value = valueToBigNumber ( amount ) ; if ( value . isZero ( ) ) { return "0" ; } else if ( value . isLessThanOrEqualTo ( 0.001 ) ) { const tokenFormatter = getTokenFormatter ( 6 ) ; return tokenFormatter . format ( Number ( amount ) ) ; } else if ( isGreaterThanOrEqualTo ( value , 1 ) || maximumFractionDigits !== undefined ) { const tokenFormatter = getTokenFormatter ( maximumFractionDigits ) ; return tokenFormatter . format ( Number ( amount ) ) ; } else { const result = value . toFormat ( 10 ) ; return trimZeroes ( result ) ; } } ; interface CurrencyConversionMethodTypes { ( amount , denomSize , targetConstructorFn ? ) : string ; ( amount , denomSize , targetConstructorFn ) : number ; ( amount , denomSize , targetConstructorFn ) : BigNumber ; } /**
 * Convert a denomination to the normal network display amount.
 */ export const denomToUnit = ( denoms , networkDenominationSize , targetConstructorFn = String ) => { const amount = valueToBigNumber ( denoms ) ; const result = divide ( amount , networkDenominationSize ) ; return targetConstructorFn ( result ) ; } ; /**
 * Convert a network display amount back to the network denomination amount.
 */ export const unitToDenom = ( amount , networkDenominationSize , targetConstructorFn = String ) => { const value = valueToBigNumber ( amount ) ; const result = multiply ( value , networkDenominationSize ) ; return targetConstructorFn ( result ) ; } ; /**
 * Convert a network currency amount to fiat price given the network
 * exchange rate.
 */ export const convertCryptoToFiat = ( price , denom , network ) : string => { const amount = valueToBigNumber ( denom ) ; if ( price ) { const atoms = denomToUnit ( amount , network . denominationSize ) ; const fiat = multiply ( atoms , price ) ; return fiat ; } return "" ; } ; /**
 * Determine the maximum possible transaction value after fees.
 */ export const calculateTransactionAmount = ( availableInput , gasPriceInput , gasAmountInput , network ) : string => { const [ available , gasPrice , gasAmount ] = [ availableInput , gasPriceInput , gasAmountInput , ] . map ( valueToBigNumber ) ; const fee = multiply ( gasPrice , gasAmount ) ; const denoms = unitToDenom ( available , network . denominationSize , toBigNumber ) ; const availableAmountDenom = subtract ( denoms , fee ) ; const availableAmount = denomToUnit ( availableAmountDenom , network . denominationSize ) ; return availableAmount ; } ; interface RenderCurrencyArgs { value : string ; denomSize : number ; fiatPrice : number ; convertToFiat ? : boolean ; } export const renderCurrencyValue = ( args ) => { const { value , denomSize , fiatPrice , convertToFiat } = args ; let result = denomToUnit ( value , denomSize , toBigNumber ) ; if ( convertToFiat ) { result = multiply ( fiatPrice , result , toBigNumber ) ; } return result . toFixed ( ) ; } ;