switch to atomic units as native units

This commit is contained in:
woodser 2023-03-04 16:17:05 -05:00
parent 05b666d6b9
commit c376be67e2
2 changed files with 20 additions and 20 deletions

View File

@ -156,11 +156,11 @@ const TestConfig = {
takeOffer: true,
awaitFundsToMakeOffer: true,
direction: "buy", // buy or sell xmr
amount: BigInt("200000000000"), // amount of xmr to trade
amount: BigInt("200000000000"), // amount of xmr to trade (0.2 XMR)
minAmount: undefined,
assetCode: "usd", // counter asset to trade
makerPaymentAccountId: undefined,
buyerSecurityDeposit: 0.15,
buyerSecurityDepositPct: 0.15,
price: undefined, // use market price if undefined
priceMargin: 0.0,
triggerPrice: undefined,
@ -219,7 +219,7 @@ interface TradeContext {
amount?: bigint,
minAmount?: bigint,
makerPaymentAccountId?: string,
buyerSecurityDeposit?: number,
buyerSecurityDepositPct?: number,
price?: number,
priceMargin?: number,
triggerPrice?: number,
@ -1474,7 +1474,7 @@ test("Can resolve disputes (CI)", async () => {
disputeWinner: DisputeResult.Winner.BUYER,
disputeReason: DisputeResult.Reason.SELLER_NOT_RESPONDING,
disputeSummary: "Split trade amount",
disputeWinnerAmount: HavenoUtils.centinerosToAtomicUnits(trade1.getAmountAsLong()) / BigInt(2) + HavenoUtils.centinerosToAtomicUnits(trade1.getBuyerSecurityDeposit())
disputeWinnerAmount: BigInt(trade1.getAmountAsLong()) / BigInt(2) + BigInt(trade1.getBuyerSecurityDeposit())
});
Object.assign(ctxs[2], {
resolveDispute: false,
@ -1482,7 +1482,7 @@ test("Can resolve disputes (CI)", async () => {
disputeWinner: DisputeResult.Winner.SELLER,
disputeReason: DisputeResult.Reason.TRADE_ALREADY_SETTLED,
disputeSummary: "Seller gets everything",
disputeWinnerAmount: HavenoUtils.centinerosToAtomicUnits(trade2.getAmountAsLong() + trade2.getBuyerSecurityDeposit() + trade2.getSellerSecurityDeposit())
disputeWinnerAmount: BigInt(trade2.getAmountAsLong()) + BigInt(trade2.getBuyerSecurityDeposit()) + BigInt(trade2.getSellerSecurityDeposit())
});
Object.assign(ctxs[3], {
resolveDispute: false,
@ -2264,7 +2264,7 @@ async function makeOffer(ctx?: TradeContext): Promise<OfferInfo> {
ctx.amount!,
ctx.assetCode!,
ctx.makerPaymentAccountId!,
ctx.buyerSecurityDeposit!,
ctx.buyerSecurityDepositPct!,
ctx.price,
ctx.priceMargin,
ctx.triggerPrice,
@ -2507,7 +2507,7 @@ async function resolveDispute(ctx: TradeContext) {
// award too little to loser
const tradeAmount: bigint = BigInt(ctx.offer!.getAmount());
const customWinnerAmount = tradeAmount + BigInt(ctx.offer!.getBuyerSecurityDeposit() + ctx.offer!.getSellerSecurityDeposit()) - BigInt("10000");
const customWinnerAmount = tradeAmount + BigInt(ctx.offer!.getBuyerSecurityDeposit()) + BigInt(ctx.offer!.getSellerSecurityDeposit()) - BigInt("10000");
try {
await arbitrator.resolveDispute(ctx.offerId!, ctx.disputeWinner!, ctx.disputeReason!, "Loser gets too little", customWinnerAmount);
throw new Error("Should have failed resolving dispute with insufficient loser payout");
@ -3261,8 +3261,8 @@ function testOffer(offer: OfferInfo, config?: TradeContext) {
expect(offer.getId().length).toBeGreaterThan(0);
if (config) {
expect(BigInt(offer.getAmount())).toEqual(config.amount);
expect(offer.getBuyerSecurityDeposit() / offer.getAmount()).toEqual(config.buyerSecurityDeposit);
expect(offer.getSellerSecurityDeposit() / offer.getAmount()).toEqual(config.buyerSecurityDeposit); // TODO: use same config.securityDeposit for buyer and seller?
expect(HavenoUtils.divideBI(BigInt(offer.getBuyerSecurityDeposit()), BigInt(offer.getAmount()))).toEqual(config.buyerSecurityDepositPct);
expect(HavenoUtils.divideBI(BigInt(offer.getSellerSecurityDeposit()), BigInt(offer.getAmount()))).toEqual(config.buyerSecurityDepositPct); // TODO: using same security deposit config for buyer and seller
}
// TODO: test rest of offer
}

View File

@ -8,7 +8,6 @@ import { PaymentAccountForm, PaymentAccountFormField } from "../protobuf/pb_pb";
export default class HavenoUtils {
static logLevel = 0;
static centinerosToAUMultiplier = 10000;
static months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
static lastLogTimeMs = 0;
@ -74,16 +73,6 @@ export default class HavenoUtils {
});
}
/**
* Convert centineros to atomic units.
*
* @param {number} centineros - denominates an amount of XMR in centineros
* @return {BigInt} the amount denominated in atomic units
*/
static centinerosToAtomicUnits(centineros: number): bigint {
return BigInt(centineros) * BigInt(HavenoUtils.centinerosToAUMultiplier);
}
/**
* Stringify a payment account form.
*
@ -139,4 +128,15 @@ export default class HavenoUtils {
static async waitFor(durationMs: number) {
return new Promise(function(resolve) { setTimeout(resolve, durationMs); });
}
/**
* Divide one bigint by another.
*
* @param {bigint} a dividend
* @param {bigint} b divisor
* @returns {number} the result
*/
static divideBI(a: bigint, b: bigint): number {
return Number(a * 100n / b) / 100
}
}