Skip to content

Commit

Permalink
test trade role and getting all trades
Browse files Browse the repository at this point in the history
Co-authored-by: fa2a5qj3 <174058787+fa2a5qj3@users.noreply.github.com>
  • Loading branch information
woodser and fa2a5qj3 committed Jul 18, 2024
1 parent c13398e commit 46aa6b7
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/HavenoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2824,7 +2824,7 @@ async function takeOffer(ctxP: Partial<TradeContext>): Promise<TradeInfo> {
expect(tradeNotifications[0].getTitle()).toEqual("Offer Taken");
expect(tradeNotifications[0].getMessage()).toEqual("Your offer " + ctx.offerId + " has been accepted");

// record context after offer taken, once
// set context after offer taken, once
if (ctx.getBuyer().balancesAfterTake === undefined) {

// wait to observe deposit txs
Expand Down Expand Up @@ -2884,42 +2884,60 @@ async function takeOffer(ctxP: Partial<TradeContext>): Promise<TradeInfo> {
expect(takerBalanceDiffReservedOffer).toEqual(0n);
}

// maker is notified of balance change
// test getting trade for all parties
await testGetTrade(ctx);

// taker can get trade
let fetchedTrade: TradeInfo = await ctx.taker.havenod!.getTrade(trade.getTradeId());
await testTrade(fetchedTrade, ctx);
assert(moneroTs.GenUtils.arrayContains(["DEPOSITS_PUBLISHED", "DEPOSITS_CONFIRMED", "DEPOSITS_UNLOCKED"], fetchedTrade.getPhase()), "Unexpected trade phase: " + fetchedTrade.getPhase());
// TODO: more fetched trade tests

// market-priced offer amounts are unadjusted, fixed-priced offer amounts are adjusted (e.g. cash at atm is $10 increments)
// TODO: adjustments are based on payment method, not fixed-price
if (fetchedTrade.getOffer()!.getUseMarketBasedPrice()) {
assert.equal(ctx.tradeAmount, BigInt(fetchedTrade.getAmount()));
if (trade.getOffer()!.getUseMarketBasedPrice()) {
assert.equal(ctx.tradeAmount, BigInt(trade.getAmount()));
} else {
expect(Math.abs(HavenoUtils.percentageDiff(ctx.tradeAmount!, BigInt(fetchedTrade.getAmount())))).toBeLessThan(TestConfig.maxAdjustmentPct);
expect(Math.abs(HavenoUtils.percentageDiff(ctx.tradeAmount!, BigInt(trade.getAmount())))).toBeLessThan(TestConfig.maxAdjustmentPct);
}

// maker is notified of balance change

// taker is notified of balance change

// maker can get trade
fetchedTrade = await ctx.maker.havenod!.getTrade(trade.getTradeId());
await testTrade(fetchedTrade, ctx);
assert(moneroTs.GenUtils.arrayContains(["DEPOSITS_PUBLISHED", "DEPOSITS_CONFIRMED", "DEPOSITS_UNLOCKED"], fetchedTrade.getPhase()), "Unexpected trade phase: " + fetchedTrade.getPhase());
return trade;
}

async function testTrade(trade: TradeInfo, ctx: TradeContext) {
async function testTrade(trade: TradeInfo, ctx: TradeContext, havenod?: HavenoClient): Promise<void> {
expect(BigInt(trade.getAmount())).toEqual(ctx!.tradeAmount);

// test security deposit = max(.1, trade amount * security deposit pct)
const expectedSecurityDeposit = HavenoUtils.max(HavenoUtils.xmrToAtomicUnits(.1), HavenoUtils.multiply(ctx.tradeAmount!, ctx.securityDepositPct!));
expect(BigInt(trade.getBuyerSecurityDeposit())).toEqual(expectedSecurityDeposit - ctx.getBuyer().depositTxFee);
expect(BigInt(trade.getSellerSecurityDeposit())).toEqual(expectedSecurityDeposit - ctx.getSeller().depositTxFee);

// test phase
if (!ctx.isPaymentSent) {
assert(moneroTs.GenUtils.arrayContains(["DEPOSITS_PUBLISHED", "DEPOSITS_CONFIRMED", "DEPOSITS_UNLOCKED"], trade.getPhase()), "Unexpected trade phase: " + trade.getPhase());
}

// test role
const role = trade.getRole();
assert(role.length > 0); // TODO: test role string based on context


// TODO: test more fields
}

async function testGetTrade(ctx: TradeContext, havenod?: HavenoClient): Promise<void> {
if (havenod) {
const trade = await havenod.getTrade(ctx.offerId!);
await testTrade(trade, ctx);
const trades = await havenod.getTrades();
const foundTrade = trades.find((trade) => trade.getTradeId() === ctx.offerId);
assert(foundTrade);
await testTrade(foundTrade, ctx, havenod);
} else {
await testGetTrade(ctx, ctx.maker.havenod);
await testGetTrade(ctx, ctx.taker.havenod);
await testGetTrade(ctx, ctx.arbitrator.havenod);
}
}

async function testOpenDispute(ctxP: Partial<TradeContext>) {
let ctx = TradeContext.init(ctxP);

Expand Down

0 comments on commit 46aa6b7

Please sign in to comment.