From 0862caae4768e78cd70510e80da2ea9a3b6ba54a Mon Sep 17 00:00:00 2001 From: woodser Date: Sun, 5 Feb 2023 17:33:46 -0500 Subject: [PATCH] test completing trade from scheduled offer with and without dispute --- src/HavenoClient.test.ts | 30 +++++++++++++++++++++--------- src/HavenoClient.ts | 10 +++++++--- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/HavenoClient.test.ts b/src/HavenoClient.test.ts index 7bfc4b73..20955e40 100644 --- a/src/HavenoClient.test.ts +++ b/src/HavenoClient.test.ts @@ -1185,6 +1185,10 @@ test("Can schedule offers with locked funds (CI)", async () => { let err: any; try { + // configure test + const completeTrade = true; + const resolveDispute = Math.random() < 0.5; + // start user3 user3 = await initHaveno(); const user3Wallet = await monerojs.connectToWalletRpc("http://127.0.0.1:" + user3.getWalletRpcPort(), TestConfig.defaultHavenod.walletUsername, TestConfig.defaultHavenod.accountPassword); @@ -1196,7 +1200,8 @@ test("Can schedule offers with locked funds (CI)", async () => { // schedule offer const assetCode = "BCH"; const direction = "BUY"; - let offer: OfferInfo = await makeOffer({maker: user3, assetCode: assetCode, direction: direction, awaitFundsToMakeOffer: false}); + const ctx = Object.assign({}, TestConfig.trade, {maker: user3, assetCode: assetCode, direction: direction, awaitFundsToMakeOffer: false}); + let offer: OfferInfo = await makeOffer(ctx); assert.equal(offer.getState(), "SCHEDULED"); // has offer @@ -1249,16 +1254,23 @@ test("Can schedule offers with locked funds (CI)", async () => { await wait(TestConfig.trade.maxTimePeerNoticeMs); if (!getOffer(await user1.getOffers(assetCode, direction), offer.getId())) throw new Error("Offer " + offer.getId() + " was not found in peer's offers after posted"); - // cancel offer - await user3.removeOffer(offer.getId()); + // complete trade or cancel offer depending on configuration + if (completeTrade) { + HavenoUtils.log(1, "Completing trade from scheduled offer, opening and resolving dispute: " + resolveDispute); + await executeTrade(Object.assign(ctx, {buyerDisputeContext: resolveDispute ? DisputeContext.OPEN_AFTER_DEPOSITS_UNLOCK : DisputeContext.NONE})); + } else { + + // cancel offer + await user3.removeOffer(offer.getId()); - // offer is removed from my offers - if (getOffer(await user3.getMyOffers(assetCode), offer.getId())) throw new Error("Offer " + offer.getId() + " was found in my offers after removal"); + // offer is removed from my offers + if (getOffer(await user3.getMyOffers(assetCode), offer.getId())) throw new Error("Offer " + offer.getId() + " was found in my offers after removal"); - // reserved balance becomes unlocked - expect(BigInt((await user3.getBalances()).getAvailableBalance())).toEqual(outputAmt * BigInt(2)); - expect(BigInt((await user3.getBalances()).getPendingBalance())).toEqual(BigInt(0)); - expect(BigInt((await user3.getBalances()).getReservedOfferBalance())).toEqual(BigInt(0)); + // reserved balance becomes unlocked + expect(BigInt((await user3.getBalances()).getAvailableBalance())).toEqual(outputAmt * BigInt(2)); + expect(BigInt((await user3.getBalances()).getPendingBalance())).toEqual(BigInt(0)); + expect(BigInt((await user3.getBalances()).getReservedOfferBalance())).toEqual(BigInt(0)); + } } catch (err2) { err = err2; } diff --git a/src/HavenoClient.ts b/src/HavenoClient.ts index 2d505eb2..a8619635 100644 --- a/src/HavenoClient.ts +++ b/src/HavenoClient.ts @@ -1647,9 +1647,13 @@ export default class HavenoClient { firstRequest = false; return; } - await this._sendNotification(new NotificationMessage() - .setType(NotificationMessage.NotificationType.KEEP_ALIVE) - .setTimestamp(Date.now())); + try { + await this._sendNotification(new NotificationMessage() + .setType(NotificationMessage.NotificationType.KEEP_ALIVE) + .setTimestamp(Date.now())); + } catch (err: any) { + HavenoUtils.log(0, "Error sending keep alive request to Haveno daemon: " + err.message); + } }); this._keepAliveLooper.start(this._keepAlivePeriodMs);