Skip to content

Commit

Permalink
Add simple api/cli regtest trading script
Browse files Browse the repository at this point in the history
This is a work in progress, but when done, will resovle issue
bisq-network#4257.
  • Loading branch information
ghubstan committed Dec 31, 2020
1 parent f8cf109 commit cf64f55
Show file tree
Hide file tree
Showing 2 changed files with 448 additions and 0 deletions.
209 changes: 209 additions & 0 deletions apitest/scripts/simple-trading-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#! /bin/bash

#
# Script for running simple fiat <-> btc trading scenarios using the API CLI on regtest.
#
# Prerequisites:
#
# - Linux or OSX with bash, Java 10, or Java 11-12 (JDK language compatibility 10).
#
# - 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"`
#
# - The regtest arbitration daemon (arbdaemon) must have registered dispute agents, run:
# `./bisq-cli --password=xyz --port=9997 registerdisputeagent mediator 6ac43ea1df2a290c1c8391736aa42e4339c5cb4f110ff0257a13b63211977b7a`
# `./bisq-cli --password=xyz --port=9997 registerdisputeagent refundagent 6ac43ea1df2a290c1c8391736aa42e4339c5cb4f110ff0257a13b63211977b7a`
#
# - Only regtest btc can be bought or sold with the default dummy eur account.
# The CLI supports creating other fiat payment accts but this script doesn't (todo).
#
# Usage:
#
# This script must be run from the root of the project, e.g.:
#
# `apitest/scripts/simple-trading-script.sh -d buy -c eur -m 3.00 -a 0.125`
#
# Script options: -d <direction> -c <currency-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%:
#
# `apitest/scripts/simple-trading-script.sh -d buy -c usd -m 0.00 -a 0.125`
#
# Create a sell/eur offer to sell 0.125 btc at a fixed-price of 23,000 euros:
#
# `apitest/scripts/simple-trading-script.sh -d sell -c usd -f 23000 -a 0.125`

APP_BASE_NAME=`basename "$0"`
APP_HOME="`pwd -P`"
APITEST_SCRIPTS_HOME="${APP_HOME}/apitest/scripts"

# Source the env and some helper functions.
. ${APITEST_SCRIPTS_HOME}/trading-script-env.sh

checksetup
parseopts "$@"

echo "[`date`] Started ${APP_BASE_NAME} with parameters:"
printscriptparams


# get balances
echo "[`date`] Bob & Alice's balances before trade:"
printcmd "ALICE CLI: ${CLI_BASE} --port=${ALICE_PORT} getbalance"
printbalances $ALICE_PORT
printbreak
printcmd "BOB CLI: ${CLI_BASE} --port=${BOB_PORT} getbalance"
printbalances $BOB_PORT
printbreak

printcmd "ALICE CLI: ${CLI_BASE} --port=${ALICE_PORT} getpaymentaccts"
ALICE_ACCT_ID=$(getdummyacctid ${ALICE_PORT})
echo "[`date`] ALICE ${ALICE_ROLE}: Fiat Acct ID: ${ALICE_ACCT_ID}"
printbreak

printcmd "BOB CLI: ${CLI_BASE} --port=${BOB_PORT} getpaymentaccts"
BOB_ACCT_ID=$(getdummyacctid ${BOB_PORT})
echo "[`date`] Bob's Fiat Acct ID: ${BOB_ACCT_ID}"
printbreak

echo "[`date`] ALICE ${ALICE_ROLE}: Creating ${DIRECTION} ${CURRENCY_CODE} offer with payment acct ${ALICE_ACCT_ID}."
if [ -z "${MKT_PRICE_MARGIN}" ]; then
printcmd "ALICE CLI: $CLI_BASE --port=${ALICE_PORT} createoffer ${ALICE_ACCT_ID} ${DIRECTION} ${CURRENCY_CODE} ${AMOUNT} ${AMOUNT} false ${FIXED_PRICE} 0.15 BSQ"
else
printcmd "ALICE CLI: $CLI_BASE --port=${ALICE_PORT} createoffer ${ALICE_ACCT_ID} ${DIRECTION} ${CURRENCY_CODE} ${AMOUNT} ${AMOUNT} true ${MKT_PRICE_MARGIN} 0.15 BSQ"
fi
OFFER_ID=$(createoffer ${ALICE_PORT} ${ALICE_ACCT_ID})
echo "[`date`] ALICE ${ALICE_ROLE}: Created offer with id: ${OFFER_ID}"
printbreak
sleep 10

