bump version to v0.0.23 and update dist

This commit is contained in:
woodser 2024-04-08 09:26:36 -04:00
parent 28a47e1aba
commit d924a8068b
5 changed files with 75 additions and 51 deletions

View File

@ -49,13 +49,35 @@ export default class HavenoUtils {
*/ */
static waitFor(durationMs: number): Promise<unknown>; static waitFor(durationMs: number): Promise<unknown>;
/** /**
* Divide one bigint by another. * Convert XMR to atomic units.
* *
* @param {bigint} a dividend * @param {number | string} amountXmr - amount in XMR to convert to atomic units
* @param {bigint} b divisor * @return {bigint} amount in atomic units
*/
static xmrToAtomicUnits(amountXmr: number | string): bigint;
/**
* Convert atomic units to XMR.
*
* @param {bigint | string} amountAtomicUnits - amount in atomic units to convert to XMR
* @return {number} amount in XMR
*/
static atomicUnitsToXmr(amountAtomicUnits: bigint | string): number;
/**
* Divide one atomic units by another.
*
* @param {bigint} au1 dividend
* @param {bigint} au2 divisor
* @returns {number} the result * @returns {number} the result
*/ */
static divideBI(a: bigint, b: bigint): number; static divide(au1: bigint, au2: bigint): number;
/**
* Multiply a bigint by a number or bigint.
*
* @param a bigint to multiply
* @param b bigint or number to multiply by
* @returns the product as a bigint
*/
static multiply(a: bigint, b: number | bigint): bigint;
/** /**
* Calculate the difference from a first bigint to a second, as a percentage (float). * Calculate the difference from a first bigint to a second, as a percentage (float).
* *
@ -72,19 +94,13 @@ export default class HavenoUtils {
*/ */
static abs(a: bigint): bigint; static abs(a: bigint): bigint;
/** /**
* Convert XMR to atomic units. * Return the maximum of two bigints.
* *
* @param {number | string} amountXmr - amount in XMR to convert to atomic units * @param {bigint} bi1 first bigint
* @return {bigint} amount in atomic units * @param {bigint} bi2 second bigint
* @returns {bigint} the maximum of the two bigints
*/ */
static xmrToAtomicUnits(amountXmr: number | string): bigint; static max(bi1: bigint, bi2: bigint): bigint;
/**
* Convert atomic units to XMR.
*
* @param {bigint | string} amountAtomicUnits - amount in atomic units to convert to XMR
* @return {number} amount in XMR
*/
static atomicUnitsToXmr(amountAtomicUnits: bigint | string): number;
/** /**
* Get a validated payment method id from a string or form id. * Get a validated payment method id from a string or form id.
* *

View File

@ -92,14 +92,42 @@ class HavenoUtils {
return new Promise(function (resolve) { setTimeout(resolve, durationMs); }); return new Promise(function (resolve) { setTimeout(resolve, durationMs); });
} }
/** /**
* Divide one bigint by another. * Convert XMR to atomic units.
* *
* @param {bigint} a dividend * @param {number | string} amountXmr - amount in XMR to convert to atomic units
* @param {bigint} b divisor * @return {bigint} amount in atomic units
*/
static xmrToAtomicUnits(amountXmr) {
return BigInt(new decimal_js_1.default(amountXmr).mul(HavenoUtils.AU_PER_XMR.toString()).toFixed(0));
}
/**
* Convert atomic units to XMR.
*
* @param {bigint | string} amountAtomicUnits - amount in atomic units to convert to XMR
* @return {number} amount in XMR
*/
static atomicUnitsToXmr(amountAtomicUnits) {
return new decimal_js_1.default(amountAtomicUnits.toString()).div(HavenoUtils.AU_PER_XMR.toString()).toNumber();
}
/**
* Divide one atomic units by another.
*
* @param {bigint} au1 dividend
* @param {bigint} au2 divisor
* @returns {number} the result * @returns {number} the result
*/ */
static divideBI(a, b) { static divide(au1, au2) {
return Number(a * 10000000000000n / b) / 10000000000000; return this.atomicUnitsToXmr(au1) / this.atomicUnitsToXmr(au2);
}
/**
* Multiply a bigint by a number or bigint.
*
* @param a bigint to multiply
* @param b bigint or number to multiply by
* @returns the product as a bigint
*/
static multiply(a, b) {
return BigInt((new decimal_js_1.default(a.toString()).mul(new decimal_js_1.default(b.toString())).toFixed(0)));
} }
/** /**
* Calculate the difference from a first bigint to a second, as a percentage (float). * Calculate the difference from a first bigint to a second, as a percentage (float).
@ -109,7 +137,7 @@ class HavenoUtils {
* @returns {number} the percentage difference as a float * @returns {number} the percentage difference as a float
*/ */
static percentageDiff(a, b) { static percentageDiff(a, b) {
return HavenoUtils.divideBI(a - b, a); return HavenoUtils.divide(a - b, a);
} }
/** /**
* Return the absolute value of the given bigint. * Return the absolute value of the given bigint.
@ -121,34 +149,14 @@ class HavenoUtils {
return a < 0 ? -a : a; return a < 0 ? -a : a;
} }
/** /**
* Convert XMR to atomic units. * Return the maximum of two bigints.
* *
* @param {number | string} amountXmr - amount in XMR to convert to atomic units * @param {bigint} bi1 first bigint
* @return {bigint} amount in atomic units * @param {bigint} bi2 second bigint
* @returns {bigint} the maximum of the two bigints
*/ */
static xmrToAtomicUnits(amountXmr) { static max(bi1, bi2) {
if (typeof amountXmr === "number") return bi1 > bi2 ? bi1 : bi2;
amountXmr = "" + amountXmr;
let decimalDivisor = 1;
let decimalIdx = amountXmr.indexOf('.');
if (decimalIdx > -1) {
decimalDivisor = Math.pow(10, amountXmr.length - decimalIdx - 1);
amountXmr = amountXmr.slice(0, decimalIdx) + amountXmr.slice(decimalIdx + 1);
}
return BigInt(amountXmr) * BigInt(HavenoUtils.AU_PER_XMR) / BigInt(decimalDivisor);
}
/**
* Convert atomic units to XMR.
*
* @param {bigint | string} amountAtomicUnits - amount in atomic units to convert to XMR
* @return {number} amount in XMR
*/
static atomicUnitsToXmr(amountAtomicUnits) {
if (typeof amountAtomicUnits === "string")
amountAtomicUnits = BigInt(amountAtomicUnits);
const quotient = amountAtomicUnits / HavenoUtils.AU_PER_XMR;
const remainder = amountAtomicUnits % HavenoUtils.AU_PER_XMR;
return new decimal_js_1.default(quotient.toString()).plus(new decimal_js_1.default(remainder.toString()).div(HavenoUtils.AU_PER_XMR.toString())).toNumber();
} }
// ------------------------- PAYMENT ACCOUNT FORMS -------------------------- // ------------------------- PAYMENT ACCOUNT FORMS --------------------------
/** /**

File diff suppressed because one or more lines are too long

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "haveno-ts", "name": "haveno-ts",
"version": "0.0.22", "version": "0.0.23",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "haveno-ts", "name": "haveno-ts",
"version": "0.0.22", "version": "0.0.23",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@types/node": "^18.14.2", "@types/node": "^18.14.2",

View File

@ -1,6 +1,6 @@
{ {
"name": "haveno-ts", "name": "haveno-ts",
"version": "0.0.22", "version": "0.0.23",
"description": "Haveno TypeScript interface", "description": "Haveno TypeScript interface",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",