-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { NB_DECIMALS } from '../basicElements/mas' | ||
|
||
/** | ||
* Divides a number by a given exponent of base 10 (10exponent), and formats it into a string representation of the number. | ||
* | ||
* This code snippet is taken from the Viem SDK. | ||
* - Docs: https://viem.sh/docs/utilities/formatUnits | ||
* | ||
* @example | ||
* | ||
* formatUnits(420000000000n, 9) | ||
* // '420' | ||
*/ | ||
export function formatUnits(value: bigint, decimals: number): string { | ||
let display = value.toString() | ||
|
||
const negative = display.startsWith('-') | ||
if (negative) display = display.slice(1) | ||
|
||
display = display.padStart(decimals, '0') | ||
|
||
// eslint-disable-next-line prefer-const | ||
let [integer, fraction] = [ | ||
display.slice(0, display.length - decimals), | ||
display.slice(display.length - decimals), | ||
] | ||
fraction = fraction.replace(/(0+)$/, '') | ||
return `${negative ? '-' : ''}${integer || '0'}${ | ||
fraction ? `.${fraction}` : '' | ||
}` | ||
} | ||
|
||
export function formatMas(value: bigint): string { | ||
return formatUnits(value, NB_DECIMALS) | ||
} | ||
|
||
/** | ||
* Multiplies a string representation of a number by a given exponent of base 10 (10exponent). | ||
* | ||
* This code snippet is taken from the Viem SDK. | ||
* - Docs: https://viem.sh/docs/utilities/parseUnits | ||
* | ||
* @example | ||
* import { parseUnits } from 'viem' | ||
* | ||
* parseUnits('420', 9) | ||
* // 420000000000n | ||
*/ | ||
export function parseUnits(value: string, decimals: number): bigint { | ||
let [integer, fraction = '0'] = value.split('.') | ||
|
||
const negative = integer.startsWith('-') | ||
if (negative) integer = integer.slice(1) | ||
|
||
// trim trailing zeros. | ||
fraction = fraction.replace(/(0+)$/, '') | ||
|
||
// round off if the fraction is larger than the number of decimals. | ||
if (decimals === 0) { | ||
if (Math.round(Number(`.${fraction}`)) === 1) | ||
integer = `${BigInt(integer) + 1n}` | ||
fraction = '' | ||
} else if (fraction.length > decimals) { | ||
const [left, unit, right] = [ | ||
fraction.slice(0, decimals - 1), | ||
fraction.slice(decimals - 1, decimals), | ||
fraction.slice(decimals), | ||
] | ||
|
||
const rounded = Math.round(Number(`${unit}.${right}`)) | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
if (rounded > 9) | ||
fraction = `${BigInt(left) + BigInt(1)}0`.padStart(left.length + 1, '0') | ||
else fraction = `${left}${rounded}` | ||
|
||
if (fraction.length > decimals) { | ||
fraction = fraction.slice(1) | ||
integer = `${BigInt(integer) + 1n}` | ||
} | ||
|
||
fraction = fraction.slice(0, decimals) | ||
} else { | ||
fraction = fraction.padEnd(decimals, '0') | ||
} | ||
|
||
return BigInt(`${negative ? '-' : ''}${integer}${fraction}`) | ||
} | ||
|
||
export function parseMas(value: string): bigint { | ||
return parseUnits(value, NB_DECIMALS) | ||
} |
867fab1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for experimental massa-web3
Test suite run success
129 tests passing in 13 suites.
Report generated by 🧪jest coverage report action from 867fab1