/**
 * This file contains functions that perform composite operations or operations that don't fit in anywhere else.
 * Most of these functions perform some form of formatting.
 *
 * Understanding how  handleSignificantDecimals works will be helpful in understanding many of these utilities.
  import BigNumber from 'bignumber.js' ; import { get , has } from 'lodash' ; import { convertAmountToNativeAmount , convertRawAmountToDecimalFormat } from './amount-conversion' ; import { lessThan } from './comparison' ; import { currencies } from '../currencies' ; import { handleSignificantDecimals } from './rounding-and-approximation' ; import { BigNumberish } from './types' ; /**
 * Checks if we support the specified native currency based on currency code.
 */ export const isSupportedCurrency = ( nativeCurrency ) => { return has ( currencies , `${ nativeCurrency }` ) ; } ; /**
 * A partial of the interface of base on-chain Asset in https://github.com/cardstack/cardwallet/blob/5b85603d477cd97a41c27ec7786f812afbfa7526/src/entities/tokens.ts#L1
 * Decimals represents the difference between the conventional amount displayed and the amount that the blockchain thinks of it in.
 * Eg. decimals would be 18 for DAI, CARD, and ETH, representing the relationship between ether and wei
 * Necessary for functions to accept arbitrary amounts of decimals as some assets like USDC do not use 18 decimals like many ETH assets do.
 */ interface Asset { decimals : number ; symbol ? : string ; } /**
 * Counts the number of decimals places in `value`
 * Returns null for Infinity or NaN
 */ export const countDecimalPlaces = ( value ) : number => new BigNumber ( value ) . dp ( ) ; see handleSignificantDecimals export const handleSignificantDecimalsWithThreshold = ( value , decimals , buffer = 3 , threshold = '0.0001' ) => { const result = handleSignificantDecimals ( value , decimals , buffer ) ; return lessThan ( result , threshold ) ? `< ${ threshold }` : result ; } ; /**
 * Returns a balance object with properties `balanceObject.amount` and `balanceObject.display`
 *
 * - `balanceObject.amount` is `amount * priceUnit`.
 * - `balanceObject.display` is a formatted version of `balanceObject.amount` based on the native currency specified.
 * `buffer` can be used to control the amount of decimal places shown.
 * For more details on how this property is generated see  convertAmountToNativeDisplay .
 *
  export const convertAmountAndPriceToNativeDisplay = ( amount , priceUnit , nativeCurrency , buffer ? ) : { amount : string ; display : string ; } => { const nativeBalanceRaw = convertAmountToNativeAmount ( amount , priceUnit ) ; const nativeDisplay = convertAmountToNativeDisplay ( nativeBalanceRaw , nativeCurrency , buffer ) ; return { amount : nativeBalanceRaw , display : nativeDisplay , } ; } ; see convertAmountAndPriceToNativeDisplay .
  export const convertRawAmountToNativeDisplay = ( rawAmount , assetDecimals , priceUnit , nativeCurrency , buffer ? ) => { const assetBalance = convertRawAmountToDecimalFormat ( rawAmount , assetDecimals ) ; return convertAmountAndPriceToNativeDisplay ( assetBalance , priceUnit , nativeCurrency , buffer ) ; } ; see convertAmountToBalanceDisplay export const convertRawAmountToBalance = ( value , asset , buffer ? ) => { const decimals = get ( asset , 'decimals' , 18 ) ; const assetBalance = convertRawAmountToDecimalFormat ( value , decimals ) ; return { amount : assetBalance , display : convertAmountToBalanceDisplay ( assetBalance , asset , buffer ) , } ; } ; /**
 * Passes `value`, `asset.decimals`, and `buffer` to `handleSignificantDecimals` and then tacks on `asset.symbol` at the end of the returned value
 */ export const convertAmountToBalanceDisplay = ( value , asset , buffer ? ) => { const decimals = get ( asset , 'decimals' , 18 ) ; const display = handleSignificantDecimals ( value , decimals , buffer ) ; return `${ display } ${ get ( asset , 'symbol' , '' ) }` ; } ; see handleSignificantDecimals export const convertAmountToPercentageDisplay = ( value , decimals = 2 , buffer ? ) : string => { const display = handleSignificantDecimals ( value , decimals , buffer ) ; return `${ display }%` ; } ; see handleSignificantDecimals for more details on calculating amount
  export const convertAmountToNativeDisplay = ( value , nativeCurrency , buffer ? ) => { const nativeSelected = get ( currencies , `${ nativeCurrency }` ) ; if ( ! nativeSelected ) { throw new Error ( `Unknown currency ${ nativeCurrency }` ) ; } const { decimals } = nativeSelected ; const display = handleSignificantDecimals ( value , decimals , buffer ) ; if ( nativeSelected . alignment === 'left' ) { return `${ nativeSelected . symbol }${ display } ${ nativeSelected . currency }` ; } return `${ display } ${ nativeSelected . symbol } ${ nativeSelected . currency }` ; } ; /**
 * Formats `amount` with specified amount of decimal places (and default BigNumber formatting)
 */ export const formatCurrencyAmount = ( amount , decimalPlaces = 2 ) => { return new BigNumber ( amount ) . toFormat ( decimalPlaces ) ; } ; /**
 * Returns a promise that resolves after `ms` milliseconds
 */ export const delay = ( ms ) : Promise < void > => { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } ;