Skip to content

Commit

Permalink
Show CLI err msgs if invalid takeoffer params are used for taking bsq…
Browse files Browse the repository at this point in the history
… swap
  • Loading branch information
ghubstan committed Sep 11, 2022
1 parent d32fd4e commit 23b9e1d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cli/src/main/java/bisq/cli/opts/TakeBsqSwapOfferOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public class TakeBsqSwapOfferOptionParser extends OfferIdOptionParser implements
.withRequiredArg()
.defaultsTo("0");

final OptionSpec<String> paymentAccountIdOpt = parser.accepts(OPT_PAYMENT_ACCOUNT_ID, "not used when taking bsq swaps")
.withRequiredArg()
.defaultsTo("invalid param");

final OptionSpec<String> takerFeeCurrencyCodeOpt = parser.accepts(OPT_FEE_CURRENCY, "not used when taking bsq swaps")
.withOptionalArg()
.defaultsTo("invalid param");

public TakeBsqSwapOfferOptionParser(String[] args) {
super(args, true);
}
Expand All @@ -40,6 +48,16 @@ public TakeBsqSwapOfferOptionParser parse() {

// Super class will short-circuit parsing if help option is present.

if (options.has(paymentAccountIdOpt)) {
throw new IllegalArgumentException("the " + OPT_PAYMENT_ACCOUNT_ID
+ " param is not used for swaps; the internal default swap account is always used");
}

if (options.has(takerFeeCurrencyCodeOpt)) {
throw new IllegalArgumentException("the " + OPT_FEE_CURRENCY
+ " param is not used for swaps; fees are always paid in bsq");
}

if (options.has(amountOpt)) {
if (options.valueOf(amountOpt).isEmpty())
throw new IllegalArgumentException("no intended btc trade amount specified");
Expand Down
51 changes: 50 additions & 1 deletion cli/src/test/java/bisq/cli/opts/OptionParsersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.junit.jupiter.api.Test;

import static bisq.cli.Method.*;
import static bisq.cli.Method.takeoffer;
import static bisq.cli.opts.OptLabel.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -444,4 +443,54 @@ public void testTakeOffer() {
assertEquals(takerFeeCurrencyCode, parser.getTakerFeeCurrencyCode());
assertEquals(amount, parser.getAmount());
}

@Test
public void testTakeBsqSwapOfferWithInvalidFeeCurrencyParam() {
var offerId = "ABC-OFFER-ID";
var takerFeeCurrencyCode = "BSQ";
var amount = "0.05";
String[] args = new String[]{
PASSWORD_OPT,
takeoffer.name(),
"--" + OPT_OFFER_ID + "=" + offerId,
"--" + OPT_FEE_CURRENCY + "=" + takerFeeCurrencyCode,
"--" + OPT_AMOUNT + "=" + amount
};
Throwable exception = assertThrows(RuntimeException.class, () ->
new TakeBsqSwapOfferOptionParser(args).parse());
assertEquals("the fee-currency param is not used for swaps; fees are always paid in bsq", exception.getMessage());
}

@Test
public void testTakeBsqSwapOfferWithInvalidPaymentAccountIdParam() {
var offerId = "ABC-OFFER-ID";
var paymentAccountId = "ABC-ACCT-ID";
var amount = "0.05";
String[] args = new String[]{
PASSWORD_OPT,
takeoffer.name(),
"--" + OPT_OFFER_ID + "=" + offerId,
"--" + OPT_PAYMENT_ACCOUNT_ID + "=" + paymentAccountId,
"--" + OPT_AMOUNT + "=" + amount
};
Throwable exception = assertThrows(RuntimeException.class, () ->
new TakeBsqSwapOfferOptionParser(args).parse());
assertEquals("the payment-account-id param is not used for swaps; the internal default swap account is always used",
exception.getMessage());
}

@Test
public void testTakeBsqSwapOffer() {
var offerId = "ABC-OFFER-ID";
var amount = "0.05";
String[] args = new String[]{
PASSWORD_OPT,
takeoffer.name(),
"--" + OPT_OFFER_ID + "=" + offerId,
"--" + OPT_AMOUNT + "=" + amount
};
var parser = new TakeBsqSwapOfferOptionParser(args).parse();
assertEquals(offerId, parser.getOfferId());
assertEquals(amount, parser.getAmount());
}
}

0 comments on commit 23b9e1d

Please sign in to comment.