Merge pull request #5129 from ghubstan/03-rolling-offers-simulation

Add apitest rolling offer simulation script
This commit is contained in:
sqrrm 2021-02-03 16:56:50 +01:00 committed by GitHub
commit 874d923e35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 416 additions and 38 deletions

View file

@ -141,13 +141,12 @@ OFFER=$($CMD)
exitoncommandalert $? exitoncommandalert $?
echo "$OFFER" echo "$OFFER"
printbreak printbreak
sleeptraced 7 sleeptraced 4
# Generate some btc blocks. # Generate some btc blocks.
printdate "Generating btc blocks after publishing Alice's offer." printdate "Generating btc blocks after publishing Alice's offer."
genbtcblocks 3 3 genbtcblocks 3 3
printbreak printbreak
sleeptraced 5
# Show Alice's offer in Bob's CLI. # Show Alice's offer in Bob's CLI.
printdate "BOB: Looking at $DIRECTION $CURRENCY_CODE offers." printdate "BOB: Looking at $DIRECTION $CURRENCY_CODE offers."

View file

@ -0,0 +1,119 @@
#! /bin/bash
# Demonstrates a way to always keep one offer in the market, using the API CLI with a local regtest bitcoin node.
# Alice creates an offer, waits for Bob to take it, and completes the trade protocol with him. Then Alice
# creates a new offer...
#
# Stop the script by entering ^C.
#
# A country code argument is used to create a country based face to face payment account for the simulated offer.
#
# Prerequisites:
#
# - Linux or OSX with bash, Java 10, or Java 11-12 (JDK language compatibility 10), and bitcoin-core (v0.19, v0.20).
#
# - 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`
#
# - Only regtest btc can be bought or sold with the test payment account.
#
# Usage:
#
# This script must be run from the root of the project, e.g.:
#
# `$ apitest/scripts/rolling-offer-simulation.sh -d buy -c us -m 2.00 -a 0.125`
#
# Script options: -d <direction> -c <country-code> (-m <mkt-price-margin(%)> || -f <fixed-price>) -a <amount(btc)>
#
# Example:
#
# Create a buy/usd offer to sell 0.1 btc at 2% above market price, using a US face to face payment account:
#
# `$ apitest/scripts/rolling-offer-simulation.sh -d sell -c us -m 2.00 -a 0.1`
APP_BASE_NAME=$(basename "$0")
APP_HOME=$(pwd -P)
APITEST_SCRIPTS_HOME="$APP_HOME/apitest/scripts"
source "$APITEST_SCRIPTS_HOME/trade-simulation-env.sh"
source "$APITEST_SCRIPTS_HOME/trade-simulation-utils.sh"
checksetup
parseopts "$@"
printdate "Started $APP_BASE_NAME with parameters:"
printscriptparams
printbreak
registerdisputeagents
showcreatepaymentacctsteps "Alice" "$ALICE_PORT"
CMD="$CLI_BASE --port=$ALICE_PORT createpaymentacct --payment-account-form=$APITEST_SCRIPTS_HOME/$F2F_ACCT_FORM"
printdate "ALICE CLI: $CMD"
CMD_OUTPUT=$(createpaymentacct "$CMD")
echo "$CMD_OUTPUT"
printbreak
export ALICE_ACCT_ID=$(getnewpaymentacctid "$CMD_OUTPUT")
export CURRENCY_CODE=$(getnewpaymentacctcurrency "$CMD_OUTPUT")
printdate "Alice's F2F payment-account-id: $ALICE_ACCT_ID, currency-code: $CURRENCY_CODE"
exitoncommandalert $?
printbreak
printdate "Bob creates his F2F payment account."
CMD="$CLI_BASE --port=$BOB_PORT createpaymentacct --payment-account-form=$APITEST_SCRIPTS_HOME/$F2F_ACCT_FORM"
printdate "BOB CLI: $CMD"
CMD_OUTPUT=$(createpaymentacct "$CMD")
echo "$CMD_OUTPUT"
printbreak
export BOB_ACCT_ID=$(getnewpaymentacctid "$CMD_OUTPUT")
export CURRENCY_CODE=$(getnewpaymentacctcurrency "$CMD_OUTPUT")
printdate "Bob's F2F payment-account-id: $BOB_ACCT_ID, currency-code: $CURRENCY_CODE"
exitoncommandalert $?
printbreak
while : ; do
printdate "ALICE $ALICE_ROLE: Creating $DIRECTION $CURRENCY_CODE offer with payment acct $ALICE_ACCT_ID."
CURRENT_PRICE=$(getcurrentprice "$ALICE_PORT" "$CURRENCY_CODE")
exitoncommandalert $?
printdate "Current Market Price: $CURRENT_PRICE"
CMD=$(gencreateoffercommand "$ALICE_PORT" "$ALICE_ACCT_ID")
printdate "ALICE CLI: $CMD"
OFFER_ID=$(createoffer "$CMD")
exitoncommandalert $?
printdate "ALICE $ALICE_ROLE: Created offer with id: $OFFER_ID."
printbreak
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 3
# Generate some btc blocks.
printdate "Generating btc blocks after publishing Alice's offer."
genbtcblocks 3 2
printbreak
RANDOM_WAIT=$(echo $[$RANDOM % 10 + 1])
printdate "Bob will take Alice's offer in $RANDOM_WAIT seconds..."
sleeptraced "$RANDOM_WAIT"
executetrade
exitoncommandalert $?
printbreak
done
exit 0

