Skip to content

Commit

Permalink
Merge pull request #5812 from ghubstan/02-cli-console-formatting-api
Browse files Browse the repository at this point in the history
Deprecate and replace hard-coded CLI console output formatters
  • Loading branch information
ripcurlx authored Nov 9, 2021
2 parents 06da45f + 4f18992 commit 1578f45
Show file tree
Hide file tree
Showing 87 changed files with 4,734 additions and 1,347 deletions.
3 changes: 3 additions & 0 deletions apitest/src/main/java/bisq/apitest/config/ApiTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class ApiTestConfig {
// Global constants
public static final String BSQ = "BSQ";
public static final String BTC = "BTC";
public static final String EUR = "EUR";
public static final String USD = "USD";
public static final String XMR = "XMR";
public static final String ARBITRATOR = "arbitrator";
public static final String MEDIATOR = "mediator";
public static final String REFUND_AGENT = "refundagent";
Expand Down
16 changes: 16 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import bisq.common.util.Utilities;

import bisq.proto.grpc.BalancesInfo;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -35,7 +37,11 @@

import javax.annotation.Nullable;

import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.apitest.config.ApiTestRateMeterInterceptorConfig.getTestRateMeterInterceptorConfig;
import static bisq.cli.table.builder.TableType.BSQ_BALANCE_TBL;
import static bisq.cli.table.builder.TableType.BTC_BALANCE_TBL;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.stream;
Expand All @@ -46,6 +52,7 @@
import bisq.apitest.ApiTestCase;
import bisq.apitest.linux.BashCommand;
import bisq.cli.GrpcClient;
import bisq.cli.table.builder.TableBuilder;

public class MethodTest extends ApiTestCase {

Expand Down Expand Up @@ -155,6 +162,15 @@ protected final bisq.core.payment.PaymentAccount createPaymentAccount(GrpcClient
return bisq.core.payment.PaymentAccount.fromProto(paymentAccount, CORE_PROTO_RESOLVER);
}

public static String formatBalancesTbls(BalancesInfo allBalances) {
StringBuilder balances = new StringBuilder(BTC).append("\n");
balances.append(new TableBuilder(BTC_BALANCE_TBL, allBalances.getBtc()).build());
balances.append("\n");
balances.append(BSQ).append("\n");
balances.append(new TableBuilder(BSQ_BALANCE_TBL, allBalances.getBsq()).build());
return balances.toString();
}

protected static String encodeToHex(String s) {
return Utilities.bytesAsHexString(s.getBytes(UTF_8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package bisq.apitest.method.offer;

import bisq.proto.grpc.BsqSwapOfferInfo;
import bisq.proto.grpc.OfferInfo;

import protobuf.PaymentAccount;

import java.math.BigDecimal;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

Expand All @@ -36,11 +40,13 @@
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.bobdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.cli.table.builder.TableType.OFFER_TBL;
import static bisq.common.util.MathUtils.exactMultiply;



import bisq.apitest.method.MethodTest;
import bisq.cli.table.builder.TableBuilder;

@Slf4j
public abstract class AbstractOfferTest extends MethodTest {
Expand All @@ -52,8 +58,11 @@ public abstract class AbstractOfferTest extends MethodTest {
@Setter
protected static boolean isLongRunningTest;

protected static PaymentAccount alicesBsqAcct;
protected static PaymentAccount bobsBsqAcct;
protected static PaymentAccount alicesBsqSwapAcct;
protected static PaymentAccount bobsBsqSwapAcct;
// TODO Deprecate legacy BSQ accounts when no longer in use.
protected static PaymentAccount alicesLegacyBsqAcct;
protected static PaymentAccount bobsLegacyBsqAcct;

@BeforeAll
public static void setUp() {
Expand All @@ -64,17 +73,9 @@ public static void setUp() {
arbdaemon,
alicedaemon,
bobdaemon);
}

public static void createBsqSwapBsqPaymentAccounts() {
alicesBsqAcct = aliceClient.createCryptoCurrencyPaymentAccount("Alice's BsqSwap Account",
BSQ,
aliceClient.getUnusedBsqAddress(), // TODO refactor, bsq address not needed for atom acct
false);
bobsBsqAcct = bobClient.createCryptoCurrencyPaymentAccount("Bob's BsqSwap Account",
BSQ,
bobClient.getUnusedBsqAddress(), // TODO refactor, bsq address not needed for atom acct
false);
initSwapPaymentAccounts();
createLegacyBsqPaymentAccounts();
}

// Mkt Price Margin value of offer returned from server is scaled down by 10^-2.
Expand Down Expand Up @@ -105,13 +106,31 @@ public static void createBsqSwapBsqPaymentAccounts() {
return priceAsBigDecimal.toPlainString();
};

protected final Function<OfferInfo, String> toOfferTable = (offer) ->
new TableBuilder(OFFER_TBL, offer).build().toString();

protected final Function<List<OfferInfo>, String> toOffersTable = (offers) ->
new TableBuilder(OFFER_TBL, offers).build().toString();

// TODO
protected final Function<BsqSwapOfferInfo, String> toBsqSwapOfferTable = (offer) ->
new TableBuilder(OFFER_TBL, offer).build().toString();


public static void initSwapPaymentAccounts() {
// A bot may not know what the default 'BSQ Swap' account name is,
// but API test cases do: the value of the i18n property 'BSQ_SWAP'.
alicesBsqSwapAcct = aliceClient.getPaymentAccount("BSQ Swap");
bobsBsqSwapAcct = bobClient.getPaymentAccount("BSQ Swap");
}

@SuppressWarnings("ConstantConditions")
public static void createBsqPaymentAccounts() {
alicesBsqAcct = aliceClient.createCryptoCurrencyPaymentAccount("Alice's BSQ Account",
public static void createLegacyBsqPaymentAccounts() {
alicesLegacyBsqAcct = aliceClient.createCryptoCurrencyPaymentAccount("Alice's Legacy BSQ Account",
BSQ,
aliceClient.getUnusedBsqAddress(),
false);
bobsBsqAcct = bobClient.createCryptoCurrencyPaymentAccount("Bob's BSQ Account",
bobsLegacyBsqAcct = bobClient.createCryptoCurrencyPaymentAccount("Bob's Legacy BSQ Account",
BSQ,
bobClient.getUnusedBsqAddress(),
false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.cli.TableFormat.formatBalancesTbls;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand All @@ -46,7 +45,6 @@ public class BsqSwapOfferTest extends AbstractOfferTest {
@BeforeAll
public static void setUp() {
AbstractOfferTest.setUp();
createBsqSwapBsqPaymentAccounts();
}

@BeforeEach
Expand Down Expand Up @@ -115,7 +113,7 @@ private void createBsqSwapOffer() {
1_000_000L,
1_000_000L,
"0.00005",
alicesBsqAcct.getId());
alicesBsqSwapAcct.getId());
log.debug("BsqSwap Sell BSQ (Buy BTC) OFFER:\n{}", bsqSwapOffer);
var newOfferId = bsqSwapOffer.getId();
assertNotEquals("", newOfferId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@

import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.cli.TableFormat.formatBalancesTbls;
import static bisq.cli.TableFormat.formatOfferTable;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand All @@ -53,7 +50,6 @@ public class CreateBSQOffersTest extends AbstractOfferTest {
@BeforeAll
public static void setUp() {
AbstractOfferTest.setUp();
createBsqPaymentAccounts();
}

@Test
Expand All @@ -68,9 +64,9 @@ public void testCreateBuy1BTCFor20KBSQOffer() {
100_000_000L,
"0.00005", // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.info("Sell BSQ (Buy BTC) OFFER:\n{}", formatOfferTable(singletonList(newOffer), BSQ));
log.debug("Sell BSQ (Buy BTC) OFFER:\n{}", toOfferTable.apply(newOffer));
assertTrue(newOffer.getIsMyOffer());
assertTrue(newOffer.getIsMyPendingOffer());

Expand All @@ -82,7 +78,7 @@ public void testCreateBuy1BTCFor20KBSQOffer() {
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -99,7 +95,7 @@ public void testCreateBuy1BTCFor20KBSQOffer() {
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -115,9 +111,9 @@ public void testCreateSell1BTCFor20KBSQOffer() {
100_000_000L,
"0.00005", // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.info("SELL 20K BSQ OFFER:\n{}", formatOfferTable(singletonList(newOffer), BSQ));
log.debug("SELL 20K BSQ OFFER:\n{}", toOfferTable.apply(newOffer));
assertTrue(newOffer.getIsMyOffer());
assertTrue(newOffer.getIsMyPendingOffer());

Expand All @@ -129,7 +125,7 @@ public void testCreateSell1BTCFor20KBSQOffer() {
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -146,7 +142,7 @@ public void testCreateSell1BTCFor20KBSQOffer() {
assertEquals(100_000_000L, newOffer.getAmount());
assertEquals(100_000_000L, newOffer.getMinAmount());
assertEquals(15_000_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -162,9 +158,9 @@ public void testCreateBuyBTCWith1To2KBSQOffer() {
5_000_000L,
"0.00005", // FIXED PRICE IN BTC sats FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.info("BUY 1-2K BSQ OFFER:\n{}", formatOfferTable(singletonList(newOffer), BSQ));
log.debug("BUY 1-2K BSQ OFFER:\n{}", toOfferTable.apply(newOffer));
assertTrue(newOffer.getIsMyOffer());
assertTrue(newOffer.getIsMyPendingOffer());

Expand All @@ -176,7 +172,7 @@ public void testCreateBuyBTCWith1To2KBSQOffer() {
assertEquals(10_000_000L, newOffer.getAmount());
assertEquals(5_000_000L, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -193,7 +189,7 @@ public void testCreateBuyBTCWith1To2KBSQOffer() {
assertEquals(10_000_000L, newOffer.getAmount());
assertEquals(5_000_000L, newOffer.getMinAmount());
assertEquals(1_500_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -209,9 +205,9 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
25_000_000L,
"0.00005", // FIXED PRICE IN BTC sats FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
alicesLegacyBsqAcct.getId(),
MAKER_FEE_CURRENCY_CODE);
log.info("SELL 5-10K BSQ OFFER:\n{}", formatOfferTable(singletonList(newOffer), BSQ));
log.debug("SELL 5-10K BSQ OFFER:\n{}", toOfferTable.apply(newOffer));
assertTrue(newOffer.getIsMyOffer());
assertTrue(newOffer.getIsMyPendingOffer());

Expand All @@ -223,7 +219,7 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
assertEquals(50_000_000L, newOffer.getAmount());
assertEquals(25_000_000L, newOffer.getMinAmount());
assertEquals(7_500_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -240,7 +236,7 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
assertEquals(50_000_000L, newOffer.getAmount());
assertEquals(25_000_000L, newOffer.getMinAmount());
assertEquals(7_500_000, newOffer.getBuyerSecurityDeposit());
assertEquals(alicesBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(alicesLegacyBsqAcct.getId(), newOffer.getPaymentAccountId());
assertEquals(BSQ, newOffer.getBaseCurrencyCode());
assertEquals(BTC, newOffer.getCounterCurrencyCode());
assertFalse(newOffer.getIsCurrencyForMakerFeeBtc());
Expand All @@ -250,18 +246,18 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
@Order(5)
public void testGetAllMyBsqOffers() {
List<OfferInfo> offers = aliceClient.getMyBsqOffersSortedByDate();
log.info("ALL ALICE'S BSQ OFFERS:\n{}", formatOfferTable(offers, BSQ));
log.debug("ALL ALICE'S BSQ OFFERS:\n{}", toOffersTable.apply(offers));
assertEquals(4, offers.size());
log.info("ALICE'S BALANCES\n{}", formatBalancesTbls(aliceClient.getBalances()));
log.debug("ALICE'S BALANCES\n{}", formatBalancesTbls(aliceClient.getBalances()));
}

@Test
@Order(6)
public void testGetAvailableBsqOffers() {
List<OfferInfo> offers = bobClient.getBsqOffersSortedByDate();
log.info("ALL BOB'S AVAILABLE BSQ OFFERS:\n{}", formatOfferTable(offers, BSQ));
log.debug("ALL BOB'S AVAILABLE BSQ OFFERS:\n{}", toOffersTable.apply(offers));
assertEquals(4, offers.size());
log.info("BOB'S BALANCES\n{}", formatBalancesTbls(bobClient.getBalances()));
log.debug("BOB'S BALANCES\n{}", formatBalancesTbls(bobClient.getBalances()));
}

private void genBtcBlockAndWaitForOfferPreparation() {
Expand Down
Loading

0 comments on commit 1578f45

Please sign in to comment.