import { BigNumber } from "@ethersproject/bignumber" ; import { ethers , BigNumber as EthersBigNumber } from "ethers" ; import currency from "currency.js" ; import { getDefaultSignificantDecimalsFromAssetDecimals } from "./asset" ; const { formatUnits } = ethers . utils ; export const formatSignificantDecimals = ( num , significantDecimals = 6 ) => parseFloat ( parseFloat ( num ) . toFixed ( significantDecimals ) ) . toString ( ) ; export const formatAmount = ( n ) : string => { if ( n < 1e4 ) return `${ currency ( n , { separator : "," , symbol : "" } ) . format ( ) }` ; if ( n >= 1e4 && n < 1e6 ) return `${ parseFloat ( ( n / 1e3 ) . toFixed ( 2 ) ) }K` ; if ( n >= 1e6 && n < 1e9 ) return `${ parseFloat ( ( n / 1e6 ) . toFixed ( 2 ) ) }M` ; if ( n >= 1e9 && n < 1e12 ) return `${ parseFloat ( ( n / 1e9 ) . toFixed ( 2 ) ) }B` ; if ( n >= 1e12 ) return `${ parseFloat ( ( n / 1e12 ) . toFixed ( 2 ) ) }T` ; return "" ; } ; export const formatBigNumber = ( num , decimals = 18 , significantDecimals ? ) => { const _significantDecimals = significantDecimals || getDefaultSignificantDecimalsFromAssetDecimals ( decimals ) ; return parseFloat ( formatUnits ( num , decimals ) ) . toLocaleString ( undefined , { maximumFractionDigits : _significantDecimals , } ) ; } ; export const formatBigNumberAmount = ( num , decimals = 18 ) => { return formatAmount ( parseFloat ( formatUnits ( num , decimals ) ) ) ; } ; export const toFiat = ( etherVal ) => { const scaleFactor = ethers . BigNumber . from ( 10 ) . pow ( "6" ) ; const scaled = etherVal . div ( scaleFactor ) ; return scaled . toNumber ( ) / 10 ** 2 ; } ; export const toUSD = ( bn ) => Math . floor ( parseFloat ( ethers . utils . formatEther ( bn ) ) ) . toLocaleString ( ) ; export const toETH = ( bn , precision = 4 ) => parseFloat ( ethers . utils . formatEther ( bn ) ) . toFixed ( precision ) ; export const assetToFiat = ( num , assetPrice , assetDecimal = 18 , precision = 2 ) : string => ( num instanceof BigNumber || num instanceof EthersBigNumber ? parseFloat ( ethers . utils . formatUnits ( num , assetDecimal ) ) * assetPrice : num * assetPrice ) . toFixed ( precision ) ; export const assetToUSD = ( num , assetPrice , assetDecimal = 18 , precision = 2 ) : string => currency ( assetToFiat ( num , assetPrice , assetDecimal , precision ) ) . format ( ) ; export const ethToUSD = ( num , ethPrice , precision = 2 ) : string => assetToUSD ( num , ethPrice , 18 , precision ) ; export const formatOption = ( bn ) : number => parseFloat ( ethers . utils . formatUnits ( bn , 8 ) ) ; export const getWAD = ( decimals ) : BigNumber => ethers . utils . parseUnits ( "1" , decimals ) ; export const wmul = ( x , y , decimals ) => x . mul ( y ) . add ( getWAD ( decimals ) . div ( ethers . BigNumber . from ( "2" ) ) ) . div ( getWAD ( decimals ) ) ; export const annualizedPerformance = ( performance ) => ( performance * 0.9 + 1 ) ** 52 - 1 ; export const getRange = ( start , stop , step ) => { const a = [ start ] ; let b = start ; while ( b < stop ) { a . push ( ( b += step || 1 ) ) ; } return a ; } ; export const handleSmallNumber = ( n , decimals = 4 ) : number => { let parsedString = n . toFixed ( decimals ) ; if ( n < 1e-6 ) { parsedString = n . toPrecision ( 1 ) ; } else if ( n < 1e-4 ) { parsedString = n . toPrecision ( 2 ) ; } return parseFloat ( parsedString ) ; } ; export const isPracticallyZero = ( num , decimals , marginString ? ) => { const defaultSignificantDecimals = getDefaultSignificantDecimalsFromAssetDecimals ( decimals ) ; const _marginString = marginString || ( 1 / 10 ** defaultSignificantDecimals ) . toFixed ( defaultSignificantDecimals ) ; const margin = ethers . utils . parseUnits ( _marginString , decimals ) ; return num . lt ( margin ) ; } ; export const amountAfterSlippage = ( num , slippage , decimals = 18 ) => { if ( slippage >= 1.0 ) { throw new Error ( "Slippage cannot exceed 100%" ) ; } const discountValue = ethers . utils . parseUnits ( "1" , decimals ) . sub ( ethers . utils . parseUnits ( slippage . toFixed ( 3 ) , decimals ) ) ; return num . mul ( discountValue ) . div ( BigNumber . from ( 10 ) . pow ( decimals ) ) ; } ;