Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods for parsing and formatting Qi values #68

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src.ts/utils/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const names = [
*/
export function formatUnits(value: BigNumberish, unit?: string | Numeric): string {
let decimals = 18;
if (typeof(unit) === "string") {
if (typeof (unit) === "string") {
const index = names.indexOf(unit);
assertArgument(index >= 0, "invalid unit", "unit", unit);
decimals = 3 * index;
Expand All @@ -61,10 +61,10 @@ export function formatUnits(value: BigNumberish, unit?: string | Numeric): strin
* or the name of a unit (e.g. ``"gwei"`` for 9 decimal places).
*/
export function parseUnits(value: string, unit?: string | Numeric): bigint {
assertArgument(typeof(value) === "string", "value must be a string", "value", value);
assertArgument(typeof (value) === "string", "value must be a string", "value", value);

let decimals = 18;
if (typeof(unit) === "string") {
if (typeof (unit) === "string") {
const index = names.indexOf(unit);
assertArgument(index >= 0, "invalid unit", "unit", unit);
decimals = 3 * index;
Expand All @@ -82,10 +82,24 @@ export function formatEther(wei: BigNumberish): string {
return formatUnits(wei, 18);
}

/**
* Converts %%value%% into a //decimal string// using 3 decimal places.
*/
export function formatQi(value: BigNumberish): string {
return formatUnits(value, 3);
}

/**
* Converts the //decimal string// %%ether%% to a BigInt, using 18
* decimal places.
*/
export function parseEther(ether: string): bigint {
return parseUnits(ether, 18);
}

/**
* Converts %%value%% into a //decimal string// using 3 decimal places.
*/
export function parseQi(value: string): bigint {
return parseUnits(value, 3);
}
Loading