View file

@ -221,6 +221,274 @@ getfirstofferid() {
echo "$FIRST_OFFER_ID" echo "$FIRST_OFFER_ID"
} }
gettrade() {
GET_TRADE_CMD="$1"
TRADE_DESC=$($GET_TRADE_CMD)
commandalert $? "Could not get trade."
echo "$TRADE_DESC"
}
gettradedetail() {
TRADE_DESC="$1"
# Get 2nd line of gettrade cmd output, and squeeze multi space delimiters into one space.
TRADE_DETAIL=$(echo "$TRADE_DESC" | sed -n '2p' | tr -s ' ')
commandalert $? "Could not get trade detail (line 2 of gettrade output)."
echo "$TRADE_DETAIL"
}
istradedepositpublished() {
TRADE_DETAIL="$1"
MAKER_OR_TAKER="$2"
if [ "$MAKER_OR_TAKER" = "MAKER" ]
then
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $9}')
else
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $10}')
fi
commandalert $? "Could not parse istradedepositpublished from trade detail."
echo "$ANSWER"
}
istradedepositconfirmed() {
TRADE_DETAIL="$1"
MAKER_OR_TAKER="$2"
if [ "$MAKER_OR_TAKER" = "MAKER" ]
then
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $10}')
else
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $11}')
fi
commandalert $? "Could not parse istradedepositconfirmed from trade detail."
echo "$ANSWER"
}
istradepaymentsent() {
TRADE_DETAIL="$1"
MAKER_OR_TAKER="$2"
if [ "$MAKER_OR_TAKER" = "MAKER" ]
then
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $11}')
else
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $12}')
fi
commandalert $? "Could not parse istradepaymentsent from trade detail."
echo "$ANSWER"
}
istradepaymentreceived() {
TRADE_DETAIL="$1"
MAKER_OR_TAKER="$2"
if [ "$MAKER_OR_TAKER" = "MAKER" ]
then
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $12}')
else
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $13}')
fi
commandalert $? "Could not parse istradepaymentreceived from trade detail."
echo "$ANSWER"
}
istradepayoutpublished() {
TRADE_DETAIL="$1"
MAKER_OR_TAKER="$2"
if [ "$MAKER_OR_TAKER" = "MAKER" ]
then
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $13}')
else
ANSWER=$(echo "$TRADE_DETAIL" | awk '{print $14}')
fi
commandalert $? "Could not parse istradepayoutpublished from trade detail."
echo "$ANSWER"
}
waitfortradedepositpublished() {
# Loops until Bob's trade deposit is published. (Bob is always the trade taker.)
OFFER_ID="$1"
DONE=0
while : ; do
if [ "$DONE" -ne 0 ]; then
break
fi
printdate "BOB $BOB_ROLE: 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 $?
IS_TRADE_DEPOSIT_PUBLISHED=$(istradedepositpublished "$TRADE_DETAIL" "TAKER")
exitoncommandalert $?
printdate "BOB $BOB_ROLE: Has taker's trade deposit been published? $IS_TRADE_DEPOSIT_PUBLISHED"
if [ "$IS_TRADE_DEPOSIT_PUBLISHED" = "YES" ]
then
DONE=1
else
RANDOM_WAIT=$(echo $[$RANDOM % 3 + 1])
sleeptraced "$RANDOM_WAIT"
fi
printbreak
done
}
waitfortradedepositconfirmed() {
# Loops until Bob's trade deposit is confirmed. (Bob is always the trade taker.)
OFFER_ID="$1"
DONE=0
while : ; do
if [ "$DONE" -ne 0 ]; then
break
fi
printdate "BOB $BOB_ROLE: 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 $?
IS_TRADE_DEPOSIT_CONFIRMED=$(istradedepositconfirmed "$TRADE_DETAIL" "TAKER")
exitoncommandalert $?
printdate "BOB $BOB_ROLE: Has taker's trade deposit been confirmed? $IS_TRADE_DEPOSIT_CONFIRMED"
printbreak
if [ "$IS_TRADE_DEPOSIT_CONFIRMED" = "YES" ]
then
DONE=1
else
printdate "Generating btc block while Bob waits for trade deposit to be confirmed."
genbtcblocks 1 0
RANDOM_WAIT=$(echo $[$RANDOM % 3 + 1])
sleeptraced "$RANDOM_WAIT"
fi
done
}
waitfortradepaymentsent() {
# Loops until buyer's trade payment has been sent.
PORT="$1"
SELLER="$2"
OFFER_ID="$3"
MAKER_OR_TAKER="$4"
DONE=0
while : ; do
if [ "$DONE" -ne 0 ]; then
break
fi
printdate "$SELLER: Looking at trade with id $OFFER_ID."
CMD="$CLI_BASE --port=$PORT gettrade --trade-id=$OFFER_ID"
printdate "$SELLER CLI: $CMD"
GETTRADE_CMD_OUTPUT=$(gettrade "$CMD")
exitoncommandalert $?
echo "$GETTRADE_CMD_OUTPUT"
printbreak
TRADE_DETAIL=$(gettradedetail "$GETTRADE_CMD_OUTPUT")
exitoncommandalert $?
IS_TRADE_PAYMENT_SENT=$(istradepaymentsent "$TRADE_DETAIL" "$MAKER_OR_TAKER")
exitoncommandalert $?
printdate "$SELLER: Has buyer's fiat payment been initiated? $IS_TRADE_PAYMENT_SENT"
if [ "$IS_TRADE_PAYMENT_SENT" = "YES" ]
then
DONE=1
else
RANDOM_WAIT=$(echo $[$RANDOM % 3 + 1])
sleeptraced "$RANDOM_WAIT"
fi
printbreak
done
}
waitfortradepaymentreceived() {
# Loops until buyer's trade payment has been received.
PORT="$1"
SELLER="$2"
OFFER_ID="$3"
MAKER_OR_TAKER="$4"
DONE=0
while : ; do
if [ "$DONE" -ne 0 ]; then
break
fi
printdate "$SELLER: Looking at trade with id $OFFER_ID."
CMD="$CLI_BASE --port=$PORT gettrade --trade-id=$OFFER_ID"
printdate "$SELLER CLI: $CMD"
GETTRADE_CMD_OUTPUT=$(gettrade "$CMD")
exitoncommandalert $?
echo "$GETTRADE_CMD_OUTPUT"
printbreak
TRADE_DETAIL=$(gettradedetail "$GETTRADE_CMD_OUTPUT")
exitoncommandalert $?
# When the seller receives a 'payment sent' message, it is assumed funds (fiat) have already been deposited.
# In a real trade, there is usually a delay between receipt of a 'payment sent' message, and the funds deposit,
# but we do not need to simulate that in this regtest script.
IS_TRADE_PAYMENT_SENT=$(istradepaymentreceived "$TRADE_DETAIL" "$MAKER_OR_TAKER")
exitoncommandalert $?
printdate "$SELLER: Has buyer's payment been transferred to seller's fiat account? $IS_TRADE_PAYMENT_SENT"
if [ "$IS_TRADE_PAYMENT_SENT" = "YES" ]
then
DONE=1
else
RANDOM_WAIT=$(echo $[$RANDOM % 3 + 1])
sleeptraced "$RANDOM_WAIT"
fi
printbreak
done
}
delayconfirmpaymentstarted() {
# Confirm payment started after a random delay. This should be run in the background
# while the payee polls the trade status, waiting for the message before confirming
# payment has been received.
PAYER="$1"
PORT="$2"
OFFER_ID="$3"
RANDOM_WAIT=$(echo $[$RANDOM % 5 + 1])
printdate "$PAYER: Sending fiat payment sent message to seller in $RANDOM_WAIT seconds..."
sleeptraced "$RANDOM_WAIT"
CMD="$CLI_BASE --port=$PORT confirmpaymentstarted --trade-id=$OFFER_ID"
printdate "$PAYER_CLI: $CMD"
SENT_MSG=$($CMD)
commandalert $? "Could not send confirmpaymentstarted message."
# Print the confirmpaymentstarted command's console output.
printdate "$SENT_MSG"
printbreak
}
delayconfirmpaymentreceived() {
# Confirm payment received after a random delay. This should be run in the background
# while the payer polls the trade status, waiting for the confirmation from the seller
# that funds have been received.
PAYEE="$1"
PORT="$2"
OFFER_ID="$3"
RANDOM_WAIT=$(echo $[$RANDOM % 5 + 1])
printdate "$PAYEE: Sending fiat payment sent message to seller in $RANDOM_WAIT seconds..."
sleeptraced "$RANDOM_WAIT"
CMD="$CLI_BASE --port=$PORT confirmpaymentreceived --trade-id=$OFFER_ID"
printdate "$PAYEE_CLI: $CMD"
RCVD_MSG=$($CMD)
commandalert $? "Could not send confirmpaymentstarted message."
# Print the confirmpaymentstarted command's console output.
printdate "$RCVD_MSG"
printbreak
}
# This is a large function that should be broken up if it ever makes sense to not treat a trade # This is a large function that should be broken up if it ever makes sense to not treat a trade
# execution simulation as an atomic operation. But we are not testing api methods here, just # execution simulation as an atomic 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 # demonstrating how to use them to get through the trade protocol. It should work for any trade
@ -248,13 +516,9 @@ executetrade() {
# Print the takeoffer command's console output. # Print the takeoffer command's console output.
printdate "$TRADE" printdate "$TRADE"
printbreak printbreak
sleeptraced 10
# Generate some btc blocks waitfortradedepositpublished "$OFFER_ID"
printdate "Generating btc blocks after Bob takes Alice's offer." waitfortradedepositconfirmed "$OFFER_ID"
genbtcblocks 3 3
printbreak
sleeptraced 5
# Send payment sent and received messages. # Send payment sent and received messages.
if [ "$DIRECTION" = "BUY" ] if [ "$DIRECTION" = "BUY" ]
@ -274,38 +538,35 @@ executetrade() {
PAYEE_CLI="ALICE CLI" PAYEE_CLI="ALICE CLI"
fi fi
# Confirm payment started. # Asynchronously send a confirm payment started message after a random delay.
printdate "$PAYER: Sending fiat payment sent msg." delayconfirmpaymentstarted "$PAYER" "$PAYER_PORT" "$OFFER_ID" &
CMD="$CLI_BASE --port=$PAYER_PORT confirmpaymentstarted --trade-id=$OFFER_ID"
printdate "$PAYER_CLI: $CMD"
SENT_MSG=$($CMD)
commandalert $? "Could not send confirmpaymentstarted message."
# Print the confirmpaymentstarted command's console output.
printdate "$SENT_MSG"
printbreak
sleeptraced 2 if [ "$DIRECTION" = "BUY" ]
printdate "Generating btc blocks after fiat payment sent msg." then
genbtcblocks 3 5 # Bob waits for payment, polling status in taker specific trade detail.
sleeptraced 2 waitfortradepaymentsent "$PAYEE_PORT" "$PAYEE" "$OFFER_ID" "TAKER"
printbreak else
# Alice waits for payment, polling status in maker specific trade detail.
waitfortradepaymentsent "$PAYEE_PORT" "$PAYEE" "$OFFER_ID" "MAKER"
fi
# Confirm payment received.
printdate "$PAYEE: Sending fiat payment received msg." # Asynchronously send a confirm payment received message after a random delay.
CMD="$CLI_BASE --port=$PAYEE_PORT confirmpaymentreceived --trade-id=$OFFER_ID" delayconfirmpaymentreceived "$PAYEE" "$PAYEE_PORT" "$OFFER_ID" &
printdate "$PAYEE_CLI: $CMD"
RCVD_MSG=$($CMD) if [ "$DIRECTION" = "BUY" ]
commandalert $? "Could not send confirmpaymentreceived message." then
# Print the confirmpaymentreceived command's console output. # Alice waits for payment rcvd confirm from Bob, polling status in maker specific trade detail.
printdate "$RCVD_MSG" waitfortradepaymentreceived "$PAYER_PORT" "$PAYER" "$OFFER_ID" "MAKER"
printbreak else
sleeptraced 4 # Bob waits for payment rcvd confirm from Alice, polling status in taker specific trade detail.
waitfortradepaymentreceived "$PAYER_PORT" "$PAYER" "$OFFER_ID" "TAKER"
fi
# Generate some btc blocks # Generate some btc blocks
printdate "Generating btc blocks after fiat transfer." printdate "Generating btc blocks after fiat transfer."
genbtcblocks 3 5 genbtcblocks 2 2
printbreak printbreak
sleeptraced 3
# Complete the trade on the seller side. # Complete the trade on the seller side.
if [ "$DIRECTION" = "BUY" ] if [ "$DIRECTION" = "BUY" ]
@ -322,7 +583,7 @@ executetrade() {
commandalert $? "Could close trade with keepfunds command." commandalert $? "Could close trade with keepfunds command."
# Print the keepfunds command's console output. # Print the keepfunds command's console output.
printdate "$KEEP_FUNDS_MSG" printdate "$KEEP_FUNDS_MSG"
sleeptraced 5 sleeptraced 3
printbreak printbreak
printdate "Trade $OFFER_ID complete." printdate "Trade $OFFER_ID complete."

View file

@ -102,13 +102,12 @@ OFFER=$($CMD)
exitoncommandalert $? exitoncommandalert $?
echo "$OFFER" echo "$OFFER"
printbreak printbreak
sleeptraced 7 sleeptraced 3
# Generate some btc blocks. # Generate some btc blocks.
printdate "Generating btc blocks after publishing Alice's offer." printdate "Generating btc blocks after publishing Alice's offer."
genbtcblocks 3 5 genbtcblocks 3 1
printbreak printbreak
sleeptraced 10
# Go through the trade protocol. # Go through the trade protocol.
executetrade executetrade