ethers#FixedNumber TypeScript Examples

The following examples show how to use ethers#FixedNumber. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From celo-web-wallet with MIT License 7 votes vote down vote up
export function calcSimpleExchangeRate(
  amountInWei: BigNumberish,
  buyBucket: string,
  sellBucket: string,
  spread: string,
  sellCelo: boolean
) {
  try {
    const fromAmountFN = FixedNumber.from(amountInWei)
    const simulate = fromAmountFN.isZero() || fromAmountFN.isNegative()
    // If no valid from amount provided, simulate rate with 1 unit
    const fromAmountAdjusted = simulate ? FixedNumber.from(WEI_PER_UNIT) : fromAmountFN

    const reducedSellAmt = fromAmountAdjusted.mulUnsafe(
      FixedNumber.from(1).subUnsafe(FixedNumber.from(spread))
    )
    const toAmountFN = reducedSellAmt
      .mulUnsafe(FixedNumber.from(buyBucket))
      .divUnsafe(reducedSellAmt.addUnsafe(FixedNumber.from(sellBucket)))

    const exchangeRateNum = toAmountFN.divUnsafe(fromAmountAdjusted).toUnsafeFloat()
    const exchangeRateWei = toWei(exchangeRateNum)
    const fromCeloRateWei = sellCelo
      ? exchangeRateWei
      : toWei(fromAmountAdjusted.divUnsafe(toAmountFN).toUnsafeFloat())

    // The FixedNumber interface isn't very friendly, need to strip out the decimal manually for BigNumber
    const toAmountWei = BigNumber.from(simulate ? 0 : toAmountFN.floor().toString().split('.')[0])

    return { exchangeRateNum, exchangeRateWei, fromCeloRateWei, toAmountWei }
  } catch (error) {
    logger.warn('Error computing exchange values')
    return { exchangeRateNum: 0, exchangeRateWei: '0', fromCeloRateWei: '0', toAmountWei: '0' }
  }
}
Example #2
Source File: amount.ts    From celo-web-wallet with MIT License 6 votes vote down vote up
// Similar to fromWei above but rounds to set number of decimals
// with a minimum floor, configured per token
export function fromWeiRounded(
  value: BigNumberish | null | undefined,
  decimals = STANDARD_TOKEN_DECIMALS,
  roundDownIfSmall = true
): string {
  if (!value) return '0'

  const minValue = FixedNumber.from(`${MIN_DISPLAY_VALUE}`) // FixedNumber throws error when given number for some reason
  const bareMinValue = FixedNumber.from(`${MIN_DISPLAY_VALUE / 5}`)

  const valueString = value.toString().trim()
  const amount = FixedNumber.from(utils.formatUnits(valueString, decimals))
  if (amount.isZero()) return '0'

  // If amount is less than min value
  if (amount.subUnsafe(minValue).isNegative()) {
    // If we should round and amount is really small
    if (roundDownIfSmall && amount.subUnsafe(bareMinValue).isNegative()) {
      return '0'
    }
    return minValue.toString()
  }

  return amount.round(DECIMALS_TO_DISPLAY).toString()
}
Example #3
Source File: formatBalance.test.ts    From glide-frontend with GNU General Public License v3.0 6 votes vote down vote up
describe('formatFixedNumber', () => {
  it.each([
    ['9763410526137450427.1196', 3, 18, '9.763'],
    ['9763410526137450427.1196', 1, 18, '9.7'],
    ['9763410526137450427.1196', 0, 18, '9.0'],
    ['567008695201201503619.22', 18, 18, '567.008695201201503619'],
    ['567008695201201503619.22', 5, 18, '567.00869'],
    ['97634105261.1196', 3, 9, '97.634'],
    ['97634105261', 1, 9, '97.6'],
    ['97634105261', 0, 9, '97.0'],
  ])('correctly formats %s (%d, %d) correctly to %s', (value, displayDecimals, decimals, expected) => {
    const ethersFn = FixedNumber.from(value)
    expect(formatFixedNumber(ethersFn, displayDecimals, decimals)).toBe(expected)
  })
})
Example #4
Source File: tradeQuoter.ts    From set.js with Apache License 2.0 5 votes vote down vote up
private normalizeTokenAmount(amount: BigNumber, decimals: number): number {
    const tokenScale = BigNumber.from(10).pow(decimals);
    return FixedNumber.from(amount).divUnsafe(FixedNumber.from(tokenScale)).toUnsafeFloat();
  }
Example #5
Source File: amount.ts    From celo-web-wallet with MIT License 5 votes vote down vote up
export function fromFixidity(value: BigNumberish | null | undefined): number {
  if (!value) return 0
  return FixedNumber.from(value)
    .divUnsafe(FixedNumber.from('1000000000000000000000000'))
    .toUnsafeFloat()
}