Add apitest/scripts/bsqswap-simulation.sh script

Some refactoring and typo corrections in existing scripts too.
This commit is contained in:
ghubstan 2021-11-28 16:12:02 -03:00
parent 66115f3d81
commit 1b7e43a874
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
5 changed files with 225 additions and 13 deletions

View File

@ -0,0 +1,83 @@
#! /bin/bash
# This file must be sourced by the driver script.
source "$APITEST_SCRIPTS_HOME/trade-simulation-env.sh"
source "$APITEST_SCRIPTS_HOME/trade-simulation-utils.sh"
gencreatebsqswapoffercommand() {
PORT="$1"
CMD="$CLI_BASE --port=$PORT createoffer"
CMD+=" --swap=true"
CMD+=" --direction=$DIRECTION"
CMD+=" --amount=$AMOUNT"
CMD+=" --fixed-price=$FIXED_PRICE"
CMD+=" --currency-code=$CURRENCY_CODE"
echo "$CMD"
}
createbsqswapoffer() {
CREATE_OFFER_CMD="$1"
OFFER_DESC=$($CREATE_OFFER_CMD)
# If the CLI command exited with an error, print the CLI error, and
# return from this function now, passing the error status code to the caller.
commandalert $? "Could not create offer."
OFFER_DETAIL=$(echo -e "$OFFER_DESC" | sed -n '2p')
NEW_OFFER_ID=$(echo -e "$OFFER_DETAIL" | awk '{print $NF}')
echo "$NEW_OFFER_ID"
}
executebsqswap() {
# Bob list available BSQ offers. (If a v1 BSQ offer is picked this simulation will break.)
printdate "Bob looking at $DIRECTION $CURRENCY_CODE offers."
CMD="$CLI_BASE --port=$BOB_PORT getoffers --direction=$DIRECTION --currency-code=$CURRENCY_CODE"
printdate "BOB CLI: $CMD"
OFFERS=$($CMD)
exitoncommandalert $?
echo "$OFFERS"
printbreak
OFFER_ID=$(getfirstofferid "$BOB_PORT")
exitoncommandalert $?
printdate "First BSQ offer found: $OFFER_ID"
# Take Alice's BSQ swap offer.
CMD="$CLI_BASE --port=$BOB_PORT takeoffer --offer-id=$OFFER_ID"
printdate "BOB CLI: $CMD"
TRADE=$($CMD)
commandalert $? "Could not take BSQ swap offer."
# Print the takeoffer command's console output.
printdate "$TRADE"
printbreak
# Generate 1 btc block
printdate "Generating 1 btc block after BSQ swap execution."
genbtcblocks 1 2
printbreak
printdate "BSQ swap trade $OFFER_ID complete."
printbreak
printdate "Alice looking at her trade with id $OFFER_ID."
CMD="$CLI_BASE --port=$ALICE_PORT gettrade --trade-id=$OFFER_ID"
printdate "ALICE CLI: $CMD"
GETTRADE_CMD_OUTPUT=$(gettrade "$CMD")
exitoncommandalert $?
echo "$GETTRADE_CMD_OUTPUT"
printbreak
TRADE_DETAIL=$(gettradedetail "$GETTRADE_CMD_OUTPUT")
exitoncommandalert $?
printdate "Bob looking at his trade with id $OFFER_ID."
CMD="$CLI_BASE --port=$BOB_PORT gettrade --trade-id=$OFFER_ID"
printdate "BOB CLI: $CMD"
GETTRADE_CMD_OUTPUT=$(gettrade "$CMD")
exitoncommandalert $?
echo "$GETTRADE_CMD_OUTPUT"
printbreak
TRADE_DETAIL=$(gettradedetail "$GETTRADE_CMD_OUTPUT")
exitoncommandalert $?
}

View File

