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 trade simulation scripts #5093

Merged
merged 41 commits into from Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5538914
Add line break
ghubstan Jan 16, 2021
ef4acc0
Make opt description generic
ghubstan Jan 16, 2021
629f408
Add method help docs
ghubstan Jan 16, 2021
03f6c2a
Add method help docs
ghubstan Jan 16, 2021
d14fc40
Fix help text typos
ghubstan Jan 16, 2021
68092a0
Add --memo opt to help text
ghubstan Jan 16, 2021
cde9a6d
Add method help docs
ghubstan Jan 16, 2021
dab65e7
Fix typo
ghubstan Jan 16, 2021
a133b0d
Add method help doc
ghubstan Jan 16, 2021
9efeee8
Explain example
ghubstan Jan 16, 2021
13d0d3e
Add method help doc
ghubstan Jan 16, 2021
44c1192
Add method help docs
ghubstan Jan 17, 2021
04657d9
Explain example
ghubstan Jan 17, 2021
8907d6b
Keep CLI method enum members in alphabetical order
ghubstan Jan 17, 2021
fbc31a5
Merge branch 'master' into 01-add-method-help-docs
ghubstan Jan 19, 2021
873c661
Add api trade simulation scripts
ghubstan Jan 19, 2021
93c3735
Fix bash bugs & style problems for codacy
ghubstan Jan 19, 2021
48a326b
Add env requirement and usage comments
ghubstan Jan 19, 2021
cf419d2
Make price request frequency configurable
ghubstan Jan 19, 2021
4eabb98
Fix indentation
ghubstan Jan 20, 2021
71ffa0f
Explain that paid tx fees are forfeited
ghubstan Jan 20, 2021
8675841
Fix punctuation
ghubstan Jan 20, 2021
fce3aab
Improve description
ghubstan Jan 20, 2021
031b18e
Fix typo
ghubstan Jan 20, 2021
11096d5
Fix tx-fee-rate opt description
ghubstan Jan 20, 2021
72731e5
Add parens around (sats/byte)
ghubstan Jan 20, 2021
67eed01
Fix tx-fee-rate opt description
ghubstan Jan 20, 2021
73c6b3d
Fix typo, group mutually exclusive opts around '||'
ghubstan Jan 20, 2021
6c322d4
Put $N variable references in double quotes
ghubstan Jan 20, 2021
0c06abd
Replace shortform '. with longform 'source'
ghubstan Jan 20, 2021
8b4938a
Do not export vars from lowest level child script
ghubstan Jan 20, 2021
cb96f27
Remove uneeded ${curlies}
ghubstan Jan 20, 2021
078f010
Remove api dev/test log statement
ghubstan Jan 20, 2021
3244db1
Print current market price just before offer is created
ghubstan Jan 20, 2021
ced422e
Add new api CLI method 'getbtcprice'
ghubstan Jan 20, 2021
20e1649
Get current mkt price from server, not directly from feed
ghubstan Jan 20, 2021
7fa61c9
Remove deprecated python script
ghubstan Jan 20, 2021
78d0024
Re-add exports to make codacy happy
ghubstan Jan 20, 2021
184ffd1
Enclose $vars in double quotes
ghubstan Jan 20, 2021
d18b2d5
Run an async price feed request when CLI needs market price
ghubstan Jan 20, 2021
b2d8faf
Log price feed request warnings, do not throw to CLI
ghubstan Jan 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 0 additions & 22 deletions apitest/scripts/getmktprice.py

This file was deleted.

17 changes: 14 additions & 3 deletions apitest/scripts/limit-order-simulation.sh
Expand Up @@ -93,8 +93,9 @@ while : ; do
break
fi

CURRENT_PRICE=$(getcurrentprice "${CURRENCY_CODE}")
printdate "Current Price: ${CURRENT_PRICE} ${CURRENCY_CODE}"
CURRENT_PRICE=$(getcurrentprice "$ALICE_PORT" "$CURRENCY_CODE")
exitoncommandalert $?
printdate "Current Market Price: $CURRENT_PRICE"

if [ "$DIRECTION" = "BUY" ] && [ "$CURRENT_PRICE" -le "$LIMIT_PRICE" ]; then
printdate "Limit price reached."
Expand Down Expand Up @@ -129,7 +130,17 @@ OFFER_ID=$(createoffer "${CMD}")
exitoncommandalert $?
printdate "ALICE: Created offer with id: ${OFFER_ID}."
printbreak
sleeptraced 10
sleeptraced 3

# Show Alice's new offer.
printdate "ALICE: Looking at her new ${DIRECTION} ${CURRENCY_CODE} offer."
CMD="$CLI_BASE --port=${ALICE_PORT} getmyoffer --offer-id=${OFFER_ID}"
printdate "ALICE CLI: ${CMD}"
OFFER=$($CMD)
exitoncommandalert $?
echo "${OFFER}"
printbreak
sleeptraced 7

