Skip to content

Commit

Permalink
feat: unScaleToBase unit with native bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmnthn committed Mar 20, 2024
1 parent e997ace commit b98f27a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
9 changes: 8 additions & 1 deletion packages/utils/src/units/unscale.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'

import { unScale } from './unscale.js'
import { unScale, unScaleToBase } from './unscale'

test('converts value to number', () => {
expect(unScale(BigInt(69), 0)).toMatchInlineSnapshot('"69"')
Expand Down Expand Up @@ -34,3 +34,10 @@ test('converts value to number', () => {
unScale(BigInt('-694212312312306942012345444446789123450000000000000000000000000000000'), 50),
).toMatchInlineSnapshot('"-6942123123123069420.1234544444678912345"')
})

test('converts value to base', () => {
expect(unScaleToBase(BigInt('69000300300000000000'), 18, 6)).toMatchInlineSnapshot('69000300n')
expect(unScaleToBase(BigInt('-69000300300000000000'), 18, 6)).toMatchInlineSnapshot('-69000300n')
expect(unScaleToBase(BigInt(69), 0, 0)).toMatchInlineSnapshot('69n')
expect(unScaleToBase(BigInt(-69), 0, 0)).toMatchInlineSnapshot('-69n')
})
12 changes: 12 additions & 0 deletions packages/utils/src/units/unscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ export function unScale(value: bigint | string, decimals: number) {

return `${negative ? '-' : ''}${integer || '0'}${fraction ? `.${fraction}` : ''}`
}

/**
* unScale the given base number to base(bigint) unscale to the given decimals and drop the remaining decimals
* @example
* unScaleToBase(69000000000000000000n, 12) // 69000000n
* unScaleToBase(69n, 0) // 69n
*/
export function unScaleToBase(value: bigint | string, decimals: number, newDecimals: number) {
if (decimals < newDecimals)
throw new Error('cannot unscale to more decimals')
return BigInt(unScale(value, decimals - newDecimals).replace(/\..+$/, ''))
}
8 changes: 0 additions & 8 deletions packages/utils/src/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ const scale = (amount: string | number | bigint, decimals: BigNumberish = 6): BN
return parseUnits(amount.toString(), decimals)
}

/**
* UnScale the given base number to the given decimals (e.g. 1000000000000000000000012 with 18 decimal -> 1000000)
* @param amount The number to un scale
* @param decimals The number of decimals to un scale to
* @returns The unscaled base number in string (e.g. 1000000000000000000000012 with 18 decimal -> 1000000)
*/
export const unScaleToBase = (amount: BigNumberish, decimals: BigNumberish = 6): string => shortenDecimals(formatUnits(amount, decimals), 0)

/**
* calculate total price from units using price per unit (e.g. units = 2e6, unitPrice = 1e5, unitDecimals = 6, priceDecimals = 6 => 0.2)
* @param units - The units to convert e.g. 2e6
Expand Down
10 changes: 0 additions & 10 deletions packages/utils/test/web3.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { BigNumber as BN } from '@ethersproject/bignumber'
import { formatUnits } from '@ethersproject/units'
import { describe, expect, it } from 'vitest'
import {
calcTotalPrice, calcUnitPrice, calcUnits,
decreaseNumByPercentage,
getFormattedAmount, getPercentOfAmount, getPercentageOfAmount, increaseNumByPercentage,
unScaleToBase,
} from '../src'

describe('increaseNumByPercentage', () => {
Expand Down Expand Up @@ -84,14 +82,6 @@ describe('getPercentOfAmount', () => {
})
})

describe('unScaleToBase', () => {
it('unScale to base amount by given decimals and returns string with no decimals', () => {
const num = '45366240903847'
const decimals = 6
expect(unScaleToBase(num, decimals)).toEqual('45366240')
})
})

describe('calcTotalPrice', () => {
it('1. calculate total price for units using price per unit', () => {
const units = 214
Expand Down

0 comments on commit b98f27a

Please sign in to comment.