Cardano Part 3: Plutus Smart Contracts
2 min readSep 6, 2021
In order to build smart contracts you will need followings
- You have cardano-node (1.29.0) running
- Clone example Alonzo repo i.e. https://github.com/input-output-hk/Alonzo-testnet/blob/main/Alonzo-exercises/alonzo-purple/
- If is recommended to use nix-shell
Run the following commands
Run cabal-update
Run cabal build
Build and Compile Plutus
> cabal build
> cat plutus-helloworld.cabal | grep executable
> cabal run plutus-helloworld -- 1 helloworld2.plutus
helloworld2.plutus is the script that is compiled and will be used to hold funds
Create Script Address
ardano-cli address build --payment-script-file helloworld2.plutus --testnet-magic ${TESTNET_MAGIC} --out-file helloworld2.addr
Query The Script Address
cardano-cli query utxo --testnet-magic 8 --address $(cat helloworld2.addr)
Questions:
1- Where the funds are coming from?
Build Hash of Data
cardano-cli transaction hash-script-data — script-data-value 79600447942433 > helloworld_hash.txt
Build Transaction
cardano-cli transaction build \
--alonzo-era \
--tx-in 78a7936263b3691e9dafe931f843981031d66b480976e9c6ac52684cdec16c58#0 \
--tx-out $(cat helloworld.addr)+98000000 \
--tx-out-datum-hash $(cat helloworld_hash.txt) \
--change-address $(cat ../app/keys/jad/payment.addr) \
--testnet-magic ${TESTNET_MAGIC} \
--out-file tx.raw
Sign The Transaction
cardano-cli transaction sign --tx-body-file tx.raw --signing-key-file ../app/keys/jad/payment.skey --out-file tx.sign
Submit
cardano-cli transaction submit --testnet-magic ${TESTNET_MAGIC} --tx-file tx.sign
Spend From The Script
cardano-cli transaction build \
--alonzo-era \
--protocol-params-file pparams.json \
--tx-in 369d6d17e6ba411535fd8bea52d531a1b09392ff74f4051cf14d7884f567e90d#1 \
--tx-in-script-file helloworld.plutus \
--tx-in-datum-value 79600447942433 \
--tx-in-redeemer-value 79600447942433 \
--tx-in-collateral 521d04192a6daa95c7d63bc58045ea0bc2a6553e8e7cac18b659a8c9ce62bced#0 \
--change-address $(cat ../app/keys/med/payment.addr) \
--testnet-magic ${TESTNET_MAGIC} \
--out-file tx.raw
Following is the input utxo that will be spend and send to the change-address.
369d6d17e6ba411535fd8bea52d531a1b09392ff74f4051cf14d7884f567e90d
Sign and Submit
cardano-cli transaction sign --tx-body-file tx.raw --signing-key-file ../app/keys/med/payment.skey --out-file tx.signcardano-cli transaction submit --testnet-magic ${TESTNET_MAGIC} --tx-file tx.sign
It is important to note that
- The transaction will be signed by the receiver of the locked funds (med)