2022-11-30 23:44:23 +03:00
|
|
|
/**
|
|
|
|
* Most interactions with Ethereum requires integer values, which use
|
|
|
|
* the smallest magnitude unit.
|
|
|
|
*
|
|
|
|
* For example, imagine dealing with dollars and cents. Since dollars
|
|
|
|
* are divisible, non-integer values are possible, such as ``$10.77``.
|
|
|
|
* By using the smallest indivisible unit (i.e. cents), the value can
|
|
|
|
* be kept as the integer ``1077``.
|
|
|
|
*
|
|
|
|
* When receiving decimal input from the user (as a decimal string),
|
|
|
|
* the value should be converted to an integer and when showing a user
|
|
|
|
* a value, the integer value should be converted to a decimal string.
|
|
|
|
*
|
|
|
|
* This creates a clear distinction, between values to be used by code
|
|
|
|
* (integers) and values used for display logic to users (decimals).
|
|
|
|
*
|
|
|
|
* The native unit in Ethereum, //ether// is divisible to 18 decimal places,
|
|
|
|
* where each individual unit is called a //wei//.
|
|
|
|
*
|
2022-12-03 05:27:06 +03:00
|
|
|
* @_subsection api/utils:Unit Conversion [about-units]
|
2022-11-30 23:44:23 +03:00
|
|
|
*/
|
2022-11-09 10:57:02 +03:00
|
|
|
import { assertArgument } from "./errors.js";
|
2022-11-30 23:44:23 +03:00
|
|
|
import { FixedNumber } from "./fixednumber.js";
|
|
|
|
import { getNumber } from "./maths.js";
|
2022-09-05 23:57:11 +03:00
|
|
|
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, unit) {
|
2022-11-30 23:44:23 +03:00
|
|
|
let decimals = 18;
|
2022-09-05 23:57:11 +03:00
|
|
|
if (typeof (unit) === "string") {
|
|
|
|
const index = names.indexOf(unit);
|
2022-11-09 10:57:02 +03:00
|
|
|
assertArgument(index >= 0, "invalid unit", "unit", unit);
|
2022-11-30 23:44:23 +03:00
|
|
|
decimals = 3 * index;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2022-11-30 23:44:23 +03:00
|
|
|
else if (unit != null) {
|
|
|
|
decimals = getNumber(unit, "unit");
|
|
|
|
}
|
|
|
|
return FixedNumber.fromValue(value, decimals, { decimals }).toString();
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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, unit) {
|
2022-11-09 10:57:02 +03:00
|
|
|
assertArgument(typeof (value) === "string", "value must be a string", "value", value);
|
2022-11-30 23:44:23 +03:00
|
|
|
let decimals = 18;
|
2022-09-05 23:57:11 +03:00
|
|
|
if (typeof (unit) === "string") {
|
|
|
|
const index = names.indexOf(unit);
|
2022-11-09 10:57:02 +03:00
|
|
|
assertArgument(index >= 0, "invalid unit", "unit", unit);
|
2022-11-30 23:44:23 +03:00
|
|
|
decimals = 3 * index;
|
|
|
|
}
|
|
|
|
else if (unit != null) {
|
|
|
|
decimals = getNumber(unit, "unit");
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2022-11-30 23:44:23 +03:00
|
|
|
return FixedNumber.fromString(value, { decimals }).value;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts %%value%% into a //decimal string// using 18 decimal places.
|
|
|
|
*/
|
|
|
|
export function formatEther(wei) {
|
|
|
|
return formatUnits(wei, 18);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts the //decimal string// %%ether%% to a [[BigInt]], using 18
|
|
|
|
* decimal places.
|
|
|
|
*/
|
|
|
|
export function parseEther(ether) {
|
|
|
|
return parseUnits(ether, 18);
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=units.js.map
|