/**
 * This file contains functions that convert amounts to other amounts.
 * Note that some functions in this file may round their results.
 */ import BigNumber from 'bignumber.js' ; import { isNil } from 'lodash' ; import { multiply } from './arithmetic' ; import { isZero } from './comparison' ; import { roundAmountToNativeCurrencyDecimals } from './rounding-and-approximation' ; import { BigNumberish } from './types' ; see convertRawAmountToDecimalFormat export const convertAmountToRawAmount = ( value , decimals ) : string => new BigNumber ( value ) . times ( new BigNumber ( 10 ) . pow ( decimals ) ) . toFixed ( ) ; /**
 * Calculates `value / priceUnit`, rounded down to specified `decimals` (default 18).
 * Handles division by zero by returning zero
 */ export const convertAmountFromNativeValue = ( value , priceUnit , decimals = 18 ) : string => { if ( isNil ( priceUnit ) || isZero ( priceUnit ) ) return '0' ; return new BigNumber ( new BigNumber ( value ) . dividedBy ( priceUnit ) . toFixed ( decimals , BigNumber . ROUND_DOWN ) ) . toFixed ( ) ; } ; see multiply export const convertAmountToNativeAmount = ( amount , priceUnit ) : string => multiply ( amount , priceUnit ) ; /**
 * Returns `value / 100`, rounded to specified `decimals`
 */ export const convertBipsToPercentage = ( value , decimals = 2 ) : string => new BigNumber ( value ) . shiftedBy ( - 2 ) . toFixed ( decimals ) ; /**
 * Returns `value / 10**decimals`. See  BigNumber . dividedBy for detailed information on how division is handled.
  export const convertRawAmountToDecimalFormat = ( value , decimals = 18 ) : string => new BigNumber ( value ) . dividedBy ( new BigNumber ( 10 ) . pow ( decimals ) ) . toFixed ( ) ; /**
 * Converts wei to ether - Returns `number / 10**18`.
 */ export const fromWei = ( number ) : string => convertRawAmountToDecimalFormat ( number , 18 ) ; const SPEND_TO_USD_RATE = 0.01 ; /**
 * Converts SPEND to USD by multiplying `amountInSpend` by 0.01
 * Returns `0` if `amountInSpend` is an empty string,
 * and otherwise `undefined` if amountInSpend is not a number.
 *
 */ export const spendToUsd = ( amountInSpend ) : number | undefined => { if ( ( amountInSpend as unknown ) === '' ) { return 0 ; } if ( typeof ( amountInSpend as unknown ) !== 'number' ) { return undefined ; } return amountInSpend * SPEND_TO_USD_RATE ; } ; /**
 * Rounds a given amount up to the specified currency's decimals and then converts it to spend, rounding up to the nearest integer.
 * Internally uses  roundAmountToNativeCurrencyDecimals with rounding mode = up
 *
 *  param amount param currency param usdRate returns export const convertToSpend = ( amount , currency , usdRate ) => { return Math . ceil ( Number ( roundAmountToNativeCurrencyDecimals ( amount , currency ) ) / usdRate / SPEND_TO_USD_RATE ) ; } ;