mirror of
https://github.com/haveno-dex/haveno-ts.git
synced 2025-02-23 16:09:57 -05:00
add atomicUnitsToXmr and xmrToAtomicUnits utils
This commit is contained in:
parent
31e78ec55e
commit
ff810de4b7
@ -176,8 +176,8 @@ const TestConfig = {
|
|||||||
walletSyncPeriodMs: 5000,
|
walletSyncPeriodMs: 5000,
|
||||||
maxTimePeerNoticeMs: 5000,
|
maxTimePeerNoticeMs: 5000,
|
||||||
maxConcurrency: 14, // max concurrency
|
maxConcurrency: 14, // max concurrency
|
||||||
maxConcurrencyCI: 4, // CI test max concurrency
|
maxConcurrencyCI: 14, // CI test max concurrency
|
||||||
stopOnFailure: false
|
stopOnFailure: true
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -382,6 +382,12 @@ test("Can get the version (CI)", async () => {
|
|||||||
expect(version).toEqual(TestConfig.haveno.version);
|
expect(version).toEqual(TestConfig.haveno.version);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("Can convert between XMR and atomic units (CI)", async () => {
|
||||||
|
expect(BigInt(250000000000)).toEqual(HavenoUtils.xmrToAtomicUnits(0.25));
|
||||||
|
expect(HavenoUtils.atomicUnitsToXmr("250000000000")).toEqual(.25);
|
||||||
|
expect(HavenoUtils.atomicUnitsToXmr(BigInt("250000000000"))).toEqual(.25);
|
||||||
|
});
|
||||||
|
|
||||||
test("Can manage an account (CI)", async () => {
|
test("Can manage an account (CI)", async () => {
|
||||||
let user3: HavenoClient|undefined;
|
let user3: HavenoClient|undefined;
|
||||||
let err: any;
|
let err: any;
|
||||||
|
@ -10,6 +10,7 @@ export default class HavenoUtils {
|
|||||||
static logLevel = 0;
|
static logLevel = 0;
|
||||||
static months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
|
static months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
|
||||||
static lastLogTimeMs = 0;
|
static lastLogTimeMs = 0;
|
||||||
|
static AU_PER_XMR = 1000000000000n;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the log level with 0 being least verbose.
|
* Set the log level with 0 being least verbose.
|
||||||
@ -139,4 +140,36 @@ export default class HavenoUtils {
|
|||||||
static divideBI(a: bigint, b: bigint): number {
|
static divideBI(a: bigint, b: bigint): number {
|
||||||
return Number(a * 100n / b) / 100
|
return Number(a * 100n / b) / 100
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert XMR to atomic units.
|
||||||
|
*
|
||||||
|
* @param {number|string} amountXmr - amount in XMR to convert to atomic units
|
||||||
|
* @return {BigInt} amount in atomic units
|
||||||
|
*/
|
||||||
|
static xmrToAtomicUnits(amountXmr: number|string): BigInt {
|
||||||
|
if (typeof amountXmr === "number") amountXmr = "" + amountXmr;
|
||||||
|
else if (typeof amountXmr !== "string") throw new Error("Must provide XMR amount as a string or js number to convert to atomic units");
|
||||||
|
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: BigInt|string) {
|
||||||
|
if (typeof amountAtomicUnits === "string") amountAtomicUnits = BigInt(amountAtomicUnits);
|
||||||
|
else if (typeof amountAtomicUnits !== "bigint") throw new Error("Must provide atomic units as BigInt or string to convert to XMR");
|
||||||
|
const quotient: bigint = amountAtomicUnits as bigint / HavenoUtils.AU_PER_XMR;
|
||||||
|
const remainder: bigint = amountAtomicUnits as bigint % HavenoUtils.AU_PER_XMR;
|
||||||
|
return Number(quotient) + Number(remainder) / Number(HavenoUtils.AU_PER_XMR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user