@ -0,0 +1,90 @@
#! /bin/bash
# Demonstrates a bsq <-> btc swap trade using the API CLI with a local regtest bitcoin node.
#
# Prerequisites:
#
# - Linux or OSX with bash, Java 10, or Java 11-15 (JDK language compatibility 11), and bitcoin-core (v0.19, v0.20, or v0.21).
#
# - Bisq must be fully built with apitest dao setup files installed.
# Build command: `./gradlew clean build :apitest:installDaoSetup`
#
# - All supporting nodes must be run locally, in dev/dao/regtest mode:
# bitcoind, seednode, arbdaemon, alicedaemon, bobdaemon
#
# These should be run using the apitest harness. From the root project dir, run:
# `$ ./bisq-apitest --apiPassword=xyz --supportingApps=bitcoind,seednode,arbdaemon,alicedaemon,bobdaemon --shutdownAfterTests=false`
#
# Usage:
#
# This script must be run from the root of the project, e.g.:
#
# `$ apitest/scripts/bsqswap-simulation.sh -d buy -f 0.00005 -a 0.125`
#
# Script options: -d <direction> -c <country-code> -f <fixed-price> -a <amount(btc)>
#
# Examples:
#
# Create and take a bsq swap offer to buy 0.05 btc at a fixed-price of 0.00005 bsq (per 1 btc):
#
# `$ apitest/scripts/bsqswap-simulation.sh -d buy -a 0.05 -f 0.00005`
#
# Create and take a bsq swap offer to buy 1 btc at a fixed-price of 0.00005 bsq (per 1 btc):
#
# `$ apitest/scripts/bsqswap-simulation.sh -d buy -a 1 -f 0.0005`
export APP_BASE_NAME=$(basename "$0")
export APP_HOME=$(pwd -P)
export APITEST_SCRIPTS_HOME="$APP_HOME/apitest/scripts"
source "$APITEST_SCRIPTS_HOME/trade-simulation-env.sh"
source "$APITEST_SCRIPTS_HOME/trade-simulation-utils.sh"
source "$APITEST_SCRIPTS_HOME/bsqswap-simulation-utils.sh"
checksetup
parsebsqswaporderopts "$@"
printdate "Started $APP_BASE_NAME with parameters:"
printbsqswapscriptparams
printbreak
# Alice creates a bsq swap offer.
printdate "Alice creating BSQ swap offer: $DIRECTION $AMOUNT BTC for BSQ at fixed price of $FIXED_PRICE BTC per 1 BSQ."
CMD=$(gencreatebsqswapoffercommand "$ALICE_PORT" "$ALICE_ACCT_ID")
printdate "ALICE CLI: $CMD"
OFFER_ID=$(createbsqswapoffer "$CMD")
exitoncommandalert $?
printdate "Alice created bsq swap offer with id: $OFFER_ID."
printbreak
sleeptraced 2
# Show Alice's new bsq swap 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 2
# Generate 1 btc block.
printdate "Generating 1 btc block after publishing Alice's offer."
genbtcblocks 1 1
printbreak
# Execute the BSQ swap.
executebsqswap
exitoncommandalert $?
printbreak
# Get balances after trade completion.
printdate "Bob & Alice's balances after BSQ swap trade."
printdate "ALICE CLI:"
printbalances "$ALICE_PORT"
printbreak
printdate "BOB CLI:"
printbalances "$BOB_PORT"
printbreak
exit 0

View File

