Skip to content

Commit

Permalink
feat: unScale 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 7d6a75a commit 77d00bf
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"prettier.enable": false,
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll": false,
"source.fixAll.eslint": true
"source.fixAll": "never",
"source.fixAll.eslint": "explicit"
},
"cSpell.words": [
"newrepo",
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './formatter'
export * from './web3'
export * from './units/unscale'

export const simulateAsyncPause = (duration = 1000) =>
new Promise<void>((resolve) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class MongoClient {

async initialize(uri: string, dbName: string) {
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
this.mongoClient = new mongoDB.MongoClient(uri)
// connect to the MongoDB cluster
await this.mongoClient.connect()
Expand Down
36 changes: 36 additions & 0 deletions packages/utils/src/units/unscale.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test } from 'vitest'

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

test('converts value to number', () => {
expect(unScale(BigInt(69), 0)).toMatchInlineSnapshot('"69"')
expect(unScale(BigInt(69), 5)).toMatchInlineSnapshot('"0.00069"')
expect(unScale(BigInt(690), 1)).toMatchInlineSnapshot('"69"')
expect(unScale(BigInt(1300000), 5)).toMatchInlineSnapshot('"13"')
expect(unScale(BigInt('4200000000000'), 10)).toMatchInlineSnapshot('"420"')
expect(unScale(BigInt('20000000000'), 9)).toMatchInlineSnapshot('"20"')
expect(unScale(BigInt('40000000000000000000'), 18)).toMatchInlineSnapshot('"40"')
expect(unScale(BigInt('10000000000000'), 18)).toMatchInlineSnapshot('"0.00001"')
expect(unScale(BigInt(12345), 4)).toMatchInlineSnapshot('"1.2345"')
expect(unScale(BigInt(12345), 4)).toMatchInlineSnapshot('"1.2345"')
expect(unScale(BigInt('6942069420123456789123450000'), 18)).toMatchInlineSnapshot(
'"6942069420.12345678912345"',
)
expect(
unScale(BigInt('694212312312306942012345444446789123450000000000000000000000000000000'), 50),
).toMatchInlineSnapshot('"6942123123123069420.1234544444678912345"')
// negative values
expect(unScale(BigInt(-690), 1)).toMatchInlineSnapshot('"-69"')
expect(unScale(BigInt(-1300000), 5)).toMatchInlineSnapshot('"-13"')
expect(unScale(BigInt('-4200000000000'), 10)).toMatchInlineSnapshot('"-420"')
expect(unScale(BigInt('-20000000000'), 9)).toMatchInlineSnapshot('"-20"')
expect(unScale(BigInt('-40000000000000000000'), 18)).toMatchInlineSnapshot('"-40"')
expect(unScale(BigInt(-12345), 4)).toMatchInlineSnapshot('"-1.2345"')
expect(unScale(BigInt(-12345), 4)).toMatchInlineSnapshot('"-1.2345"')
expect(unScale(BigInt('-6942069420123456789123450000'), 18)).toMatchInlineSnapshot(
'"-6942069420.12345678912345"',
)
expect(
unScale(BigInt('-694212312312306942012345444446789123450000000000000000000000000000000'), 50),
).toMatchInlineSnapshot('"-6942123123123069420.1234544444678912345"')
})
22 changes: 22 additions & 0 deletions packages/utils/src/units/unscale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Divides a number by a given exponent of base 10 (10exponent), and un-scales it into a string representation of the number.
* @returns A string representation of the number
* @example
* unScale(112000000000000000000n, 18) // '112'
* unScale('112000000000000000000', 18) // '112'
*/
export function unScale(value: bigint | string, decimals: number) {
let display = value.toString()

const negative = display.startsWith('-')
if (negative)
display = display.slice(1)

display = display.padStart(decimals, '0')

const integer = display.slice(0, display.length - decimals)
let fraction = display.slice(display.length - decimals)
fraction = fraction.replace(/(0+)$/, '')

return `${negative ? '-' : ''}${integer || '0'}${fraction ? `.${fraction}` : ''}`
}
8 changes: 0 additions & 8 deletions packages/utils/src/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ export const scaleUserAmount = (amount: string | number, decimals = 6) => {
return scaleToString(shortenDecimals(amount, decimals), decimals)
}

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

/**
* UnScale the given base number to the given decimals (e.g. 1000000000000000000000012 with 18 decimal -> 1000000)
* @param amount The number to un scale
Expand Down
10 changes: 1 addition & 9 deletions packages/utils/test/web3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
decreaseNumByPercentage,
getFormattedAmount, getPercentOfAmount, getPercentageOfAmount, increaseNumByPercentage,
scale, scaleToString, scaleUserAmount,
unScale, unScaleToBase,
unScaleToBase,
} from '../src'

describe('increaseNumByPercentage', () => {
Expand Down Expand Up @@ -115,14 +115,6 @@ describe('scaleUserAmount', () => {
})
})

describe('unScale', () => {
it('unScale amount by given decimals and returns string', () => {
const num = '45366240'
const decimals = 6
expect(unScale(num, decimals)).toEqual('45.36624')
})
})

describe('unScaleToBase', () => {
it('unScale to base amount by given decimals and returns string with no decimals', () => {
const num = '45366240903847'
Expand Down

0 comments on commit 77d00bf

Please sign in to comment.