Skip to content

Commit

Permalink
Add an example how to use a script as stake credential
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Kant committed Sep 3, 2021
1 parent 6281302 commit f2ad65c
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/plutus/scripts/guess-42-stake.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "",
"cborHex": "587a58780100003233322232323233223232225335300a333500900800233500700d4815040184d400d2411d496e636f727265637420646174756d2e2045787065637465642034322e001235002353003335738002008930930900090008900091199ab9a3375e00400200c00a24002244004244002400246ea00041"
}
8 changes: 8 additions & 0 deletions scripts/test-stake-cred-script/consts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NODE_SOCK=../../../shelley-qa/node.sock
TESTNET_MAGIC=3
SCRIPTPATH=../plutus/scripts/guess-42-stake.plutus
REDEEMERPATH=../plutus/data/42.redeemer
SLOTS_PER_EPOCH_BYRON=360
## TODO: This is hardcoded at the moment. We need to change that, once we have
## the necessary CLI tooling to get a stake address from a script.
STAKE_ADDRESS=stake_test17r6ju53vp4xdt9cyezpuxv8hsyf5chcqmq2jmvlv5wrqz4qj3pjl8
44 changes: 44 additions & 0 deletions scripts/test-stake-cred-script/create-collateral.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

set -e
# Unoffiical bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail

source consts.sh
export CARDANO_NODE_SOCKET_PATH=${NODE_SOCK}


# query the UTxO
cardano-cli query utxo \
--address "$(cat addresses/address.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo.json

# cardano-cli transaction build-raw
TXIN=$(jq -r 'keys[0]' queries/utxo.json)
LOVELACE=$(jq -r ".[\"$TXIN\"].value.lovelace" queries/utxo.json)

mkdir -p txs

cardano-cli transaction build \
--alonzo-era \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-in ${TXIN} \
--tx-out "$(cat addresses/address.addr)+1000000" \
--change-address $(cat addresses/address-script.addr) \
--out-file txs/create-collateral.txraw

cardano-cli transaction sign \
--tx-body-file txs/create-collateral.txraw \
--signing-key-file addresses/payment-addr.skey \
--testnet-magic ${TESTNET_MAGIC} \
--out-file txs/create-collateral

cardano-cli transaction submit \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-file txs/create-collateral
61 changes: 61 additions & 0 deletions scripts/test-stake-cred-script/delegate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

set -e
# Unoffiical bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail

source consts.sh
export CARDANO_NODE_SOCKET_PATH=${NODE_SOCK}


# query the UTxO
cardano-cli query utxo \
--address "$(cat addresses/address.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo-collateral.json

cardano-cli query utxo \
--address "$(cat addresses/address-script.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo.json

cardano-cli query protocol-parameters \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/parameters.json

# cardano-cli transaction build-raw
TXIN_COLLATERAL=$(jq -r 'keys[0]' queries/utxo-collateral.json)
TXIN=$(jq -r 'keys[0]' queries/utxo.json)
LOVELACE=$(jq -r ".[\"$TXIN\"].value.lovelace" queries/utxo.json)

mkdir -p txs

cardano-cli transaction build \
--alonzo-era \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-in ${TXIN} \
--tx-in-collateral ${TXIN_COLLATERAL} \
--change-address $(cat addresses/address-script.addr) \
--certificate-file certs/register-stake-address-script \
--certificate-file certs/delegate-script \
--certificate-script-file ${SCRIPTPATH} \
--certificate-redeemer-file ${REDEEMERPATH} \
--protocol-params-file queries/parameters.json \
--out-file txs/delegate.raw

cardano-cli transaction sign \
--tx-body-file txs/delegate.raw \
--signing-key-file addresses/payment-addr.skey \
--testnet-magic ${TESTNET_MAGIC} \
--out-file txs/delegate

cardano-cli transaction submit \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-file txs/delegate
60 changes: 60 additions & 0 deletions scripts/test-stake-cred-script/deregister.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash

set -e
# Unoffiical bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail

source consts.sh
export CARDANO_NODE_SOCKET_PATH=${NODE_SOCK}


# query the UTxO
cardano-cli query utxo \
--address "$(cat addresses/address.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo-collateral.json

cardano-cli query utxo \
--address "$(cat addresses/address-script.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo.json

cardano-cli query protocol-parameters \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/parameters.json

# cardano-cli transaction build-raw
TXIN_COLLATERAL=$(jq -r 'keys[0]' queries/utxo-collateral.json)
TXIN=$(jq -r 'keys[0]' queries/utxo.json)
LOVELACE=$(jq -r ".[\"$TXIN\"].value.lovelace" queries/utxo.json)

mkdir -p txs

cardano-cli transaction build \
--alonzo-era \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-in ${TXIN} \
--tx-in-collateral ${TXIN_COLLATERAL} \
--change-address $(cat addresses/address-script.addr) \
--certificate-file certs/deregister-script \
--certificate-script-file ${SCRIPTPATH} \
--certificate-redeemer-file ${REDEEMERPATH} \
--protocol-params-file queries/parameters.json \
--out-file txs/deregister.raw

