From dbd3d1f7fb59f7d652dedb37a1f2879f52fc3ea2 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sat, 6 Aug 2022 15:11:10 -0300 Subject: [PATCH] Test support for new takeoffer --amount param --- .../method/trade/AbstractTradeTest.java | 8 +- .../method/trade/BsqSwapBuyBtcTradeTest.java | 2 +- .../method/trade/BsqSwapSellBtcTradeTest.java | 2 +- .../trade/InsufficientBtcToTakeOfferTest.java | 1 + .../method/trade/TakeBuyBSQOfferTest.java | 3 + .../method/trade/TakeBuyBTCOfferTest.java | 3 + ...keBuyBTCOfferWithNationalBankAcctTest.java | 12 +- .../method/trade/TakeBuyXMROfferTest.java | 8 +- .../TakeOfferWithOutOfRangeAmountTest.java | 108 ++++++++++++++++++ .../method/trade/TakeSellBSQOfferTest.java | 6 + .../method/trade/TakeSellBTCOfferTest.java | 2 + .../method/trade/TakeSellXMROfferTest.java | 8 +- .../java/bisq/apitest/scenario/TradeTest.java | 8 ++ .../bisq/apitest/scenario/bot/BotClient.java | 2 +- 14 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 apitest/src/test/java/bisq/apitest/method/trade/TakeOfferWithOutOfRangeAmountTest.java diff --git a/apitest/src/test/java/bisq/apitest/method/trade/AbstractTradeTest.java b/apitest/src/test/java/bisq/apitest/method/trade/AbstractTradeTest.java index b389b6d6fb8..77408cb1ec1 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/AbstractTradeTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/AbstractTradeTest.java @@ -61,21 +61,25 @@ public static void initStaticFixtures() { protected final TradeInfo takeAlicesOffer(String offerId, String paymentAccountId, - String takerFeeCurrencyCode) { + String takerFeeCurrencyCode, + long intendedTradeAmount) { return takeAlicesOffer(offerId, paymentAccountId, takerFeeCurrencyCode, + intendedTradeAmount, true); } protected final TradeInfo takeAlicesOffer(String offerId, String paymentAccountId, String takerFeeCurrencyCode, + long intendedTradeAmount, boolean generateBtcBlock) { @SuppressWarnings("ConstantConditions") var trade = bobClient.takeOffer(offerId, paymentAccountId, - takerFeeCurrencyCode); + takerFeeCurrencyCode, + intendedTradeAmount); assertNotNull(trade); assertEquals(offerId, trade.getTradeId()); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapBuyBtcTradeTest.java b/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapBuyBtcTradeTest.java index 172f4b2c449..98002814907 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapBuyBtcTradeTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapBuyBtcTradeTest.java @@ -120,7 +120,7 @@ public void testBobTakesBsqSwapOffer() { sleep(3_000); - var swapTrade = bobClient.takeBsqSwapOffer(availableSwapOffer.getId()); + var swapTrade = bobClient.takeBsqSwapOffer(availableSwapOffer.getId(), 0L); tradeId = swapTrade.getTradeId(); // Cache the tradeId for following test case(s). log.debug("BsqSwap Trade at PREPARATION:\n{}", toTradeDetailTable.apply(swapTrade)); assertEquals(PREPARATION.name(), swapTrade.getState()); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapSellBtcTradeTest.java b/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapSellBtcTradeTest.java index 9c97695c2d6..7eb5e498abc 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapSellBtcTradeTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/BsqSwapSellBtcTradeTest.java @@ -118,7 +118,7 @@ public void testBobTakesBsqSwapOffer() { sleep(10_000); - var swapTrade = bobClient.takeBsqSwapOffer(availableSwapOffer.getId()); + var swapTrade = bobClient.takeBsqSwapOffer(availableSwapOffer.getId(), 0L); tradeId = swapTrade.getTradeId(); // Cache the tradeId for following test case(s). log.debug("BsqSwap Trade at PREPARATION:\n{}", toTradeDetailTable.apply(swapTrade)); assertEquals(PREPARATION.name(), swapTrade.getState()); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/InsufficientBtcToTakeOfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/InsufficientBtcToTakeOfferTest.java index 0a91cd367c9..bdd71c0aaf6 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/InsufficientBtcToTakeOfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/InsufficientBtcToTakeOfferTest.java @@ -107,6 +107,7 @@ public void testTakeOfferWithInsufficientBTC() { takeAlicesOffer(offerId, bobsUsdAccount.getId(), TRADE_FEE_CURRENCY_CODE, + 12_500_000L, false)); String expectedExceptionMessage = format("UNAVAILABLE: wallet has insufficient btc to take offer with id '%s'", offerId); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBSQOfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBSQOfferTest.java index 923a243b28a..c2aab2f36a2 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBSQOfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBSQOfferTest.java @@ -87,9 +87,11 @@ public void testTakeAlicesSellBTCForBSQOffer(final TestInfo testInfo) { var alicesBsqOffers = aliceClient.getMyOffers(btcTradeDirection, BSQ); assertEquals(1, alicesBsqOffers.size()); + var intendedTradeAmount = 10_000_000L; var trade = takeAlicesOffer(offerId, bobsLegacyBsqAcct.getId(), TRADE_FEE_CURRENCY_CODE, + intendedTradeAmount, false); assertNotNull(trade); assertEquals(offerId, trade.getTradeId()); @@ -105,6 +107,7 @@ public void testTakeAlicesSellBTCForBSQOffer(final TestInfo testInfo) { genBtcBlocksThenWait(1, 2_500); trade = bobClient.getTrade(tradeId); + assertEquals(intendedTradeAmount, trade.getTradeAmountAsLong()); verifyTakerDepositConfirmed(trade); logTrade(log, testInfo, "Alice's Maker/Buyer View (Payment Sent)", aliceClient.getTrade(tradeId)); logTrade(log, testInfo, "Bob's Taker/Seller View (Payment Sent)", bobClient.getTrade(tradeId)); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferTest.java index 54d20acc23f..4872c21032b 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferTest.java @@ -76,15 +76,18 @@ public void testTakeAlicesBuyOffer(final TestInfo testInfo) { assertEquals(1, alicesUsdOffers.size()); PaymentAccount bobsUsdAccount = createDummyF2FAccount(bobClient, "US"); + var ignoredTakeOfferAmountParam = 0L; var trade = takeAlicesOffer(offerId, bobsUsdAccount.getId(), TRADE_FEE_CURRENCY_CODE, + ignoredTakeOfferAmountParam, false); sleep(2_500); // Allow available offer to be removed from offer book. alicesUsdOffers = aliceClient.getMyOffersSortedByDate(BUY.name(), USD); assertEquals(0, alicesUsdOffers.size()); trade = bobClient.getTrade(tradeId); + assertEquals(alicesOffer.getAmount(), trade.getTradeAmountAsLong()); verifyTakerDepositNotConfirmed(trade); logTrade(log, testInfo, "Alice's Maker/Buyer View", aliceClient.getTrade(tradeId)); logTrade(log, testInfo, "Bob's Taker/Seller View", bobClient.getTrade(tradeId)); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferWithNationalBankAcctTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferWithNationalBankAcctTest.java index d1b7efd3492..9c1a1f99ebe 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferWithNationalBankAcctTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyBTCOfferWithNationalBankAcctTest.java @@ -37,6 +37,7 @@ import bisq.core.payment.PaymentAccount; import bisq.core.payment.payload.NationalBankAccountPayload; +import io.grpc.Status; import io.grpc.StatusRuntimeException; import lombok.extern.slf4j.Slf4j; @@ -112,11 +113,12 @@ public void testTakeAlicesBuyOffer(final TestInfo testInfo) { var alicesOffers = aliceClient.getMyOffersSortedByDate(BUY.name(), BRL); assertEquals(1, alicesOffers.size()); - var trade = takeAlicesOffer(offerId, bobsPaymentAccount.getId(), TRADE_FEE_CURRENCY_CODE, + 0L, false); + assertEquals(alicesOffer.getAmount(), trade.getTradeAmountAsLong()); // Before generating a blk and confirming deposit tx, make sure there // are no bank acct details in the either side's contract. @@ -130,13 +132,11 @@ public void testTakeAlicesBuyOffer(final TestInfo testInfo) { verifyJsonContractExcludesBankAccountDetails(bobsContract, bobsPaymentAccount); break; } catch (StatusRuntimeException ex) { - if (ex.getMessage() == null) { + if (ex.getStatus().equals(Status.NOT_FOUND)) { String message = ex.getMessage().replaceFirst("^[A-Z_]+: ", ""); - if (message.contains("trade") && message.contains("not found")) { - fail(ex); - } + log.warn(message); } else { - sleep(500); + sleep(1_000); } } } diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyXMROfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyXMROfferTest.java index 5e85a866e99..bda3b7835d3 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyXMROfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeBuyXMROfferTest.java @@ -87,11 +87,17 @@ public void testTakeAlicesSellBTCForXMROffer(final TestInfo testInfo) { var alicesXmrOffers = aliceClient.getMyOffers(btcTradeDirection, XMR); assertEquals(1, alicesXmrOffers.size()); - var trade = takeAlicesOffer(offerId, bobsXmrAcct.getId(), TRADE_FEE_CURRENCY_CODE); + + var intendedTradeAmount = 10_000_000L; + var trade = takeAlicesOffer(offerId, + bobsXmrAcct.getId(), + TRADE_FEE_CURRENCY_CODE, + intendedTradeAmount); alicesXmrOffers = aliceClient.getMyOffersSortedByDate(XMR); assertEquals(0, alicesXmrOffers.size()); trade = bobClient.getTrade(tradeId); + assertEquals(intendedTradeAmount, trade.getTradeAmountAsLong()); verifyTakerDepositNotConfirmed(trade); logTrade(log, testInfo, "Alice's Maker/Buyer View", aliceClient.getTrade(tradeId)); logTrade(log, testInfo, "Bob's Taker/Seller View", bobClient.getTrade(tradeId)); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeOfferWithOutOfRangeAmountTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeOfferWithOutOfRangeAmountTest.java new file mode 100644 index 00000000000..4c76d6e40e7 --- /dev/null +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeOfferWithOutOfRangeAmountTest.java @@ -0,0 +1,108 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.apitest.method.trade; + +import bisq.core.payment.PaymentAccount; + +import bisq.proto.grpc.OfferInfo; + +import io.grpc.StatusRuntimeException; + +import org.bitcoinj.core.Coin; + +import java.util.List; + +import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.TestMethodOrder; + +import static bisq.apitest.config.ApiTestConfig.BTC; +import static bisq.apitest.config.ApiTestConfig.USD; +import static java.lang.String.format; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; +import static protobuf.OfferDirection.BUY; + +@Disabled +@SuppressWarnings("ConstantConditions") +@Slf4j +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class TakeOfferWithOutOfRangeAmountTest extends AbstractTradeTest { + + @Test + @Order(1) + public void testTakeOfferWithInvalidAmountParam(final TestInfo testInfo) { + try { + PaymentAccount alicesUsdAccount = createDummyF2FAccount(aliceClient, "US"); + PaymentAccount bobsUsdAccount = createDummyF2FAccount(bobClient, "US"); + + var alicesOffer = aliceClient.createMarketBasedPricedOffer(BUY.name(), + USD, + 10_000_000L, + 8_000_000L, + 0.00, + defaultBuyerSecurityDepositPct.get(), + alicesUsdAccount.getId(), + BTC, + NO_TRIGGER_PRICE); + + // Wait for Alice's AddToOfferBook task. + // Wait times vary; my logs show >= 2-second delay. + sleep(3_000); // TODO loop instead of hard code a wait time + List alicesUsdOffers = aliceClient.getMyOffersSortedByDate(BUY.name(), USD); + assertEquals(1, alicesUsdOffers.size()); + + var intendedTradeAmountTooLow = 7_000_000L; + takeOfferWithInvalidAmountParam(bobsUsdAccount, alicesOffer, intendedTradeAmountTooLow); + + var intendedTradeAmountTooHigh = 11_000_000L; + takeOfferWithInvalidAmountParam(bobsUsdAccount, alicesOffer, intendedTradeAmountTooHigh); + } catch (StatusRuntimeException e) { + fail(e); + } + } + + private void takeOfferWithInvalidAmountParam(PaymentAccount paymentAccount, + OfferInfo offer, + long invalidTakeOfferAmount) { + Throwable exception = assertThrows(StatusRuntimeException.class, () -> + takeAlicesOffer(offer.getId(), + paymentAccount.getId(), + BTC, + invalidTakeOfferAmount, + false)); + + var invalidAmount = Coin.valueOf(invalidTakeOfferAmount); + var minAmount = Coin.valueOf(offer.getMinAmount()); + var maxAmount = Coin.valueOf(offer.getAmount()); + String expectedExceptionMessage = + format("INVALID_ARGUMENT: intended trade amount %s is outside offer's min - max amount range of %s - %s", + invalidAmount.toPlainString(), + minAmount.toPlainString(), + maxAmount.toPlainString()); + log.info(exception.getMessage()); + assertEquals(expectedExceptionMessage, exception.getMessage()); + } + +} diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBSQOfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBSQOfferTest.java index efd0a6c77af..27b0ecf515d 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBSQOfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBSQOfferTest.java @@ -87,16 +87,22 @@ public void testTakeAlicesBuyBTCForBSQOffer(final TestInfo testInfo) { assertTrue(alicesOffer.getIsCurrencyForMakerFeeBtc()); var alicesBsqOffers = aliceClient.getMyOffers(btcTradeDirection, BSQ); assertEquals(1, alicesBsqOffers.size()); + + var intendedTradeAmount = 10_000_000L; var trade = takeAlicesOffer(offerId, bobsLegacyBsqAcct.getId(), TRADE_FEE_CURRENCY_CODE, + intendedTradeAmount, false); + sleep(2_500); // Allow available offer to be removed from offer book. alicesBsqOffers = aliceClient.getMyOffersSortedByDate(BSQ); assertEquals(0, alicesBsqOffers.size()); genBtcBlocksThenWait(1, 2_500); waitForTakerDepositConfirmation(log, testInfo, bobClient, trade.getTradeId()); + trade = bobClient.getTrade(tradeId); + assertEquals(intendedTradeAmount, trade.getTradeAmountAsLong()); verifyTakerDepositConfirmed(trade); logTrade(log, testInfo, "Alice's Maker/Seller View", aliceClient.getTrade(tradeId)); logTrade(log, testInfo, "Bob's Taker/Buyer View", bobClient.getTrade(tradeId)); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBTCOfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBTCOfferTest.java index 6d9f2e450a8..cb9de760337 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBTCOfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeSellBTCOfferTest.java @@ -83,12 +83,14 @@ public void testTakeAlicesSellOffer(final TestInfo testInfo) { var trade = takeAlicesOffer(offerId, bobsUsdAccount.getId(), TRADE_FEE_CURRENCY_CODE, + 0L, false); sleep(2_500); // Allow available offer to be removed from offer book. var takeableUsdOffers = bobClient.getOffersSortedByDate(SELL.name(), USD); assertEquals(0, takeableUsdOffers.size()); trade = bobClient.getTrade(tradeId); + assertEquals(alicesOffer.getAmount(), trade.getTradeAmountAsLong()); verifyTakerDepositNotConfirmed(trade); logTrade(log, testInfo, "Alice's Maker/Buyer View", aliceClient.getTrade(tradeId)); logTrade(log, testInfo, "Bob's Taker/Seller View", bobClient.getTrade(tradeId)); diff --git a/apitest/src/test/java/bisq/apitest/method/trade/TakeSellXMROfferTest.java b/apitest/src/test/java/bisq/apitest/method/trade/TakeSellXMROfferTest.java index 90c8fe1007e..ad9162d09af 100644 --- a/apitest/src/test/java/bisq/apitest/method/trade/TakeSellXMROfferTest.java +++ b/apitest/src/test/java/bisq/apitest/method/trade/TakeSellXMROfferTest.java @@ -90,11 +90,17 @@ public void testTakeAlicesBuyBTCForXMROffer(final TestInfo testInfo) { var alicesXmrOffers = aliceClient.getMyOffers(btcTradeDirection, XMR); assertEquals(1, alicesXmrOffers.size()); - var trade = takeAlicesOffer(offerId, bobsXmrAcct.getId(), TRADE_FEE_CURRENCY_CODE); + + var intendedTradeAmount = 10_500_000L; + var trade = takeAlicesOffer(offerId, + bobsXmrAcct.getId(), + TRADE_FEE_CURRENCY_CODE, + intendedTradeAmount); alicesXmrOffers = aliceClient.getMyOffersSortedByDate(XMR); assertEquals(0, alicesXmrOffers.size()); trade = bobClient.getTrade(tradeId); + assertEquals(intendedTradeAmount, trade.getTradeAmountAsLong()); verifyTakerDepositNotConfirmed(trade); logTrade(log, testInfo, "Alice's Maker/Seller View", aliceClient.getTrade(tradeId)); logTrade(log, testInfo, "Bob's Taker/Buyer View", bobClient.getTrade(tradeId)); diff --git a/apitest/src/test/java/bisq/apitest/scenario/TradeTest.java b/apitest/src/test/java/bisq/apitest/scenario/TradeTest.java index 3c2252d402f..d5ee26c041c 100644 --- a/apitest/src/test/java/bisq/apitest/scenario/TradeTest.java +++ b/apitest/src/test/java/bisq/apitest/scenario/TradeTest.java @@ -37,6 +37,7 @@ import bisq.apitest.method.trade.TakeBuyBTCOfferTest; import bisq.apitest.method.trade.TakeBuyBTCOfferWithNationalBankAcctTest; import bisq.apitest.method.trade.TakeBuyXMROfferTest; +import bisq.apitest.method.trade.TakeOfferWithOutOfRangeAmountTest; import bisq.apitest.method.trade.TakeSellBSQOfferTest; import bisq.apitest.method.trade.TakeSellBTCOfferTest; import bisq.apitest.method.trade.TakeSellXMROfferTest; @@ -159,4 +160,11 @@ public void testFailUnfailTrade(final TestInfo testInfo) { test.testFailAndUnFailBuyXmrTrade(testInfo); test.testFailAndUnFailTakeSellXMRTrade(testInfo); } + + @Test + @Order(11) + public void testTakeOfferWithOutOfRangeAmount(final TestInfo testInfo) { + TakeOfferWithOutOfRangeAmountTest test = new TakeOfferWithOutOfRangeAmountTest(); + test.testTakeOfferWithInvalidAmountParam(testInfo); + } } diff --git a/apitest/src/test/java/bisq/apitest/scenario/bot/BotClient.java b/apitest/src/test/java/bisq/apitest/scenario/bot/BotClient.java index da11b5fa694..ccdd236f3f3 100644 --- a/apitest/src/test/java/bisq/apitest/scenario/bot/BotClient.java +++ b/apitest/src/test/java/bisq/apitest/scenario/bot/BotClient.java @@ -174,7 +174,7 @@ public OfferInfo createOfferAtFixedPrice(PaymentAccount paymentAccount, } public TradeInfo takeOffer(String offerId, PaymentAccount paymentAccount, String feeCurrency) { - return grpcClient.takeOffer(offerId, paymentAccount.getId(), feeCurrency); + return grpcClient.takeOffer(offerId, paymentAccount.getId(), feeCurrency, 0L); } /**