Adjust CLI classes to new console output api

This commit is contained in:
ghubstan 2021-11-07 13:59:58 -03:00
parent 08d16e6040
commit 0341104823
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
5 changed files with 69 additions and 20 deletions

View File

@ -34,6 +34,8 @@ import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -41,13 +43,13 @@ import lombok.extern.slf4j.Slf4j;
import static bisq.cli.CurrencyFormat.*; import static bisq.cli.CurrencyFormat.*;
import static bisq.cli.Method.*; import static bisq.cli.Method.*;
import static bisq.cli.TableFormat.*;
import static bisq.cli.opts.OptLabel.*; import static bisq.cli.opts.OptLabel.*;
import static bisq.cli.table.builder.TableType.*;
import static java.lang.String.format; import static java.lang.String.format;
import static java.lang.System.err; import static java.lang.System.err;
import static java.lang.System.exit; import static java.lang.System.exit;
import static java.lang.System.out; import static java.lang.System.out;
import static java.util.Collections.singletonList; import static java.math.BigDecimal.ZERO;
@ -76,6 +78,7 @@ import bisq.cli.opts.TakeOfferOptionParser;
import bisq.cli.opts.UnlockWalletOptionParser; import bisq.cli.opts.UnlockWalletOptionParser;
import bisq.cli.opts.VerifyBsqSentToAddressOptionParser; import bisq.cli.opts.VerifyBsqSentToAddressOptionParser;
import bisq.cli.opts.WithdrawFundsOptionParser; import bisq.cli.opts.WithdrawFundsOptionParser;
import bisq.cli.table.builder.TableBuilder;
/** /**
* A command-line client for the Bisq gRPC API. * A command-line client for the Bisq gRPC API.
@ -167,15 +170,19 @@ public class CliMain {
var balances = client.getBalances(currencyCode); var balances = client.getBalances(currencyCode);
switch (currencyCode.toUpperCase()) { switch (currencyCode.toUpperCase()) {
case "BSQ": case "BSQ":
out.println(formatBsqBalanceInfoTbl(balances.getBsq())); new TableBuilder(BSQ_BALANCE_TBL, balances.getBsq()).build().print(out);
break; break;
case "BTC": case "BTC":
out.println(formatBtcBalanceInfoTbl(balances.getBtc())); new TableBuilder(BTC_BALANCE_TBL, balances.getBtc()).build().print(out);
break; break;
case "": case "":
default: default: {
out.println(formatBalancesTbls(balances)); out.println("BTC");
new TableBuilder(BTC_BALANCE_TBL, balances.getBtc()).build().print(out);
out.println("BSQ");
new TableBuilder(BSQ_BALANCE_TBL, balances.getBsq()).build().print(out);
break; break;
}
} }
return; return;
} }
@ -187,7 +194,7 @@ public class CliMain {
} }
var address = opts.getAddress(); var address = opts.getAddress();
var addressBalance = client.getAddressBalance(address); var addressBalance = client.getAddressBalance(address);
out.println(formatAddressBalanceTbl(singletonList(addressBalance))); new TableBuilder(ADDRESS_BALANCE_TBL, addressBalance).build().print(out);
return; return;
} }
case getbtcprice: { case getbtcprice: {
@ -207,7 +214,7 @@ public class CliMain {
return; return;
} }
var fundingAddresses = client.getFundingAddresses(); var fundingAddresses = client.getFundingAddresses();
out.println(formatAddressBalanceTbl(fundingAddresses)); new TableBuilder(ADDRESS_BALANCE_TBL, fundingAddresses).build().print(out);
return; return;
} }
case getunusedbsqaddress: { case getunusedbsqaddress: {
@ -316,7 +323,7 @@ public class CliMain {
} }
var txId = opts.getTxId(); var txId = opts.getTxId();
var tx = client.getTransaction(txId); var tx = client.getTransaction(txId);
out.println(TransactionFormat.format(tx)); new TableBuilder(TRANSACTION_TBL, tx).build().print(out);
return; return;
} }
case createoffer: { case createoffer: {
@ -347,7 +354,7 @@ public class CliMain {
paymentAcctId, paymentAcctId,
makerFeeCurrencyCode, makerFeeCurrencyCode,
triggerPrice); triggerPrice);
out.println(formatOfferTable(singletonList(offer), currencyCode)); new TableBuilder(OFFER_TBL, offer).build().print(out);
return; return;
} }
case editoffer: { case editoffer: {
@ -360,7 +367,7 @@ public class CliMain {
var fixedPrice = opts.getFixedPrice(); var fixedPrice = opts.getFixedPrice();
var isUsingMktPriceMargin = opts.isUsingMktPriceMargin(); var isUsingMktPriceMargin = opts.isUsingMktPriceMargin();
var marketPriceMargin = opts.getMktPriceMarginAsBigDecimal(); var marketPriceMargin = opts.getMktPriceMarginAsBigDecimal();
var triggerPrice = toInternalFiatPrice(opts.getTriggerPriceAsBigDecimal()); var triggerPrice = toInternalTriggerPrice(client, offerId, opts.getTriggerPriceAsBigDecimal());
var enable = opts.getEnableAsSignedInt(); var enable = opts.getEnableAsSignedInt();
var editOfferType = opts.getOfferEditType(); var editOfferType = opts.getOfferEditType();
client.editOffer(offerId, client.editOffer(offerId,
@ -392,7 +399,7 @@ public class CliMain {
} }
var offerId = opts.getOfferId(); var offerId = opts.getOfferId();
var offer = client.getOffer(offerId); var offer = client.getOffer(offerId);
out.println(formatOfferTable(singletonList(offer), offer.getCounterCurrencyCode())); new TableBuilder(OFFER_TBL, offer).build().print(out);
return; return;
} }
case getmyoffer: { case getmyoffer: {
@ -403,7 +410,7 @@ public class CliMain {
} }
var offerId = opts.getOfferId(); var offerId = opts.getOfferId();
var offer = client.getMyOffer(offerId); var offer = client.getMyOffer(offerId);
out.println(formatOfferTable(singletonList(offer), offer.getCounterCurrencyCode())); new TableBuilder(OFFER_TBL, offer).build().print(out);
return; return;
} }
case getoffers: { case getoffers: {
@ -418,7 +425,7 @@ public class CliMain {
if (offers.isEmpty()) if (offers.isEmpty())
out.printf("no %s %s offers found%n", direction, currencyCode); out.printf("no %s %s offers found%n", direction, currencyCode);
else else
out.println(formatOfferTable(offers, currencyCode)); new TableBuilder(OFFER_TBL, offers).build().print(out);
return; return;
} }
@ -434,7 +441,7 @@ public class CliMain {
if (offers.isEmpty()) if (offers.isEmpty())
out.printf("no %s %s offers found%n", direction, currencyCode); out.printf("no %s %s offers found%n", direction, currencyCode);
else else
out.println(formatOfferTable(offers, currencyCode)); new TableBuilder(OFFER_TBL, offers).build().print(out);
return; return;
} }
@ -464,7 +471,7 @@ public class CliMain {
if (showContract) if (showContract)
out.println(trade.getContractAsJson()); out.println(trade.getContractAsJson());
else else
out.println(TradeFormat.format(trade)); new TableBuilder(TRADE_DETAIL_TBL, trade).build().print(out);
return; return;
} }
@ -556,11 +563,12 @@ public class CliMain {
} }
var paymentAccount = client.createPaymentAccount(jsonString); var paymentAccount = client.createPaymentAccount(jsonString);
out.println("payment account saved"); out.println("payment account saved");
out.println(formatPaymentAcctTbl(singletonList(paymentAccount))); new TableBuilder(PAYMENT_ACCOUNT_TBL, paymentAccount).build().print(out);
return; return;
} }
case createcryptopaymentacct: { case createcryptopaymentacct: {
var opts = new CreateCryptoCurrencyPaymentAcctOptionParser(args).parse(); var opts =
new CreateCryptoCurrencyPaymentAcctOptionParser(args).parse();
if (opts.isForHelp()) { if (opts.isForHelp()) {
out.println(client.getMethodHelp(method)); out.println(client.getMethodHelp(method));
return; return;
@ -574,7 +582,7 @@ public class CliMain {
address, address,
isTradeInstant); isTradeInstant);
out.println("payment account saved"); out.println("payment account saved");
out.println(formatPaymentAcctTbl(singletonList(paymentAccount))); new TableBuilder(PAYMENT_ACCOUNT_TBL, paymentAccount).build().print(out);
return; return;
} }
case getpaymentaccts: { case getpaymentaccts: {
@ -584,7 +592,7 @@ public class CliMain {
} }
var paymentAccounts = client.getPaymentAccounts(); var paymentAccounts = client.getPaymentAccounts();
if (paymentAccounts.size() > 0) if (paymentAccounts.size() > 0)
out.println(formatPaymentAcctTbl(paymentAccounts)); new TableBuilder(PAYMENT_ACCOUNT_TBL, paymentAccounts).build().print(out);
else else
out.println("no payment accounts are saved"); out.println("no payment accounts are saved");
@ -708,6 +716,25 @@ public class CliMain {
} }
} }
private static long toInternalTriggerPrice(GrpcClient client,
String offerId,
BigDecimal unscaledTriggerPrice) {
if (unscaledTriggerPrice.compareTo(ZERO) >= 0) {
// Unfortunately, the EditOffer proto triggerPrice field was declared as
// a long instead of a string, so the CLI has to look at the offer to know
// how to scale the trigger-price (for a fiat or altcoin offer) param sent
// to the server in its 'editoffer' request. That means a preliminary round
// trip to the server: a 'getmyoffer' request.
var offer = client.getMyOffer(offerId);
if (offer.getCounterCurrencyCode().equals("BTC"))
return toInternalCryptoCurrencyPrice(unscaledTriggerPrice);
else
return toInternalFiatPrice(unscaledTriggerPrice);
} else {
return 0L;
}
}
private static File saveFileToDisk(String prefix, private static File saveFileToDisk(String prefix,
@SuppressWarnings("SameParameterValue") String suffix, @SuppressWarnings("SameParameterValue") String suffix,
String text) { String text) {

View File

@ -327,6 +327,10 @@ public final class GrpcClient {
return offersServiceRequest.getMyOffersSortedByDate(currencyCode); return offersServiceRequest.getMyOffersSortedByDate(currencyCode);
} }
public List<OfferInfo> getMyCryptoCurrencyOffersSortedByDate(String currencyCode) {
return offersServiceRequest.getMyCryptoCurrencyOffersSortedByDate(currencyCode);
}
public List<OfferInfo> getMyBsqOffersSortedByDate() { public List<OfferInfo> getMyBsqOffersSortedByDate() {
return offersServiceRequest.getMyBsqOffersSortedByDate(); return offersServiceRequest.getMyBsqOffersSortedByDate();
} }

View File

@ -60,6 +60,7 @@ public class OffersServiceRequest {
this.grpcStubs = grpcStubs; this.grpcStubs = grpcStubs;
} }
@SuppressWarnings("unused")
public OfferInfo createFixedPricedOffer(String direction, public OfferInfo createFixedPricedOffer(String direction,
String currencyCode, String currencyCode,
long amount, long amount,
@ -81,6 +82,7 @@ public class OffersServiceRequest {
0 /* no trigger price */); 0 /* no trigger price */);
} }
@SuppressWarnings("unused")
public OfferInfo createMarketBasedPricedOffer(String direction, public OfferInfo createMarketBasedPricedOffer(String direction,
String currencyCode, String currencyCode,
long amount, long amount,
@ -328,6 +330,13 @@ public class OffersServiceRequest {
return sortOffersByDate(offers); return sortOffersByDate(offers);
} }
public List<OfferInfo> getMyCryptoCurrencyOffersSortedByDate(String currencyCode) {
ArrayList<OfferInfo> offers = new ArrayList<>();
offers.addAll(getMyCryptoCurrencyOffers(BUY.name(), currencyCode));
offers.addAll(getMyCryptoCurrencyOffers(SELL.name(), currencyCode));
return sortOffersByDate(offers);
}
public List<OfferInfo> getMyBsqOffersSortedByDate() { public List<OfferInfo> getMyBsqOffersSortedByDate() {
ArrayList<OfferInfo> offers = new ArrayList<>(); ArrayList<OfferInfo> offers = new ArrayList<>();
offers.addAll(getMyCryptoCurrencyOffers(BUY.name(), "BSQ")); offers.addAll(getMyCryptoCurrencyOffers(BUY.name(), "BSQ"));

View File

@ -74,6 +74,7 @@ public class TradesServiceRequest {
var request = ConfirmPaymentStartedRequest.newBuilder() var request = ConfirmPaymentStartedRequest.newBuilder()
.setTradeId(tradeId) .setTradeId(tradeId)
.build(); .build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.tradesService.confirmPaymentStarted(request); grpcStubs.tradesService.confirmPaymentStarted(request);
} }
@ -81,6 +82,7 @@ public class TradesServiceRequest {
var request = ConfirmPaymentReceivedRequest.newBuilder() var request = ConfirmPaymentReceivedRequest.newBuilder()
.setTradeId(tradeId) .setTradeId(tradeId)
.build(); .build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.tradesService.confirmPaymentReceived(request); grpcStubs.tradesService.confirmPaymentReceived(request);
} }
@ -88,6 +90,7 @@ public class TradesServiceRequest {
var request = KeepFundsRequest.newBuilder() var request = KeepFundsRequest.newBuilder()
.setTradeId(tradeId) .setTradeId(tradeId)
.build(); .build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.tradesService.keepFunds(request); grpcStubs.tradesService.keepFunds(request);
} }
@ -97,6 +100,7 @@ public class TradesServiceRequest {
.setAddress(address) .setAddress(address)
.setMemo(memo) .setMemo(memo)
.build(); .build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.tradesService.withdrawFunds(request); grpcStubs.tradesService.withdrawFunds(request);
} }
} }