cardano-cli transaction sign \
--tx-body-file txs/deregister.raw \
--signing-key-file addresses/payment-addr.skey \
--testnet-magic ${TESTNET_MAGIC} \
--out-file txs/deregister

cardano-cli transaction submit \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-file txs/deregister
96 changes: 96 additions & 0 deletions scripts/test-stake-cred-script/mkfiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

set -e
# Unoffiical bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail

echo "This script allows testing of using a plutus script as a"
echo "stake credential. Files for addresses and certificates are"
echo "generated by this script."


source consts.sh
export CARDANO_NODE_SOCKET_PATH=${NODE_SOCK}

# create an address, with a script as a stake credential, along with stake
# address registration and delegation certificates

mkdir -p addresses certs queries

# create a payment address key
cardano-cli address key-gen \
--verification-key-file addresses/payment-addr.vkey \
--signing-key-file addresses/payment-addr.skey

# create an address (with null stake credential)
cardano-cli address build \
--payment-verification-key-file addresses/payment-addr.vkey \
--testnet-magic ${TESTNET_MAGIC} \
--out-file addresses/address.addr

# create an address (with script stake credential)
cardano-cli address build \
--payment-verification-key-file addresses/payment-addr.vkey \
--stake-script-file ${SCRIPTPATH} \
--testnet-magic ${TESTNET_MAGIC} \
--out-file addresses/address-script.addr

# create a stake address registration certificate
cardano-cli stake-address registration-certificate \
--stake-script-file ${SCRIPTPATH} \
--out-file certs/register-stake-address-script

# create a stake address deregistration certificate
cardano-cli stake-address deregistration-certificate \
--stake-script-file ${SCRIPTPATH} \
--out-file certs/deregister-script

# create a delegation certificate
# this will just pick the first pool in the list; if that pool does not
# deal out rewards (margin of 1, not actually creating blocks, ...),
# then the experiment will fail.
cardano-cli query stake-distribution \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/stake-distribution

POOL=$(jq -r 'keys[0]' queries/stake-distribution)

cardano-cli stake-address delegation-certificate \
--stake-script-file ${SCRIPTPATH} \
--stake-pool-id ${POOL} \
--out-file certs/delegate

cardano-cli stake-address delegation-certificate \
--stake-script-file ${SCRIPTPATH} \
--stake-pool-id ${POOL} \
--out-file certs/delegate-script

echo "Files have been created."
echo
echo "Next, you will have to move some funds to this address:"
echo
cat addresses/address.addr
echo
echo
echo "Once the funds are at this address, continue with the script"
echo
echo "./createt-collateral"
echo
echo "which will move funds so that you have one UTxO entry for"
echo "collateral, and one at the address using a script stake credential."
echo
echo "Once that has taken effect on the chain, use"
echo
echo "./delegate.sh"
echo
echo "to register the stake address, and actually delegate."
echo "It is possible that this fails, if the stake address is still registered"
echo "from a previous test. In this case, you can first deregister it via"
echo
echo "./deregister.sh"
echo
echo "After rewards are in the reward address, you can withdraw them via"
echo
echo "./withdraw.sh"
67 changes: 67 additions & 0 deletions scripts/test-stake-cred-script/withdraw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

set -e
# Unoffiical bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail

source consts.sh
export CARDANO_NODE_SOCKET_PATH=${NODE_SOCK}

# query the UTxO
cardano-cli query utxo \
--address "$(cat addresses/address.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo-collateral-claim.json

cardano-cli query utxo \
--address "$(cat addresses/address-script.addr)" \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/utxo-claim.json

cardano-cli query protocol-parameters \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--out-file queries/parameters-claim.json

## FIXME: This currently uses a fixed stake address, we need to get this from the CLI
cardano-cli query stake-address-info \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--epoch-slots ${SLOTS_PER_EPOCH_BYRON} \
--address ${STAKE_ADDRESS} \
--out-file queries/stake-address-info.json


TXIN_COLLATERAL=$(jq -r 'keys[0]' queries/utxo-collateral-claim.json)
TXIN=$(jq -r 'keys[0]' queries/utxo-claim.json)
LOVELACE=$(jq -r ".[\"$TXIN\"].value.lovelace" queries/utxo-claim.json)

REWARDS_BALANCE=$(jq -r '.[0].rewardAccountBalance' queries/stake-address-info.json)

cardano-cli transaction build \
--alonzo-era \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-in ${TXIN} \
--tx-in-collateral ${TXIN_COLLATERAL} \
--change-address $(cat addresses/address-script.addr) \
--withdrawal "${STAKE_ADDRESS}+${REWARDS_BALANCE}" \
--withdrawal-script-file ${SCRIPTPATH} \
--withdrawal-redeemer-file ${REDEEMERPATH} \
--protocol-params-file queries/parameters.json \
--out-file txs/withdraw.raw

cardano-cli transaction sign \
--tx-body-file txs/withdraw.raw \
--signing-key-file addresses/payment-addr.skey \
--testnet-magic ${TESTNET_MAGIC} \
--out-file txs/withdraw

cardano-cli transaction submit \
--cardano-mode \
--testnet-magic ${TESTNET_MAGIC} \
--tx-file txs/withdraw

0 comments on commit f2ad65c

Please sign in to comment.