2022-09-05 23:14:43 +03:00
|
|
|
import { formatFixed, parseFixed } from "./fixednumber.js";
|
2022-09-09 06:21:08 +03:00
|
|
|
import { throwArgumentError } from "./errors.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
import type { BigNumberish, Numeric } from "../utils/index.js";
|
|
|
|
|
|
|
|
|
|
|
|
const names = [
|
|
|
|
"wei",
|
|
|
|
"kwei",
|
|
|
|
"mwei",
|
|
|
|
"gwei",
|
|
|
|
"szabo",
|
|
|
|
"finney",
|
|
|
|
"ether",
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts %%value%% into a //decimal string//, assuming %%unit%% decimal
|
|
|
|
* places. The %%unit%% may be the number of decimal places or the name of
|
|
|
|
* a unit (e.g. ``"gwei"`` for 9 decimal places).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export function formatUnits(value: BigNumberish, unit?: string | Numeric): string {
|
|
|
|
if (typeof(unit) === "string") {
|
|
|
|
const index = names.indexOf(unit);
|
2022-09-09 06:21:08 +03:00
|
|
|
if (index === -1) { throwArgumentError("invalid unit", "unit", unit); }
|
2022-09-05 23:14:43 +03:00
|
|
|
unit = 3 * index;
|
|
|
|
}
|
|
|
|
return formatFixed(value, (unit != null) ? unit: 18);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the //decimal string// %%value%% to a [[BigInt]], assuming
|
|
|
|
* %%unit%% decimal places. The %%unit%% may the number of decimal places
|
|
|
|
* or the name of a unit (e.g. ``"gwei"`` for 9 decimal places).
|
|
|
|
*/
|
|
|
|
export function parseUnits(value: string, unit?: string | Numeric): bigint {
|
|
|
|
if (typeof(value) !== "string") {
|
2022-09-09 06:21:08 +03:00
|
|
|
throwArgumentError("value must be a string", "value", value);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(unit) === "string") {
|
|
|
|
const index = names.indexOf(unit);
|
2022-09-09 06:21:08 +03:00
|
|
|
if (index === -1) { throwArgumentError("invalid unit", "unit", unit); }
|
2022-09-05 23:14:43 +03:00
|
|
|
unit = 3 * index;
|
|
|
|
}
|
|
|
|
return parseFixed(value, (unit != null) ? unit: 18);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts %%value%% into a //decimal string// using 18 decimal places.
|
|
|
|
*/
|
|
|
|
export function formatEther(wei: BigNumberish): string {
|
|
|
|
return formatUnits(wei, 18);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the //decimal string// %%ether%% to a [[BigInt]], using 18
|
|
|
|
* decimal places.
|
|
|
|
*/
|
|
|
|
export function parseEther(ether: string): bigint {
|
|
|
|
return parseUnits(ether, 18);
|
|
|
|
}
|