Cardano Part 2: Plutus Transactions

Amjad Saleem
5 min readSep 3, 2021

You can start with latest instructions onhttps://github.com/input-output-hk/Alonzo-testnet.

Sending/Receiving Payments from Addresses (Wallets)

  1. Create verification and signing keys
cardano-cli address key-gen \
--verification-key-file payment.vkey \
--signing-key-file payment.skey

2. Create stake address

cardano-cli stake-address key-gen \
--verification-key-file stake.vkey \
--signing-key-file stake.skey

3. Create a payment address for testing

cardano-cli address build \
--payment-verification-key-file payment.vkey \
--stake-verification-key-file stake.vkey \
--out-file payment.addr \
--testnet-magic $TESTNET_MAGIC

4. Run & wait for cardano-node to sync the blockchain

NOTE: Make sure you have correct config files downloaded for the given node build. For example 1.29.0 testnet uses the ones from https://hydra.iohk.io/build/7370192/download/1/index.html

cardano-node run \
--topology testnet-topology.json \
--database-path db \
--socket-path db/node.socket \
--host-addr 127.0.0.1 \
--port 3001 \
--config testnet-config.json

Funding Your Account with tADA

You can get test ada (tADA) from https://testnets.cardano.org/en/testnets/cardano/tools/faucet/. There is a limit of 1000 ADA per 24 hours.

Check Sync Status

cardano-cli query tip  --testnet-magic ${TESTNET_MAGIC}

Building Simple Transactions-Non-Plutus

Build A Transaction

cardano-cli transaction build --alonzo-era --testnet-magic ${TESTNET_MAGIC} --change-address $(cat keys/jad/payment.addr) --tx-in 8dd25e6bd5c02c8a86f1bf4c301500cc5dfaee5f491519eaacd158cdee60a545#0 --tx-out $(cat keys/med/payment.addr)+25000000 --out-file tx.build

The 3fb***#0 is utxo from last transaction at the payment address that can be retried using

cardano-cli query utxo --address $(cat keys/sender/payment.addr) --testnet-magic $TESTNET_MAGIC

Sign The Transaction

cardano-cli transaction sign \
--tx-body-file tx.build \
--testnet-magic ${TESTNET_MAGIC} \
--signing-key-file keys/jad/payment.skey \
--out-file to-med-tx.signed

Submit Transaction

cardano-cli transaction submit --tx-file tx.signed --testnet-magic ${TESTNET_MAGIC}

At this point your transaction is signed by you i.e. payment.addr and submitted to blockchain. You can check in corresponding explorer e.g. https://explorer.cardano-testnet.iohkdev.io/en/address?address=addr_test1qzn8lrdt8mrk8yk7ryl807lyl7cwjxsh3zku764gqa7ks406cku9057aqrygf694hesxt0ma7s77npnqyxuqkgfv5ekqmdtrlt

You can query with

cardano-cli query utxo --address $(cat keys/cian/payment.addr) --testnet-magic ${TESTNET_MAGIC}

Building Plutus Transactions

In order to build plutus transactions you will need followings

  1. A plutus script — compiled
  2. The plutus script address
  3. The script datum hash — to attach data to the UTXO
  4. The protocol params — that contains cost per bytes and other blockchain related info
  5. Make sure you have funds (See Part 1)
  6. Build Plutus Transaction
  7. Sign The Transaction
  8. Submit The Transaction
  9. See in the explorer

Scenario

There are three users invovled in the plutus script execution. Jad, Cian and Med.

  • Jad sends some funds to a script that holders for Med to unlock
  • Med uses his funds as collatrals to unlock the funds from the script
  • The Med should receive funds

1. The Plutus Script

We will need a compiled plutus script where we will send funds. A dummy example is