# Generate a btc block
echo "Generating btc block after publishing Alice's offer."
genbtcblock
printbreak
sleep 10

printcmd "BOB (CLI): $CLI_BASE --port=${BOB_PORT} getoffers ${DIRECTION} ${CURRENCY_CODE}"
getoffers ${BOB_PORT}
printbreak

echo "[`date`] BOB ${BOB_ROLE}: Taking offer ${OFFER_ID} with payment acct ${BOB_ACCT_ID}."
printcmd "BOB CLI: $CLI_BASE --port=${BOB_PORT} takeoffer ${OFFER_ID} ${BOB_ACCT_ID} BSQ"
TRADE=$(takeoffer ${BOB_PORT} ${OFFER_ID} ${BOB_ACCT_ID})
echo "[`date`] ${TRADE}"
printbreak
sleep 10

# Generate a btc block
echo "Generating btc block after Bob takes Alice's offer."
genbtcblock
printbreak
sleep 5


if [ $DIRECTION = "SELL" ]
then
# Confirm payment started
echo "[`date`] BOB ${BOB_ROLE}: Sending fiat payment sent msg."
printcmd "BOB CLI: $CLI_BASE --port=${BOB_PORT} confirmpaymentstarted ${OFFER_ID}"
SENT_MSG=$(confirmpaymentstarted ${BOB_PORT} ${OFFER_ID})
echo "[`date`] ${SENT_MSG}"
printbreak

sleep 10

# Confirm payment received
echo "[`date`] ALICE ${ALICE_ROLE}: Sending fiat payment recieved msg."
printcmd "ALICE CLI: $CLI_BASE --port=${ALICE_PORT} confirmpaymentreceived ${OFFER_ID}"
SENT_MSG=$(confirmpaymentreceived ${ALICE_PORT} ${OFFER_ID})
echo "[`date`] ${RCVD_MSG}"
printbreak

sleep 10
else
# Confirm payment started
echo "[`date`] ALICE ${ALICE_ROLE}: Sending fiat payment sent msg."
printcmd "ALICE CLI: $CLI_BASE --port=${ALICE_PORT} confirmpaymentstarted ${OFFER_ID}"
SENT_MSG=$(confirmpaymentstarted ${ALICE_PORT} ${OFFER_ID})
echo "[`date`] ALICE ${ALICE_ROLE}: ${SENT_MSG}"
printbreak

sleep 10

# Confirm payment received
echo "[`date`] BOB ${BOB_ROLE}: Sending fiat payment recieved msg."
printcmd "BOB CLI: $CLI_BASE --port=${BOB_PORT} confirmpaymentreceived ${OFFER_ID}"
RCVD_MSG=$(confirmpaymentreceived ${BOB_PORT} ${OFFER_ID})
echo "[`date`] ${RCVD_MSG}"
printbreak

sleep 10
fi


# Generate a btc block
echo "[`date`] Generating btc block after fiat transfer."
genbtcblock
printbreak
sleep 3


if [ $DIRECTION = "SELL" ]
then
# Complete Alice's side of trade
echo "[`date`] ALICE (taker): Closing trade by keeping funds in Bisq wallet."
printcmd "ALICE CLI: $CLI_BASE --port=${ALICE_PORT} keepfunds ${OFFER_ID}"
KEEP_FUNDS_MSG=$(keepfunds ${ALICE_PORT} ${OFFER_ID})
echo "[`date`] ${KEEP_FUNDS_MSG}"
sleep 5
else
# Complete Bob's side of trade
echo "[`date`] BOB ${BOB_ROLE}: Closing trade by keeping funds in Bisq wallet."
printcmd "BOB CLI: $CLI_BASE --port=${BOB_PORT} keepfunds ${OFFER_ID}"
KEEP_FUNDS_MSG=$(keepfunds ${BOB_PORT} ${OFFER_ID})
echo "[`date`] ${KEEP_FUNDS_MSG}"
sleep 5
fi
printbreak


# get balances
echo "[`date`] Bob & Alice's balances after trade:"
printcmd "[`date`] ALICE CLI: ${CLI_BASE} --port=${ALICE_PORT} getbalance"
printbalances $ALICE_PORT
printbreak
printcmd "[`date`] BOB CLI: ${CLI_BASE} --port=${BOB_PORT} getbalance"
printbalances $BOB_PORT
printbreak


exit 0






















Loading

0 comments on commit cf64f55

Please sign in to comment.