Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api method createcryptopaymentacct #5356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

import bisq.cli.opts.ArgumentList;
import bisq.cli.opts.CancelOfferOptionParser;
import bisq.cli.opts.CreateCryptoCurrencyPaymentAcctOptionParser;
import bisq.cli.opts.CreateOfferOptionParser;
import bisq.cli.opts.CreatePaymentAcctOptionParser;
import bisq.cli.opts.GetAddressBalanceOptionParser;
Expand Down Expand Up @@ -517,6 +518,24 @@ public static void run(String[] args) {
out.println(formatPaymentAcctTbl(singletonList(paymentAccount)));
return;
}
case createcryptopaymentacct: {
var opts = new CreateCryptoCurrencyPaymentAcctOptionParser(args).parse();
if (opts.isForHelp()) {
out.println(client.getMethodHelp(method));
return;
}
var accountName = opts.getAccountName();
var currencyCode = opts.getCurrencyCode();
var address = opts.getAddress();
var isTradeInstant = opts.getIsTradeInstant();
var paymentAccount = client.createCryptoCurrencyPaymentAccount(accountName,
currencyCode,
address,
isTradeInstant);
out.println("payment account saved");
out.println(formatPaymentAcctTbl(singletonList(paymentAccount)));
return;
}
case getpaymentaccts: {
if (new SimpleMethodOptionParser(args).parse().isForHelp()) {
out.println(client.getMethodHelp(method));
Expand Down Expand Up @@ -676,7 +695,7 @@ private static void printHelp(OptionParser parser, @SuppressWarnings("SameParame
stream.println();
parser.printHelpOn(stream);
stream.println();
String rowFormat = "%-24s%-52s%s%n";
String rowFormat = "%-25s%-52s%s%n";
stream.format(rowFormat, "Method", "Params", "Description");
stream.format(rowFormat, "------", "------", "------------");
stream.format(rowFormat, getversion.name(), "", "Get server version");
Expand Down Expand Up @@ -727,7 +746,9 @@ private static void printHelp(OptionParser parser, @SuppressWarnings("SameParame
stream.format(rowFormat, getmyoffers.name(), "--direction=<buy|sell> \\", "Get my current offers");
stream.format(rowFormat, "", "--currency-code=<currency-code>", "");
stream.println();
stream.format(rowFormat, takeoffer.name(), "--offer-id=<offer-id> [--fee-currency=<btc|bsq>]", "Take offer with id");
stream.format(rowFormat, takeoffer.name(), "--offer-id=<offer-id> \\", "Take offer with id");
stream.format(rowFormat, "", "--payment-account=<payment-account-id>", "");
stream.format(rowFormat, "", "[--fee-currency=<btc|bsq>]", "");
stream.println();
stream.format(rowFormat, gettrade.name(), "--trade-id=<trade-id> \\", "Get trade summary or full contract");
stream.format(rowFormat, "", "[--show-contract=<true|false>]", "");
Expand All @@ -748,6 +769,11 @@ private static void printHelp(OptionParser parser, @SuppressWarnings("SameParame
stream.println();
stream.format(rowFormat, createpaymentacct.name(), "--payment-account-form=<path>", "Create a new payment account");
stream.println();
stream.format(rowFormat, createcryptopaymentacct.name(), "--account-name=<name> \\", "Create a new cryptocurrency payment account");
stream.format(rowFormat, "", "--currency-code=<bsq> \\", "");
stream.format(rowFormat, "", "--address=<bsq-address>", "");
stream.format(rowFormat, "", "--trade-instant=<true|false>", "");
stream.println();
stream.format(rowFormat, getpaymentaccts.name(), "", "Get user payment accounts");
stream.println();
stream.format(rowFormat, lockwallet.name(), "", "Remove wallet password from memory, locking the wallet");
Expand Down
51 changes: 48 additions & 3 deletions cli/src/main/java/bisq/cli/GrpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import bisq.proto.grpc.CancelOfferRequest;
import bisq.proto.grpc.ConfirmPaymentReceivedRequest;
import bisq.proto.grpc.ConfirmPaymentStartedRequest;
import bisq.proto.grpc.CreateCryptoCurrencyPaymentAccountRequest;
import bisq.proto.grpc.CreateOfferRequest;
import bisq.proto.grpc.CreatePaymentAccountRequest;
import bisq.proto.grpc.GetAddressBalanceRequest;
import bisq.proto.grpc.GetBalancesRequest;
import bisq.proto.grpc.GetCryptoCurrencyPaymentMethodsRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.GetMethodHelpRequest;
import bisq.proto.grpc.GetMyOfferRequest;
Expand Down Expand Up @@ -67,11 +69,11 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static protobuf.OfferPayload.Direction.BUY;
import static protobuf.OfferPayload.Direction.SELL;

Expand Down Expand Up @@ -228,7 +230,6 @@ public OfferInfo createMarketBasedPricedOffer(String direction,
makerFeeCurrencyCode);
}

// TODO make private, move to bottom of class
public OfferInfo createOffer(String direction,
String currencyCode,
long amount,
Expand Down Expand Up @@ -283,6 +284,12 @@ public List<OfferInfo> getOffers(String direction, String currencyCode) {
return grpcStubs.offersService.getOffers(request).getOffersList();
}

public List<OfferInfo> getBsqOffers(String direction) {
return getOffers(direction, "BTC").stream()
.filter(o -> o.getBaseCurrencyCode().equals("BSQ"))
.collect(toList());
}

public List<OfferInfo> getOffersSortedByDate(String currencyCode) {
ArrayList<OfferInfo> offers = new ArrayList<>();
offers.addAll(getOffers(BUY.name(), currencyCode));
Expand All @@ -295,6 +302,13 @@ public List<OfferInfo> getOffersSortedByDate(String direction, String currencyCo
return offers.isEmpty() ? offers : sortOffersByDate(offers);
}

public List<OfferInfo> getBsqOffersSortedByDate() {
ArrayList<OfferInfo> offers = new ArrayList<>();
offers.addAll(getBsqOffers(BUY.name()));
offers.addAll(getBsqOffers(SELL.name()));
return sortOffersByDate(offers);
}

public List<OfferInfo> getMyOffers(String direction, String currencyCode) {
var request = GetMyOffersRequest.newBuilder()
.setDirection(direction)
Expand All @@ -303,6 +317,12 @@ public List<OfferInfo> getMyOffers(String direction, String currencyCode) {
return grpcStubs.offersService.getMyOffers(request).getOffersList();
}

public List<OfferInfo> getMyBsqOffers(String direction) {
return getMyOffers(direction, "BTC").stream()
.filter(o -> o.getBaseCurrencyCode().equals("BSQ"))
.collect(toList());
}

public List<OfferInfo> getMyOffersSortedByDate(String direction, String currencyCode) {
var offers = getMyOffers(direction, currencyCode);
return offers.isEmpty() ? offers : sortOffersByDate(offers);
Expand All @@ -315,6 +335,13 @@ public List<OfferInfo> getMyOffersSortedByDate(String currencyCode) {
return sortOffersByDate(offers);
}

public List<OfferInfo> getMyBsqOffersSortedByDate() {
ArrayList<OfferInfo> offers = new ArrayList<>();
offers.addAll(getMyBsqOffers(BUY.name()));
offers.addAll(getMyBsqOffers(SELL.name()));
return sortOffersByDate(offers);
}

public OfferInfo getMostRecentOffer(String direction, String currencyCode) {
List<OfferInfo> offers = getOffersSortedByDate(direction, currencyCode);
return offers.isEmpty() ? null : offers.get(offers.size() - 1);
Expand All @@ -323,7 +350,7 @@ public OfferInfo getMostRecentOffer(String direction, String currencyCode) {
public List<OfferInfo> sortOffersByDate(List<OfferInfo> offerInfoList) {
return offerInfoList.stream()
.sorted(comparing(OfferInfo::getDate))
.collect(Collectors.toList());
.collect(toList());
}

public TakeOfferReply getTakeOfferReply(String offerId, String paymentAccountId, String takerFeeCurrencyCode) {
Expand Down Expand Up @@ -404,6 +431,24 @@ public List<PaymentAccount> getPaymentAccounts() {
return grpcStubs.paymentAccountsService.getPaymentAccounts(request).getPaymentAccountsList();
}

public PaymentAccount createCryptoCurrencyPaymentAccount(String accountName,
String currencyCode,
String address,
boolean tradeInstant) {
var request = CreateCryptoCurrencyPaymentAccountRequest.newBuilder()
.setAccountName(accountName)
.setCurrencyCode(currencyCode)
.setAddress(address)
.setTradeInstant(tradeInstant)
.build();
return grpcStubs.paymentAccountsService.createCryptoCurrencyPaymentAccount(request).getPaymentAccount();
}

public List<PaymentMethod> getCryptoPaymentMethods() {
var request = GetCryptoCurrencyPaymentMethodsRequest.newBuilder().build();
return grpcStubs.paymentAccountsService.getCryptoCurrencyPaymentMethods(request).getPaymentMethodsList();
}

public void lockWallet() {
var request = LockWalletRequest.newBuilder().build();
grpcStubs.walletsService.lockWallet(request);
Expand Down
1 change: 1 addition & 0 deletions cli/src/main/java/bisq/cli/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum Method {
confirmpaymentstarted,
createoffer,
createpaymentacct,
createcryptopaymentacct,
getaddressbalance,
getbalance,
getbtcprice,
Expand Down
1 change: 1 addition & 0 deletions cli/src/main/java/bisq/cli/opts/ArgumentList.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ boolean hasMore() {
return currentIndex < arguments.length;
}

@SuppressWarnings("UnusedReturnValue")
String next() {
return arguments[currentIndex++];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.cli.opts;


import joptsimple.OptionSpec;

import static bisq.cli.opts.OptLabel.OPT_ACCOUNT_NAME;
import static bisq.cli.opts.OptLabel.OPT_ADDRESS;
import static bisq.cli.opts.OptLabel.OPT_CURRENCY_CODE;
import static bisq.cli.opts.OptLabel.OPT_TRADE_INSTANT;

public class CreateCryptoCurrencyPaymentAcctOptionParser extends AbstractMethodOptionParser implements MethodOpts {

final OptionSpec<String> accountNameOpt = parser.accepts(OPT_ACCOUNT_NAME, "crypto currency account name")
.withRequiredArg();

final OptionSpec<String> currencyCodeOpt = parser.accepts(OPT_CURRENCY_CODE, "crypto currency code (bsq only)")
.withRequiredArg();

final OptionSpec<String> addressOpt = parser.accepts(OPT_ADDRESS, "bsq address")
.withRequiredArg();

final OptionSpec<Boolean> tradeInstantOpt = parser.accepts(OPT_TRADE_INSTANT, "create trade instant account")
.withOptionalArg()
.ofType(boolean.class)
.defaultsTo(Boolean.FALSE);

public CreateCryptoCurrencyPaymentAcctOptionParser(String[] args) {
super(args);
}

public CreateCryptoCurrencyPaymentAcctOptionParser parse() {
super.parse();

// Short circuit opt validation if user just wants help.
if (options.has(helpOpt))
return this;

if (!options.has(accountNameOpt) || options.valueOf(accountNameOpt).isEmpty())
throw new IllegalArgumentException("no payment account name specified");

if (!options.has(currencyCodeOpt) || options.valueOf(currencyCodeOpt).isEmpty())
throw new IllegalArgumentException("no currency code specified");

if (!options.valueOf(currencyCodeOpt).equalsIgnoreCase("bsq"))
throw new IllegalArgumentException("api only supports bsq crypto currency payment accounts");

if (!options.has(addressOpt) || options.valueOf(addressOpt).isEmpty())
throw new IllegalArgumentException("no bsq address specified");

return this;
}

public String getAccountName() {
return options.valueOf(accountNameOpt);
}

public String getCurrencyCode() {
return options.valueOf(currencyCodeOpt);
}

public String getAddress() {
return options.valueOf(addressOpt);
}

public boolean getIsTradeInstant() {
return options.valueOf(tradeInstantOpt);
}
}
1 change: 0 additions & 1 deletion cli/src/main/java/bisq/cli/opts/MethodOpts.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ public interface MethodOpts {
MethodOpts parse();

boolean isForHelp();

}
2 changes: 2 additions & 0 deletions cli/src/main/java/bisq/cli/opts/OptLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* CLI opt label definitions.
*/
public class OptLabel {
public final static String OPT_ACCOUNT_NAME = "account-name";
public final static String OPT_ADDRESS = "address";
public final static String OPT_AMOUNT = "amount";
public final static String OPT_CURRENCY_CODE = "currency-code";
Expand All @@ -43,6 +44,7 @@ public class OptLabel {
public final static String OPT_SECURITY_DEPOSIT = "security-deposit";
public final static String OPT_SHOW_CONTRACT = "show-contract";
public final static String OPT_TRADE_ID = "trade-id";
public final static String OPT_TRADE_INSTANT = "trade-instant";
public final static String OPT_TIMEOUT = "timeout";
public final static String OPT_TRANSACTION_ID = "transaction-id";
public final static String OPT_TX_FEE_RATE = "tx-fee-rate";
Expand Down
Loading