176 lines
5.4 KiB
JavaScript
176 lines
5.4 KiB
JavaScript
// src/utils/number.util.ts
|
|
import Web3 from "web3";
|
|
import { BN } from "ethereumjs-util";
|
|
function renderFromTokenMinimalUnit(tokenValue, decimals, decimalsToShow = 5) {
|
|
const minimalUnit = fromTokenMinimalUnit(tokenValue || 0, decimals);
|
|
const minimalUnitNumber = parseFloat(minimalUnit);
|
|
let renderMinimalUnit;
|
|
if (minimalUnitNumber < 1e-5 && minimalUnitNumber > 0) {
|
|
renderMinimalUnit = "< 0.00001";
|
|
} else {
|
|
const base = Math.pow(10, decimalsToShow);
|
|
renderMinimalUnit = (Math.round(minimalUnitNumber * base) / base).toString();
|
|
}
|
|
return renderMinimalUnit;
|
|
}
|
|
function fromTokenMinimalUnit(minimalInput, decimals) {
|
|
minimalInput = addHexPrefix(Number(minimalInput).toString(16));
|
|
let minimal = safeNumberToBN(minimalInput);
|
|
const negative = minimal.lt(new BN(0));
|
|
const base = Web3.utils.toBN(Math.pow(10, decimals).toString());
|
|
if (negative) {
|
|
minimal = minimal.mul(new BN(-1));
|
|
}
|
|
let fraction = minimal.mod(base).toString(10);
|
|
while (fraction.length < decimals) {
|
|
fraction = "0" + fraction;
|
|
}
|
|
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
|
const whole = minimal.div(base).toString(10);
|
|
let value = "" + whole + (fraction === "0" ? "" : "." + fraction);
|
|
if (negative) {
|
|
value = "-" + value;
|
|
}
|
|
return value;
|
|
}
|
|
function renderFromWei(value, decimalsToShow = 5) {
|
|
let renderWei = "0";
|
|
if (value) {
|
|
const wei = Web3.utils.fromWei(value);
|
|
const weiNumber = parseFloat(wei);
|
|
if (weiNumber < 1e-5 && weiNumber > 0) {
|
|
renderWei = "< 0.00001";
|
|
} else {
|
|
const base = Math.pow(10, decimalsToShow);
|
|
renderWei = (Math.round(weiNumber * base) / base).toString();
|
|
}
|
|
}
|
|
return renderWei;
|
|
}
|
|
function calcTokenValueToSend(value, decimals) {
|
|
return value ? (value * Math.pow(10, decimals)).toString(16) : 0;
|
|
}
|
|
function isDecimal(value) {
|
|
return Number.isFinite(parseFloat(value)) && !Number.isNaN(parseFloat(value)) && !isNaN(+value);
|
|
}
|
|
function toBN(value) {
|
|
return Web3.utils.toBN(value);
|
|
}
|
|
var addHexPrefix = (str) => {
|
|
if (typeof str !== "string" || str.match(/^-?0x/u)) {
|
|
return str;
|
|
}
|
|
if (str.match(/^-?0X/u)) {
|
|
return str.replace("0X", "0x");
|
|
}
|
|
if (str.startsWith("-")) {
|
|
return str.replace("-", "-0x");
|
|
}
|
|
return `0x${str}`;
|
|
};
|
|
function safeNumberToBN(value) {
|
|
const safeValue = fastSplit(value.toString()) || "0";
|
|
return numberToBN(safeValue);
|
|
}
|
|
function fastSplit(value, divider = ".") {
|
|
value += "";
|
|
const [from, to] = [value.indexOf(divider), 0];
|
|
return value.substring(from, to) || value;
|
|
}
|
|
function stripHexPrefix(str) {
|
|
if (typeof str !== "string") {
|
|
return str;
|
|
}
|
|
return str.slice(0, 2) === "0x" ? str.slice(2) : str;
|
|
}
|
|
function numberToBN(arg) {
|
|
if (typeof arg === "string" || typeof arg === "number") {
|
|
var multiplier = Web3.utils.toBN(1);
|
|
var formattedString = String(arg).toLowerCase().trim();
|
|
var isHexPrefixed = formattedString.substr(0, 2) === "0x" || formattedString.substr(0, 3) === "-0x";
|
|
var stringArg = stripHexPrefix(formattedString);
|
|
if (stringArg.substr(0, 1) === "-") {
|
|
stringArg = stripHexPrefix(stringArg.slice(1));
|
|
multiplier = Web3.utils.toBN(-1);
|
|
}
|
|
stringArg = stringArg === "" ? "0" : stringArg;
|
|
if (!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/) || stringArg.match(/^[a-fA-F]+$/) || isHexPrefixed === true && stringArg.match(/^[0-9A-Fa-f]+$/)) {
|
|
return Web3.utils.toBN(stringArg).mul(multiplier);
|
|
}
|
|
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === "") && isHexPrefixed === false) {
|
|
return Web3.utils.toBN(stringArg).mul(multiplier);
|
|
}
|
|
} else if (typeof arg === "object" && arg.toString && !arg.pop && !arg.push) {
|
|
if (arg.toString(10).match(/^-?[0-9]+$/) && (arg.mul || arg.dividedToIntegerBy)) {
|
|
return Web3.utils.toBN(arg.toString(10));
|
|
}
|
|
}
|
|
throw new Error(
|
|
"[number-to-bn] while converting number " + JSON.stringify(arg) + " to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported."
|
|
);
|
|
}
|
|
function checkRadixLegal(radix) {
|
|
return radix >= 2 && radix <= 62;
|
|
}
|
|
function transformCharToNum(letter, base) {
|
|
if (base <= 36) {
|
|
letter = letter.toLowerCase();
|
|
}
|
|
if (letter >= "0" && letter <= "9") {
|
|
return parseInt(letter);
|
|
}
|
|
if (letter >= "a" && letter <= "z") {
|
|
return letter.charCodeAt(0) - "a".charCodeAt(0) + 10;
|
|
}
|
|
if (letter >= "A" && letter <= "Z") {
|
|
return letter.charCodeAt(0) - "A".charCodeAt(0) + 36;
|
|
}
|
|
return 0;
|
|
}
|
|
function transformNumToChar(num, alphabet) {
|
|
alphabet = alphabet || "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
return alphabet.charAt(num);
|
|
}
|
|
function convert({
|
|
numStr,
|
|
base,
|
|
to,
|
|
alphabet
|
|
}) {
|
|
if (base === to || !checkRadixLegal(base) || !checkRadixLegal(to)) {
|
|
return numStr;
|
|
}
|
|
let p = 0;
|
|
let number10 = 0;
|
|
while (p < numStr.length) {
|
|
number10 *= base;
|
|
number10 += transformCharToNum(numStr.charAt(p), base);
|
|
p++;
|
|
}
|
|
if (to === 10) {
|
|
return number10.toString();
|
|
}
|
|
let result = "";
|
|
let cur;
|
|
while (number10) {
|
|
cur = number10 % to;
|
|
result = transformNumToChar(cur, alphabet) + result;
|
|
number10 = Math.floor(number10 / to);
|
|
}
|
|
return result;
|
|
}
|
|
export {
|
|
addHexPrefix,
|
|
calcTokenValueToSend,
|
|
convert,
|
|
fastSplit,
|
|
fromTokenMinimalUnit,
|
|
isDecimal,
|
|
numberToBN,
|
|
renderFromTokenMinimalUnit,
|
|
renderFromWei,
|
|
safeNumberToBN,
|
|
stripHexPrefix,
|
|
toBN
|
|
};
|
|
//# sourceMappingURL=number.util.js.map
|