View File

@ -161,6 +161,7 @@ public class WalletsServiceRequest {
public void lockWallet() { public void lockWallet() {
var request = LockWalletRequest.newBuilder().build(); var request = LockWalletRequest.newBuilder().build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.lockWallet(request); grpcStubs.walletsService.lockWallet(request);
} }
@ -168,18 +169,21 @@ public class WalletsServiceRequest {
var request = UnlockWalletRequest.newBuilder() var request = UnlockWalletRequest.newBuilder()
.setPassword(walletPassword) .setPassword(walletPassword)
.setTimeout(timeout).build(); .setTimeout(timeout).build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.unlockWallet(request); grpcStubs.walletsService.unlockWallet(request);
} }
public void removeWalletPassword(String walletPassword) { public void removeWalletPassword(String walletPassword) {
var request = RemoveWalletPasswordRequest.newBuilder() var request = RemoveWalletPasswordRequest.newBuilder()
.setPassword(walletPassword).build(); .setPassword(walletPassword).build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.removeWalletPassword(request); grpcStubs.walletsService.removeWalletPassword(request);
} }
public void setWalletPassword(String walletPassword) { public void setWalletPassword(String walletPassword) {
var request = SetWalletPasswordRequest.newBuilder() var request = SetWalletPasswordRequest.newBuilder()
.setPassword(walletPassword).build(); .setPassword(walletPassword).build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.setWalletPassword(request); grpcStubs.walletsService.setWalletPassword(request);
} }
@ -187,6 +191,7 @@ public class WalletsServiceRequest {
var request = SetWalletPasswordRequest.newBuilder() var request = SetWalletPasswordRequest.newBuilder()
.setPassword(oldWalletPassword) .setPassword(oldWalletPassword)
.setNewPassword(newWalletPassword).build(); .setNewPassword(newWalletPassword).build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.setWalletPassword(request); grpcStubs.walletsService.setWalletPassword(request);
} }
} }