2019-05-15 01:48:48 +03:00
|
|
|
"use strict";
|
2019-08-25 23:20:34 +03:00
|
|
|
import { formatFixed, parseFixed } from "@ethersproject/bignumber";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
const names = [
|
2019-05-15 01:48:48 +03:00
|
|
|
"wei",
|
|
|
|
"kwei",
|
|
|
|
"mwei",
|
|
|
|
"gwei",
|
|
|
|
"szabo",
|
|
|
|
"finney",
|
|
|
|
"ether",
|
|
|
|
];
|
|
|
|
// Some environments have issues with RegEx that contain back-tracking, so we cannot
|
|
|
|
// use them.
|
2019-08-25 09:39:20 +03:00
|
|
|
export function commify(value) {
|
2019-11-20 12:57:38 +03:00
|
|
|
const comps = String(value).split(".");
|
2019-05-15 01:48:48 +03:00
|
|
|
if (comps.length > 2 || !comps[0].match(/^-?[0-9]*$/) || (comps[1] && !comps[1].match(/^[0-9]*$/)) || value === "." || value === "-.") {
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.throwArgumentError("invalid value", "value", value);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
// Make sure we have at least one whole digit (0 if none)
|
2019-08-25 09:39:20 +03:00
|
|
|
let whole = comps[0];
|
|
|
|
let negative = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
if (whole.substring(0, 1) === "-") {
|
|
|
|
negative = "-";
|
|
|
|
whole = whole.substring(1);
|
|
|
|
}
|
|
|
|
// Make sure we have at least 1 whole digit with no leading zeros
|
|
|
|
while (whole.substring(0, 1) === "0") {
|
|
|
|
whole = whole.substring(1);
|
|
|
|
}
|
|
|
|
if (whole === "") {
|
|
|
|
whole = "0";
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let suffix = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
if (comps.length === 2) {
|
|
|
|
suffix = "." + (comps[1] || "0");
|
|
|
|
}
|
2020-07-13 15:03:56 +03:00
|
|
|
while (suffix.length > 2 && suffix[suffix.length - 1] === "0") {
|
|
|
|
suffix = suffix.substring(0, suffix.length - 1);
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const formatted = [];
|
2019-05-15 01:48:48 +03:00
|
|
|
while (whole.length) {
|
|
|
|
if (whole.length <= 3) {
|
|
|
|
formatted.unshift(whole);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2019-11-20 12:57:38 +03:00
|
|
|
const index = whole.length - 3;
|
2019-05-15 01:48:48 +03:00
|
|
|
formatted.unshift(whole.substring(index));
|
|
|
|
whole = whole.substring(0, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return negative + formatted.join(",") + suffix;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export function formatUnits(value, unitName) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (unitName) === "string") {
|
2019-11-20 12:57:38 +03:00
|
|
|
const index = names.indexOf(unitName);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (index !== -1) {
|
|
|
|
unitName = 3 * index;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
return formatFixed(value, (unitName != null) ? unitName : 18);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export function parseUnits(value, unitName) {
|
2020-10-08 03:10:50 +03:00
|
|
|
if (typeof (value) !== "string") {
|
|
|
|
logger.throwArgumentError("value must be a string", "value", value);
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (unitName) === "string") {
|
2019-11-20 12:57:38 +03:00
|
|
|
const index = names.indexOf(unitName);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (index !== -1) {
|
|
|
|
unitName = 3 * index;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
return parseFixed(value, (unitName != null) ? unitName : 18);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export function formatEther(wei) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return formatUnits(wei, 18);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export function parseEther(ether) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return parseUnits(ether, 18);
|
|
|
|
}
|
2020-07-13 15:03:56 +03:00
|
|
|
//# sourceMappingURL=index.js.map
|