-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecimals.ts
More file actions
35 lines (30 loc) · 989 Bytes
/
decimals.ts
File metadata and controls
35 lines (30 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { BN_ASSERT_DECIMALS_OP } from '@/enums'
import { assertDecimals } from './assertions'
export const toDecimals = (
val: bigint,
currentDecimals: number,
targetDecimals: number,
): bigint => {
return targetDecimals > currentDecimals
? toGreaterDecimals(val, currentDecimals, targetDecimals)
: toLessDecimals(val, currentDecimals, targetDecimals)
}
export const toGreaterDecimals = (
val: bigint,
currentDecimals: number,
targetDecimals: number,
): bigint => {
assertDecimals(currentDecimals, targetDecimals, BN_ASSERT_DECIMALS_OP.GREATER)
return val * 10n ** BigInt(targetDecimals - currentDecimals)
}
export const toLessDecimals = (
val: bigint,
currentDecimals: number,
targetDecimals: number,
): bigint => {
assertDecimals(currentDecimals, targetDecimals, BN_ASSERT_DECIMALS_OP.LESS)
return val / 10n ** BigInt(currentDecimals - targetDecimals)
}
export const getTens = (precision: number): bigint => {
return 10n ** BigInt(precision)
}