import { BigNumber , BigNumberish } from '@ethersproject/bignumber' ; import { formatUnits , parseUnits } from '@ethersproject/units' ; import { WAD_DECIMALS } from 'constants/numbers' ; type FormatConfig = { empty ? : string ; thousandsSeparator ? : string ; decimalSeparator ? : string ; precision ? : number ; padEnd ? : boolean ; decimals ? : number ; } ; const decimalSeparator = '.' ; const thousandsSeparator = ',' ; export const parseWad = ( amt ? ) => parseUnits ( amt ?. toString ( ) || '0' , WAD_DECIMALS ) ; export const fromWad = ( amt ? , decimals = WAD_DECIMALS ) => { const result = formatUnits ( amt ?? '0' , decimals ) ; return result . substring ( result . length - 2 ) === '.0' ? result . substring ( 0 , result . length - 2 ) : result ; } ; export const formatWad = ( amt ? , config ? ) => { if ( amt === undefined && amt === null && amt === '' ) return ; let _amt = amt ; if ( _amt ?. toString ( ) . includes ( '.' ) ) { _amt = _amt . toString ( ) . split ( '.' ) [ 0 ] ; } return formattedNum ( fromWad ( _amt , config ?. decimals ) , config ) ; } ; export const stripCommas = ( string ) => { return string . replace ( /,/g , '' ) ; } ; export const parsePermyriad = ( amt ? ) => BigNumber . from ( amt ? Math . floor ( parseFloat ( amt . toString ( ) ) * 100 ) : 0 ) ; export const fromPermyriad = ( amt ? ) => amt ? ( BigNumber . from ( amt ) . toNumber ( ) / 100 ) . toString ( ) : '0' ; export const parsePermille = ( amt ? ) => BigNumber . from ( amt ? Math . floor ( parseFloat ( amt . toString ( ) ) * 10 ) : 0 ) ; export const fromPermille = ( amt ? ) => amt ? ( BigNumber . from ( amt ) . toNumber ( ) / 10 ) . toString ( ) : '0' ; export const parsePerbicent = ( amt ? ) => BigNumber . from ( amt ? Math . floor ( parseFloat ( amt . toString ( ) ) * 2 ) : 0 ) ; export const fromPerbicent = ( amt ? ) => amt ? ( BigNumber . from ( amt ) . toNumber ( ) / 2 ) . toString ( ) : '0' ; export const fracDiv = ( quotient , dividend ) => { return parseFloat ( quotient ) / parseFloat ( dividend ) ; } ; const separateThousands = ( str ? , separator = thousandsSeparator ) => { if ( ! str ?. trim ( ) . length ) return ; return str . replace ( /\B(?=(\d{3})+(?!\d))/g , separator ) ; } ; export const formattedNum = ( num , config ? ) => { const _empty = config ?. empty ?? '0' ; if ( num === undefined || num === '' ) return _empty ; const _thousandsSeparator = config ?. thousandsSeparator ?? thousandsSeparator ; const _decimalSeparator = config ?. decimalSeparator ?? decimalSeparator ; let str = num . toString ( ) ; while ( str . length && str [ 0 ] === '0' ) { str = str . substring ( 1 ) ; } if ( ! str . length ) return _empty ; function formatNearZero ( formatted ) { if ( num ?. toString ( ) . trim ( ) && formatted ?. split ( '' ) . filter ( char => char !== _decimalSeparator && char !== _thousandsSeparator ) . every ( char => char === '0' ) ) { return '~' + formatted ; } return formatted ; } if ( str . includes ( _decimalSeparator ) ) { const [ pre , post ] = str . split ( _decimalSeparator ) ; const formattedPre = separateThousands ( pre , _thousandsSeparator ) || '0' ; if ( post === '0' ) return formatNearZero ( pre ) ; const formattedPost = post . substring ( 0 , config ?. precision ?? WAD_DECIMALS ) . padEnd ( config ?. padEnd ? config ?. precision ?? 0 : 0 , '0' ) ; if ( ! formattedPost || config ?. precision === 0 ) { return formatNearZero ( formattedPre ) ; } return formatNearZero ( [ formattedPre , formattedPost ] . join ( _decimalSeparator ) ) ; } const formatted = separateThousands ( str , _thousandsSeparator ) ; return formatted ; } ; export const toUint256 = ( num ) => '0x' + ( num ?. toHexString ( ) . split ( 'x' ) [ 1 ] ?? '' ) . padStart ( 64 , '0' ) ; export const formatPercent = ( numerator , divisor ) : string => { if ( ! divisor ?. gt ( 0 ) || ! numerator ) return '' ; const sharePct = numerator ?. mul ( 10000 ) . div ( divisor ) ; if ( sharePct ?. toString ( ) === '0' && numerator ?. gt ( 0 ) ) { return '<0.01' ; } return ( sharePct ?. toNumber ( ) / 100 ) . toString ( ) ; } ;