Skip to content

Commit

Permalink
feat: formatAmount with bigint and better property names
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmnthn committed Mar 21, 2024
1 parent 5e13ba9 commit 9412cc5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 61 deletions.
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './units/unscale'
export * from './units/scale'
export * from './units/bigUtils'
export * from './units/price'
export * from './units/formatter'

export const simulateAsyncPause = (duration = 1000) =>
new Promise<void>((resolve) => {
Expand Down
32 changes: 32 additions & 0 deletions packages/utils/src/units/formatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, test } from 'vitest'

import { formatAmount } from './formatter'

test('calculate unit price from units and total price', () => {
expect(formatAmount('10000000', 6)).toMatchObject({ base: BigInt(10000000), display: '10', formatted: '10' })
expect(formatAmount('1110000', 6, 0)).toMatchObject({ base: BigInt(1110000), display: '1', formatted: '1.11' })
expect(formatAmount('0', 6, 1)).toMatchObject({ base: BigInt(0), display: '0', formatted: '0' })
expect(formatAmount('', 6, 1)).toMatchObject({ base: BigInt(0), display: '0', formatted: '0' })
expect(formatAmount('1110000', 6))
.toMatchObject({ base: BigInt(1110000), display: '1.11', formatted: '1.11' })
expect(formatAmount('1110000', 6, 1))
.toMatchObject({ base: BigInt(1110000), display: '1.1', formatted: '1.11' })
expect(formatAmount('123456789', 8, 3))
.toMatchObject({ base: BigInt(123456789), display: '1.234', formatted: '1.23456789' })
expect(formatAmount('12345678900223', 6, 3))
.toMatchObject({ base: BigInt(12345678900223), display: '12,345,678.9', formatted: '12345678.900223' })

// large numbers
expect(formatAmount('1234567890123456789012345678901234567890', 30, 3))
.toMatchObject({
base: BigInt('1234567890123456789012345678901234567890'),
display: '1,234,567,890.123',
formatted: '1234567890.12345678901234567890123456789',
})
expect(formatAmount('1234567890123456789', 15, 2))
.toMatchObject({
base: BigInt('1234567890123456789'),
display: '1,234.56',
formatted: '1234.567890123456789',
})
})
22 changes: 22 additions & 0 deletions packages/utils/src/units/formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { unScale } from './unscale'

export interface FormattedAmount {
base: bigint
display: string
formatted: string
}

/**
* un-scales unit with give decimals and returns
* @returns base: original value, display: unScaled value with 0-3 decimals with comma, formatted: unScaled value with all decimals
* @example
* getFormattedAmount('12345678900223', 6, 3) // { base: 12345678900223n, display: '12,345,678.9', formatted: '12345678.900223' }
*/
export function formatAmount(value: bigint | string, decimals: number, displayDecimals: 0 | 1 | 2 | 3 = 3) {
const valueUnScaled = unScale(value, decimals)
return {
base: BigInt(value),
display: parseFloat(valueUnScaled.slice(0, valueUnScaled.indexOf('.') + displayDecimals + 1)).toLocaleString(),
formatted: valueUnScaled,
}
}
26 changes: 2 additions & 24 deletions packages/utils/src/web3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber as BN } from '@ethersproject/bignumber'
import { commify, formatUnits, parseUnits } from '@ethersproject/units'
import { removeExtraZeros, shortenDecimals } from './formatter'
import { formatUnits, parseUnits } from '@ethersproject/units'
import { shortenDecimals } from './formatter'

export const bnPow = (decimal = 6, base = 10): string => {
return BN.from(base).pow(decimal).toString()
Expand All @@ -10,28 +10,6 @@ export const bnToFloat = (num: BigNumberish, decimals = 6) => {
return parseFloat(formatUnits(num.toString(), decimals))
}

/**
* format value with give decimals and get original value with unScaled values with 3 decimals with comma and all decimals
* @param value - value to format
* @param decimals - number of decimals to format (default: 6) (optional)
* @param displayDecimals - number of decimals to display (default: 3) (optional)
* @returns formatted value as object with base, formatted and formattedFull values as string
*/
export const getFormattedAmount = (value: number | string, decimals = 6, displayDecimals = 3): AmountFormat => {
const valueFormatted = formatUnits(value, decimals)
return {
base: `${value}`,
formatted: shortenDecimals(commify(valueFormatted), displayDecimals, true),
formattedFull: removeExtraZeros(valueFormatted),
}
}

export interface AmountFormat {
base: string
formatted: string
formattedFull: string
}

export type BigNumberish = BN | bigint | string | number

/**
Expand Down
38 changes: 1 addition & 37 deletions packages/utils/test/web3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { formatUnits } from '@ethersproject/units'
import { describe, expect, it } from 'vitest'
import {
decreaseNumByPercentage,
getFormattedAmount, getPercentOfAmount, getPercentageOfAmount, increaseNumByPercentage,
getPercentOfAmount, getPercentageOfAmount, increaseNumByPercentage,
} from '../src'

describe('increaseNumByPercentage', () => {
Expand Down Expand Up @@ -80,39 +80,3 @@ describe('getPercentOfAmount', () => {
expect(formatUnits(result, 6)).toEqual('0.566074')
})
})

describe('getFormattedAmount', () => {
it('should format amount', () => {
const value = '1234567'
const decimals = 6
const out = getFormattedAmount(value, decimals)
const expected = { base: '1234567', formatted: '1.234', formattedFull: '1.234567' }
expect(out).toEqual(expected)
})

it('should format amount', () => {
const value = '1234567'
const decimals = 18
const out = getFormattedAmount(value, decimals)
const expected = { base: '1234567', formatted: '< 0.001', formattedFull: '0.000000000001234567' }
expect(out).toEqual(expected)
})
})

describe('calcAmountWithSlippage', () => {
it('should format amount', () => {
const value = '1234567'
const decimals = 6
const out = getFormattedAmount(value, decimals)
const expected = { base: '1234567', formatted: '1.234', formattedFull: '1.234567' }
expect(out).toEqual(expected)
})

it('should format amount', () => {
const value = '1234567'
const decimals = 18
const out = getFormattedAmount(value, decimals)
const expected = { base: '1234567', formatted: '< 0.001', formattedFull: '0.000000000001234567' }
expect(out).toEqual(expected)
})
})

0 comments on commit 9412cc5

Please sign in to comment.