Skip to content

Commit

Permalink
Test support for new takeoffer --amount param
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubstan committed Aug 6, 2022
1 parent 2a5bc45 commit dbd3d1f
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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<OfferInfo> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 8 additions & 0 deletions apitest/src/test/java/bisq/apitest/scenario/TradeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit dbd3d1f

Please sign in to comment.