{-# INLINABLE mkValidator #-}
mkValidator :: Data -> Data -> Data -> ()
mkValidator _ _ _ = ()

After compiling the script — which is a different topic and will be covered later- you will use the address to generate address and lock funds.

2. Calculate Address For The Script

cardano-cli address build --payment-script-file alwayssucceeds.plutus --testnet-magic ${TESTNET_MAGIC} --out-file script.addr

Where alwayssucceeds.plutus is the compiled file.

3. Get the Script address

cat script.addr
addr_test1wpnlxv2xv9a9ucvnvzqakwepzl9ltx7jzgm53av2e9ncv4sysemm8

addre_* is the address of the script.

Next we will need datum to attach to UTXO and that is done using hash of the script address

cardano-cli transaction hash-script-data --script-data-value 12

Above command will return a hash e.g. e9d8bac576e8604e7c3526025bc146f5fa178173e3a5592d122687bd785b520

The hash will be needed to link an arbitrary datum to an output UTXO.

4. Get And Set Hash As Env. Var

export scriptdatumhash=5e9d8bac576e8604e7c3526025bc146f5fa178173e3a5592d122687bd785b520

5. Get Protocol Params and Save As JSON

ardano-cli query protocol-parameters --testnet-magic ${TESTNET_MAGIC} > --out-file pparams.json

6. Get Funds

Check you ada balance in the address from where you want to send and lock funds. You can get the utxos are a particular addressing

cardano-cli query utxo --address $(cat keys/sender/payment.addr) --testnet-magic $TESTNET_MAGIC

7. Build The Plutus Transaction

cardano-cli transaction build \
--alonzo-era \
--testnet-magic ${TESTNET_MAGIC} \
--change-address $(cat the-payment.addr-where-you-want-to-get-ada-change) \
--tx-in a7f58d4651b9de1ffa876315d8dfd5b03cda6f5de430269b810e80d1cc791817#0 \
--tx-out $(cat script.addr)+2000000 \
--tx-out-datum-hash ${scriptdatumhash} \
--protocol-params-file pparams.json \
--out-file tx-script.build

8. Sign Transaction

cardano-cli transaction sign \
--tx-body-file tx-script.build \
--signing-key-file keys/jad/payment.skey \
--testnet-magic ${TESTNET_MAGIC} \--out-file tx-script.signed

9. Submit

cardano-cli transaction submit --testnet-magic ${TESTNET_MAGIC} --tx-file tx-script.signed

10. Verify The Transaction

You can search for the script address in the explorer e.g. https://explorer.cardano-testnet.iohkdev.io/en/address?address=addr_test1wpnlxv2xv9a9ucvnvzqakwepzl9ltx7jzgm53av2e9ncv4sysemm8&page=22&perPage=2

You can see

  • The address from where the funds where sent
  • The change, output, that is returned to the same address
  • The script address where we sent 2 ADA
  • Check UTXOs are the script address
cardano-cli query utxo --address $(cat script.addr) --testnet-magic ${TESTNET_MAGIC}
TxHash TxIx Amount
--------------------------------------------------------------------------------------
43ff6a7ca9eb0821a92d445a1b9ece0230fe02fe0dff9ae875e9449131636599 0 2000000 lovelace + TxOutDatumHash ScriptDataInAlonzoEra "d19cb82412566ed9919b50c7afca52c9d2beaff7be54e43997a5590c6a1af0ea"
7b26fc7146cce663d9b76f24f0ce05b33203a7facf6399eca5becff9db508b94 1 1000000000 lovelace + TxOutDatumHashNone
8dd25e6bd5c02c8a86f1bf4c301500cc5dfaee5f491519eaacd158cdee60a545 1 2000000 lovelace + TxOutDatumHash ScriptDataInAlonzoEra "5e9d8bac576e8604e7c3526025bc146f5fa178173e3a5592d122687bd785b520"

The Funds At The Script

cardano-cli query utxo --address $(cat script.addr) --testnet-magic ${TESTNET_MAGIC}

Export The Script UTXOs as Input

export plutusutxotxin=8dd25e6bd5c02c8a86f1bf4c301500cc5dfaee5f491519eaacd158cdee60a545#1

Unlock The Funds From The Script

Lets say we have a third wallet (payment.addr) that will unlock the funds. Make sure the wallet has some balance to pay fee

cardano-cli  query utxo --address $(cat keys/med/payment.addr) --testnet-magic ${TESTNET_MAGIC}

Lock Funds from the wallet as Collateral

export txCollateral="7b05a271ab1431eee131fd5f134238a285acd06cc80d9c2807a1158ca1fd187b#1"

Build Transaction

cardano-cli transaction build --alonzo-era --testnet-magic ${TESTNET_MAGIC} --tx-in ${plutusutxotxin} --tx-in-script-file AlwaysSucceeds.plutus --tx-in-datum-value 12 --tx-in-redeemer-value 22 --tx-in-collateral ${txCollateral} --change-address $(cat keys/med/payment.addr) --protocol-params-file pparams.json --out-file unlock-test-alonzo.tx

NOTES

  • — tx-in-datum 12 is the same we set when building script transaction
  • — tx-redeemer-value can be anything since we didn’t set a value

Sign The Transaction

cardano-cli transaction sign \
--tx-body-file unlock-test-alonzo.tx \
--signing-key-file keys/med/payment.skey \
--testnet-magic ${TESTNET_MAGIC} \
--out-file unlock-test-alonzo.signed

Submit

cardano-cli transaction submit --testnet-magic ${TESTNET_MAGIC} --tx-file unlock-test-alonzo.signed

Verify

You can verify the transactions and balance at the explorer using your med wallet

https://explorer.cardano-testnet.iohkdev.io/en/address?address=addr_test1qzzaney9yg4xwf8r2vyye85fnwxef6zyqs5y2sffk0t6fnc6c6m8ahfmmwsvqas6rdd0zn9u3ddtxrpfrjhewm5aur4quprx9g

--

--