Add new api CLI method 'getbtcprice'

The server impl was there, but it is now needed by the trading
sim scripts (CLI) to get the price from the Bisq server instead
of the feed.  (The server does not request prices more than
once a minute.)
This commit is contained in:
ghubstan 2021-01-20 15:01:16 -03:00
parent 3244db12c4
commit ced422e930
No known key found for this signature in database
GPG key ID: E35592D6800A861E
5 changed files with 108 additions and 0 deletions

View file

@ -40,6 +40,7 @@ import bisq.proto.grpc.GetUnusedBsqAddressRequest;
import bisq.proto.grpc.GetVersionRequest;
import bisq.proto.grpc.KeepFundsRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.MarketPriceRequest;
import bisq.proto.grpc.OfferInfo;
import bisq.proto.grpc.RegisterDisputeAgentRequest;
import bisq.proto.grpc.RemoveWalletPasswordRequest;
@ -75,6 +76,7 @@ import java.util.List;
import lombok.extern.slf4j.Slf4j;
import static bisq.cli.CurrencyFormat.formatMarketPrice;
import static bisq.cli.CurrencyFormat.formatTxFeeRateInfo;
import static bisq.cli.CurrencyFormat.toSatoshis;
import static bisq.cli.CurrencyFormat.toSecurityDepositAsPct;
@ -98,6 +100,7 @@ import bisq.cli.opts.CancelOfferOptionParser;
import bisq.cli.opts.CreateOfferOptionParser;
import bisq.cli.opts.CreatePaymentAcctOptionParser;
import bisq.cli.opts.GetAddressBalanceOptionParser;
import bisq.cli.opts.GetBTCMarketPriceOptionParser;
import bisq.cli.opts.GetBalanceOptionParser;
import bisq.cli.opts.GetOfferOptionParser;
import bisq.cli.opts.GetOffersOptionParser;
@ -189,6 +192,7 @@ public class CliMain {
var helpService = grpcStubs.helpService;
var offersService = grpcStubs.offersService;
var paymentAccountsService = grpcStubs.paymentAccountsService;
var priceService = grpcStubs.priceService;
var tradesService = grpcStubs.tradesService;
var versionService = grpcStubs.versionService;
var walletsService = grpcStubs.walletsService;
@ -243,6 +247,20 @@ public class CliMain {
out.println(formatAddressBalanceTbl(singletonList(reply.getAddressBalanceInfo())));
return;
}
case getbtcprice: {
var opts = new GetBTCMarketPriceOptionParser(args).parse();
if (opts.isForHelp()) {
out.println(getMethodHelp(helpService, method));
return;
}
var currencyCode = opts.getCurrencyCode();
var request = MarketPriceRequest.newBuilder()
.setCurrencyCode(currencyCode)
.build();
var reply = priceService.getMarketPrice(request);
out.println(formatMarketPrice(reply.getPrice()));
return;
}
case getfundingaddresses: {
if (new SimpleMethodOptionParser(args).parse().isForHelp()) {
out.println(getMethodHelp(helpService, method));
@ -802,6 +820,8 @@ public class CliMain {
stream.println();
stream.format(rowFormat, getaddressbalance.name(), "--address=<btc-address>", "Get server wallet address balance");
stream.println();
stream.format(rowFormat, getbtcprice.name(), "--currency-code=<currency-code>", "Get current market btc price");
stream.println();
stream.format(rowFormat, getfundingaddresses.name(), "", "Get BTC funding addresses");
stream.println();
stream.format(rowFormat, getunusedbsqaddress.name(), "", "Get unused BSQ address");

View file

@ -77,6 +77,11 @@ public class CurrencyFormat {
: formatOfferVolume(volume);
}
static String formatMarketPrice(double price) {
NUMBER_FORMAT.setMinimumFractionDigits(4);
return NUMBER_FORMAT.format(price);
}
static String formatOfferPrice(long price) {
NUMBER_FORMAT.setMaximumFractionDigits(4);
NUMBER_FORMAT.setMinimumFractionDigits(4);

View file

@ -28,6 +28,7 @@ public enum Method {
createpaymentacct,
getaddressbalance,
getbalance,
getbtcprice,
getfundingaddresses,
getmyoffer,
getmyoffers,

View file

@ -0,0 +1,52 @@
/*
* 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_CURRENCY_CODE;
import static joptsimple.internal.Strings.EMPTY;
public class GetBTCMarketPriceOptionParser extends AbstractMethodOptionParser implements MethodOpts {
final OptionSpec<String> currencyCodeOpt = parser.accepts(OPT_CURRENCY_CODE, "currency-code")
.withRequiredArg()
.defaultsTo(EMPTY);
public GetBTCMarketPriceOptionParser(String[] args) {
super(args);
}
public GetBTCMarketPriceOptionParser parse() {
super.parse();
// Short circuit opt validation if user just wants help.
if (options.has(helpOpt))
return this;
if (!options.has(currencyCodeOpt))
throw new IllegalArgumentException("no currency code specified");
return this;
}
public String getCurrencyCode() {
return options.valueOf(currencyCodeOpt);
}
}

View file

@ -0,0 +1,30 @@
getbtcprice
NAME
----
getbtcprice - get current btc market price
SYNOPSIS
--------
getbtcprice
--currency-code=<eur|usd>
DESCRIPTION
-----------
Returns the current market BTC price for the given currency-code.
OPTIONS
-------
--currency-code
The three letter code for the fiat currency code, e.g., EUR, USD, BRL, ...
EXAMPLES
--------
Get the current BTC market price in Euros:
$ ./bisq-cli --password=xyz --port=9998 getbtcprice --currency-code=eur
Get the current BTC market price in Brazilian Reais:
$ ./bisq-cli --password=xyz --port=9998 getbtcprice --currency-code=brl