131 lines
4.7 KiB
JavaScript
131 lines
4.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.baseDecode = exports.baseEncode = exports.parseNearAmount = exports.formatNearAmount = exports.NEAR_NOMINATION = exports.NEAR_NOMINATION_EXP = void 0;
|
|
const base_1 = require("@scure/base");
|
|
/**
|
|
* Exponent for calculating how many indivisible units are there in one NEAR. See {@link NEAR_NOMINATION}.
|
|
*/
|
|
exports.NEAR_NOMINATION_EXP = 24;
|
|
/**
|
|
* Number of indivisible units in one NEAR. Derived from {@link NEAR_NOMINATION_EXP}.
|
|
*/
|
|
exports.NEAR_NOMINATION = 10n ** BigInt(exports.NEAR_NOMINATION_EXP);
|
|
// Pre-calculate offsets used for rounding to different number of digits
|
|
const ROUNDING_OFFSETS = [];
|
|
const BN10 = 10n;
|
|
for (let i = 0, offset = 5n; i < exports.NEAR_NOMINATION_EXP; i++, offset = offset * BN10) {
|
|
ROUNDING_OFFSETS[i] = offset;
|
|
}
|
|
/**
|
|
* Convert account balance value from internal indivisible units to NEAR. 1 NEAR is defined by {@link NEAR_NOMINATION}.
|
|
* Effectively this divides given amount by {@link NEAR_NOMINATION}.
|
|
*
|
|
* @param balance decimal string representing balance in smallest non-divisible NEAR units (as specified by {@link NEAR_NOMINATION})
|
|
* @param fracDigits number of fractional digits to preserve in formatted string. Balance is rounded to match given number of digits.
|
|
* @returns Value in Ⓝ
|
|
*/
|
|
function formatNearAmount(balance, fracDigits = exports.NEAR_NOMINATION_EXP) {
|
|
let balanceBN = BigInt(balance);
|
|
if (fracDigits !== exports.NEAR_NOMINATION_EXP) {
|
|
// Adjust balance for rounding at given number of digits
|
|
const roundingExp = exports.NEAR_NOMINATION_EXP - fracDigits - 1;
|
|
if (roundingExp > 0) {
|
|
balanceBN += ROUNDING_OFFSETS[roundingExp];
|
|
}
|
|
}
|
|
balance = balanceBN.toString();
|
|
const wholeStr = balance.substring(0, balance.length - exports.NEAR_NOMINATION_EXP) || "0";
|
|
const fractionStr = balance
|
|
.substring(balance.length - exports.NEAR_NOMINATION_EXP)
|
|
.padStart(exports.NEAR_NOMINATION_EXP, "0")
|
|
.substring(0, fracDigits);
|
|
return trimTrailingZeroes(`${formatWithCommas(wholeStr)}.${fractionStr}`);
|
|
}
|
|
exports.formatNearAmount = formatNearAmount;
|
|
/**
|
|
* Convert human readable NEAR amount to internal indivisible units.
|
|
* Effectively this multiplies given amount by {@link NEAR_NOMINATION}.
|
|
*
|
|
* @param amt decimal string (potentially fractional) denominated in NEAR.
|
|
* @returns The parsed yoctoⓃ amount or null if no amount was passed in
|
|
*/
|
|
function parseNearAmount(amt) {
|
|
if (!amt) {
|
|
return null;
|
|
}
|
|
amt = cleanupAmount(amt);
|
|
const split = amt.split(".");
|
|
const wholePart = split[0];
|
|
const fracPart = split[1] || "";
|
|
if (split.length > 2 || fracPart.length > exports.NEAR_NOMINATION_EXP) {
|
|
throw new Error(`Cannot parse '${amt}' as NEAR amount`);
|
|
}
|
|
return trimLeadingZeroes(wholePart + fracPart.padEnd(exports.NEAR_NOMINATION_EXP, "0"));
|
|
}
|
|
exports.parseNearAmount = parseNearAmount;
|
|
/**
|
|
* Removes commas from the input
|
|
* @param amount A value or amount that may contain commas
|
|
* @returns string The cleaned value
|
|
*/
|
|
function cleanupAmount(amount) {
|
|
return amount.replace(/,/g, "").trim();
|
|
}
|
|
/**
|
|
* Removes .000… from an input
|
|
* @param value A value that may contain trailing zeroes in the decimals place
|
|
* @returns string The value without the trailing zeros
|
|
*/
|
|
function trimTrailingZeroes(value) {
|
|
return value.replace(/\.?0*$/, "");
|
|
}
|
|
/**
|
|
* Removes leading zeroes from an input
|
|
* @param value A value that may contain leading zeroes
|
|
* @returns string The value without the leading zeroes
|
|
*/
|
|
function trimLeadingZeroes(value) {
|
|
value = value.replace(/^0+/, "");
|
|
if (value === "") {
|
|
return "0";
|
|
}
|
|
return value;
|
|
}
|
|
/**
|
|
* Returns a human-readable value with commas
|
|
* @param value A value that may not contain commas
|
|
* @returns string A value with commas
|
|
*/
|
|
function formatWithCommas(value) {
|
|
const pattern = /(-?\d+)(\d{3})/;
|
|
while (pattern.test(value)) {
|
|
value = value.replace(pattern, "$1,$2");
|
|
}
|
|
return value;
|
|
}
|
|
/**
|
|
* Encodes a Uint8Array or string into base58
|
|
* @param value Uint8Array or string representing a borsh encoded object
|
|
* @returns string base58 encoding of the value
|
|
*/
|
|
function baseEncode(value) {
|
|
if (typeof value === "string") {
|
|
const bytes = [];
|
|
for (let c = 0; c < value.length; c++) {
|
|
bytes.push(value.charCodeAt(c));
|
|
}
|
|
value = new Uint8Array(bytes);
|
|
}
|
|
return base_1.base58.encode(value);
|
|
}
|
|
exports.baseEncode = baseEncode;
|
|
/**
|
|
* Decodes a base58 string into a Uint8Array
|
|
* @param value base58 encoded string
|
|
* @returns Uint8Array representing the decoded value
|
|
*/
|
|
function baseDecode(value) {
|
|
return base_1.base58.decode(value);
|
|
}
|
|
exports.baseDecode = baseDecode;
|