Skip to content

Commit

Permalink
Make it possible to pass configuration to plutus test script.
Browse files Browse the repository at this point in the history
Such configuration will make it possible to run the script from automated tests in CI.
  • Loading branch information
newhoggy committed Jun 23, 2021
1 parent b76de42 commit 9ccbfa9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
61 changes: 32 additions & 29 deletions scripts/plutus/example-txin-locking-plutus-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
set -e
set -o pipefail

export WORK="${WORK:-example/work}"
export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/node-bft1/node.sock}"
export TESTNET_MAGIC="${TESTNET_MAGIC:-42}"
export UTXO_VKEY="${UTXO_VKEY:-example/shelley/utxo-keys/utxo1.vkey}"
export UTXO_SKEY="${UTXO_SKEY:-example/shelley/utxo-keys/utxo1.skey}"

if [ "$1" == "guessinggame" ]; then
# NB: This plutus script uses a "typed" redeemer and "typed" datum.
plutusscriptinuse=scripts/plutus/scripts/typed-guessing-game-redeemer-42-datum-42.plutus
Expand Down Expand Up @@ -36,19 +42,16 @@ fi
# in order to accomodate this.


plutusscriptaddr=$(cardano-cli address build --payment-script-file $plutusscriptinuse --testnet-magic 42)

mkdir -p example/work
plutusscriptaddr=$(cardano-cli address build --payment-script-file $plutusscriptinuse --testnet-magic "$TESTNET_MAGIC")

utxovkey=example/shelley/utxo-keys/utxo1.vkey
utxoskey=example/shelley/utxo-keys/utxo1.skey
mkdir -p $WORK

utxoaddr=$(cardano-cli address build --testnet-magic 42 --payment-verification-key-file $utxovkey)
utxoaddr=$(cardano-cli address build --testnet-magic "$TESTNET_MAGIC" --payment-verification-key-file $UTXO_VKEY)

cardano-cli query utxo --address $utxoaddr --cardano-mode --testnet-magic 42 --out-file example/work/utxo.json
cardano-cli query utxo --address $utxoaddr --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo.json

txin=$(jq -r 'keys[]' example/work/utxo.json)
lovelaceattxin=$(jq -r ".[\"$txin\"].value.lovelace" example/work/utxo.json)
txin=$(jq -r 'keys[]' $WORK/utxo.json)
lovelaceattxin=$(jq -r ".[\"$txin\"].value.lovelace" $WORK/utxo.json)
lovelaceattxindiv2=$(expr $lovelaceattxin / 2)

cardano-cli transaction build-raw \
Expand All @@ -58,34 +61,34 @@ cardano-cli transaction build-raw \
--tx-out "$plutusscriptaddr+$lovelaceattxindiv2" \
--tx-out-datum-hash "$scriptdatumhash" \
--tx-out "$utxoaddr+$lovelaceattxindiv2" \
--out-file example/work/create-datum-output.body
--out-file $WORK/create-datum-output.body

cardano-cli transaction sign \
--tx-body-file example/work/create-datum-output.body \
--testnet-magic 42 \
--signing-key-file $utxoskey\
--out-file example/work/create-datum-output.tx
--tx-body-file $WORK/create-datum-output.body \
--testnet-magic "$TESTNET_MAGIC" \
--signing-key-file $UTXO_SKEY \
--out-file $WORK/create-datum-output.tx

# SUBMIT
cardano-cli transaction submit --tx-file example/work/create-datum-output.tx --testnet-magic 42
cardano-cli transaction submit --tx-file $WORK/create-datum-output.tx --testnet-magic "$TESTNET_MAGIC"
echo "Pausing for 5 seconds..."
sleep 5

# Step 2
# After "locking" the tx output at the script address, we can now can attempt to spend
# the "locked" tx output below.

cardano-cli query utxo --address $plutusscriptaddr --testnet-magic 42 --out-file example/work/plutusutxo.json
plutusutxotxin=$(jq -r 'keys[]' example/work/plutusutxo.json)
cardano-cli query utxo --address $plutusscriptaddr --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/plutusutxo.json
plutusutxotxin=$(jq -r 'keys[]' $WORK/plutusutxo.json)

cardano-cli query utxo --address $utxoaddr --cardano-mode --testnet-magic 42 --out-file example/work/utxo.json
txinCollateral=$(jq -r 'keys[]' example/work/utxo.json)
cardano-cli query utxo --address $utxoaddr --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo.json
txinCollateral=$(jq -r 'keys[]' $WORK/utxo.json)

cardano-cli query protocol-parameters --testnet-magic 42 --out-file example/pparams.json
cardano-cli query protocol-parameters --testnet-magic "$TESTNET_MAGIC" --out-file example/pparams.json

dummyaddress=addr_test1vpqgspvmh6m2m5pwangvdg499srfzre2dd96qq57nlnw6yctpasy4

lovelaceatplutusscriptaddr=$(jq -r ".[\"$plutusutxotxin\"].value.lovelace" example/work/plutusutxo.json)
lovelaceatplutusscriptaddr=$(jq -r ".[\"$plutusutxotxin\"].value.lovelace" $WORK/plutusutxo.json)

txfee=$(expr $plutusrequiredtime + $plutusrequiredtime)
spendable=$(expr $lovelaceatplutusscriptaddr - $plutusrequiredtime - $plutusrequiredtime)
Expand All @@ -101,20 +104,20 @@ cardano-cli transaction build-raw \
--protocol-params-file example/pparams.json\
--tx-in-redeemer-file "$redeemerfilepath" \
--tx-in-execution-units "($plutusrequiredtime, $plutusrequiredspace)" \
--out-file example/work/test-alonzo.body
--out-file $WORK/test-alonzo.body

cardano-cli transaction sign \
--tx-body-file example/work/test-alonzo.body \
--testnet-magic 42 \
--signing-key-file example/shelley/utxo-keys/utxo1.skey \
--out-file example/work/alonzo.tx
--tx-body-file $WORK/test-alonzo.body \
--testnet-magic "$TESTNET_MAGIC" \
--signing-key-file "${UTXO_SKEY}" \
--out-file $WORK/alonzo.tx

# SUBMIT example/work/alonzo.tx
# SUBMIT $WORK/alonzo.tx
echo "Submit the tx with plutus script and wait 5 seconds..."
cardano-cli transaction submit --tx-file example/work/alonzo.tx --testnet-magic 42
cardano-cli transaction submit --tx-file $WORK/alonzo.tx --testnet-magic "$TESTNET_MAGIC"
sleep 5
echo ""
echo "Querying UTxO at $dummyaddress. If there is ADA at the address the Plutus script successfully executed!"
echo ""
cardano-cli query utxo --address "$dummyaddress" --testnet-magic 42
cardano-cli query utxo --address "$dummyaddress" --testnet-magic "$TESTNET_MAGIC"

2 changes: 1 addition & 1 deletion scripts/plutus/simple-minting-policy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set -o pipefail

# Step 1 - Send ADA to token script owner

export CARDANO_NODE_SOCKET_PATH=example/node-bft1/node.sock
export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/node-bft1/node.sock}"

utxovkey=example/shelley/utxo-keys/utxo1.vkey
utxoskey=example/shelley/utxo-keys/utxo1.skey
Expand Down

0 comments on commit 9ccbfa9

Please sign in to comment.