synapsecns/sanguine

View on GitHub
packages/rest-api/src/utils/formatBNToString.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { BigNumber } from '@ethersproject/bignumber'
import { formatUnits } from '@ethersproject/units'

export const formatBNToString = (
  bn: BigNumber,
  nativePrecision: number,
  decimalPlaces = 18
) => {
  const fullPrecision = formatUnits(bn, nativePrecision)
  const decimalIdx = fullPrecision.indexOf('.')

  if (decimalPlaces === undefined || decimalIdx === -1) {
    return fullPrecision
  } else {
    const rawNumber = Number(fullPrecision)

    if (rawNumber === 0) {
      return rawNumber.toFixed(1)
    }
    return rawNumber.toString()
  }
}