# Generate some btc blocks.
printdate "Generating btc blocks after publishing Alice's offer."
Expand Down
15 changes: 10 additions & 5 deletions apitest/scripts/trade-simulation-utils.sh
Expand Up @@ -166,9 +166,14 @@ createoffer() {
}

getcurrentprice() {
CURRENCY_CODE="$1"
CMD="python3 ${APITEST_SCRIPTS_HOME}/getmktprice.py ${CURRENCY_CODE}"
PRICE=$($CMD)
echo "${PRICE}"
PORT="$1"
CURRENCY_CODE="$2"
CMD="$CLI_BASE --port=$PORT getbtcprice --currency-code=$CURRENCY_CODE"
CMD_OUTPUT=$($CMD)
commandalert $? "Could not get current market $CURRENCY_CODE price."
FLOOR=$(echo $CMD_OUTPUT| cut -d'.' -f 1)
commandalert $? "Could not get the floor of the current market $CURRENCY_CODE price."
INTEGER=$(echo $FLOOR | tr -cd '[[:digit:]]')
commandalert $? "Could not convert the current market $CURRENCY_CODE price string to an integer."
echo "$INTEGER"
}

17 changes: 14 additions & 3 deletions apitest/scripts/trade-simulation.sh
Expand Up @@ -97,8 +97,9 @@ printdate "ALICE F2F payment-account-id = ${ALICE_ACCT_ID}, currency-code = ${AL
printbreak

printdate "ALICE ${ALICE_ROLE}: Creating ${DIRECTION} ${ALICE_ACCT_CURRENCY_CODE} offer with payment acct ${ALICE_ACCT_ID}."
CURRENT_PRICE=$(getcurrentprice "$ALICE_ACCT_CURRENCY_CODE")
printdate "Current Market Price: $CURRENT_PRICE $ALICE_ACCT_CURRENCY_CODE"
CURRENT_PRICE=$(getcurrentprice "$ALICE_PORT" "$ALICE_ACCT_CURRENCY_CODE")
exitoncommandalert $?
printdate "Current Market Price: $CURRENT_PRICE"
CMD="$CLI_BASE --port=${ALICE_PORT} createoffer"
CMD+=" --payment-account=${ALICE_ACCT_ID}"
CMD+=" --direction=${DIRECTION}"
Expand All @@ -116,7 +117,17 @@ OFFER_ID=$(createoffer "${CMD}")
exitoncommandalert $?
printdate "ALICE ${ALICE_ROLE}: Created offer with id: ${OFFER_ID}."
printbreak
sleeptraced 10
sleeptraced 3

# Show Alice's new offer.
printdate "ALICE ${ALICE_ROLE}: Looking at her new ${DIRECTION} ${CURRENCY_CODE} offer."
CMD="$CLI_BASE --port=${ALICE_PORT} getmyoffer --offer-id=${OFFER_ID}"
printdate "ALICE CLI: ${CMD}"
OFFER=$($CMD)
exitoncommandalert $?
echo "${OFFER}"
printbreak
sleeptraced 7

# Generate some btc blocks.
printdate "Generating btc blocks after publishing Alice's offer."
Expand Down
20 changes: 20 additions & 0 deletions cli/src/main/java/bisq/cli/CliMain.java
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down Expand Up @@ -75,6 +76,7 @@

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;
Expand All @@ -98,6 +100,7 @@
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;
Expand Down Expand Up @@ -189,6 +192,7 @@ public static void run(String[] args) {
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;
Expand Down Expand Up @@ -243,6 +247,20 @@ public static void run(String[] args) {
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));
Expand Down Expand Up @@ -802,6 +820,8 @@ private static void printHelp(OptionParser parser, @SuppressWarnings("SameParame
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");
Expand Down
5 changes: 5 additions & 0 deletions cli/src/main/java/bisq/cli/CurrencyFormat.java
Expand Up @@ -77,6 +77,11 @@ static String formatVolumeRange(long minVolume, long volume) {
: 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);
Expand Down
1 change: 1 addition & 0 deletions cli/src/main/java/bisq/cli/Method.java
Expand Up @@ -28,6 +28,7 @@ public enum Method {
createpaymentacct,
getaddressbalance,
getbalance,
getbtcprice,
getfundingaddresses,
getmyoffer,
getmyoffers,
Expand Down
52 changes: 52 additions & 0 deletions cli/src/main/java/bisq/cli/opts/GetBTCMarketPriceOptionParser.java
@@ -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);
}
}
30 changes: 30 additions & 0 deletions core/src/main/resources/help/getbtcprice-help.txt
@@ -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