Skip to content

Commit

Permalink
Add helper methods for parsing and formatting Qi values
Browse files Browse the repository at this point in the history
  • Loading branch information
rileystephens28 committed Apr 5, 2024
1 parent 6966db7 commit a369804
Showing 1 changed file with 17 additions and 3 deletions.
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);
}

0 comments on commit a369804

Please sign in to comment.