@ -179,6 +179,41 @@ parselimitorderopts() {
fi
}
parsebsqswaporderopts() {
usage() {
echo "Usage: $0 [-d buy|sell] [-f <fixed-price>] [-a <amount in btc>]" 1>&2
exit 1;
}
local OPTIND o d f a
while getopts "d:f:a:" o; do
case "${o}" in
d) d=$(echo "${OPTARG}" | tr '[:lower:]' '[:upper:]')
((d == "BUY" || d == "SELL")) || usage
export DIRECTION=${d}
;;
f) f=${OPTARG}
export FIXED_PRICE=${f}
;;
a) a=${OPTARG}
export AMOUNT=${a}
;;
*) usage ;;
esac
done
shift $((OPTIND-1))
if [ -z "${d}" ] || [ -z "${a}" ]; then
usage
fi
if [ -z "${f}" ] ; then
usage
fi
export CURRENCY_CODE="BSQ"
}
checkbitcoindrunning() {
# There may be a '+' char in the path and we have to escape it for pgrep.
if [[ $APP_HOME == *"+"* ]]; then
@ -310,3 +345,9 @@ printscriptparams() {
echo " WAIT = $WAIT"
fi
}
printbsqswapscriptparams() {
echo " DIRECTION = $DIRECTION"
echo " FIXED_PRICE = $FIXED_PRICE"
echo " AMOUNT = $AMOUNT"
}

View File

@ -457,11 +457,10 @@ delayconfirmpaymentreceived() {
printbreak
}
# This is a large function that should be broken up if it ever makes sense to not treat a trade
# execution simulation as an bsq swap operation. But we are not testing api methods here, just
# demonstrating how to use them to get through the trade protocol. It should work for any trade
# between Bob & Alice, as long as Alice is maker, Bob is taker, and the offer to be taken is the
# first displayed in Bob's getoffers command output.
# This is a large function that might be split into smaller functions. But we are not testing
# api methods here, just demonstrating how to use them to get through the V1 trade protocol with
# the CLI. It should work for any trade between Bob & Alice, as long as Alice is maker, Bob is
# taker, and the offer to be taken is the first displayed in Bob's getoffers command output.
executetrade() {
# Bob list available offers.
printdate "BOB $BOB_ROLE: Looking at $DIRECTION $CURRENCY_CODE offers."
@ -477,7 +476,7 @@ executetrade() {
printdate "First offer found: $OFFER_ID"
# Take Alice's offer.
CMD="$CLI_BASE --port=$BOB_PORT takeoffer --offer-id=$OFFER_ID --payment-account=$BOB_ACCT_ID --fee-currency=bsq"
CMD="$CLI_BASE --port=$BOB_PORT takeoffer --offer-id=$OFFER_ID --payment-account=$BOB_ACCT_ID --fee-currency=BSQ"
printdate "BOB CLI: $CMD"
TRADE=$($CMD)
commandalert $? "Could not take offer."

View File

@ -1,13 +1,13 @@
#! /bin/bash
# Runs fiat <-> btc trading scenarios using the API CLI with a local regtest bitcoin node.
# Demonstrates a fiat <-> btc trade using the API CLI with a local regtest bitcoin node.
#
# A country code argument is used to create a country based face to face payment account for the simulated
# trade, and the maker's face to face payment account's currency code is used when creating the offer.
#
# Prerequisites:
#
# - Linux or OSX with bash, Java 10, or Java 11-12 (JDK language compatibility 10), and bitcoin-core (v0.19, v0.20, or v0.21).
# - Linux or OSX with bash, Java 10, or Java 11-15 (JDK language compatibility 11), and bitcoin-core (v0.19, v0.20, or v0.21).
#
# - Bisq must be fully built with apitest dao setup files installed.
# Build command: `./gradlew clean build :apitest:installDaoSetup`
@ -26,15 +26,16 @@
#
# `$ apitest/scripts/trade-simulation.sh -d buy -c fr -m 3.00 -a 0.125`
#
# Script options: -d <direction> -c <country-code> -m <mkt-price-margin(%)> - f <fixed-price> -a <amount(btc)>
# Script options: -d <direction> -c <country-code> -m <mkt-price-margin(%)> -f <fixed-price> -a <amount(btc)>
#
# Examples:
#
# Create a buy/eur offer to buy 0.125 btc at a mkt-price-margin of 0%, using an Italy face to face payment account:
# Create and take a buy/eur offer to buy 0.125 btc at a mkt-price-margin of 0%, using an Italy face to face
# payment account:
#
# `$ apitest/scripts/trade-simulation.sh -d buy -c it -m 0.00 -a 0.125`
#
# Create a sell/eur offer to sell 0.125 btc at a fixed-price of 38,000 euros, using a France face to face
# Create and take a sell/eur offer to sell 0.125 btc at a fixed-price of 38,000 euros, using a France face to face
# payment account:
#
# `$ apitest/scripts/trade-simulation.sh -d sell -c fr -f 38000 -a 0.125`
@ -53,8 +54,6 @@ printdate "Started $APP_BASE_NAME with parameters:"
printscriptparams
printbreak
registerdisputeagents
# Demonstrate how to create a country based, face to face account.
showcreatepaymentacctsteps "Alice" "$ALICE_PORT"