Building transactions, the easy way with the build command
On the previous section we built a transaction using the cardano-cli transaction build-raw command. You might have wished that it was a little bit more direct. The good news is that we have the cardano-cli transaction build command
​

cardano-cli transaction build

This command automates a lot of things for you. Lets send 5 ada from payment2.addr to payment.addr to demonstrate how to use this command.
Lets first query the utxo as we have done before:
cardano-cli query utxo --address $(cat payment2.addr) --testnet-magic 2
TxHash TxIx Amount
--------------------------------------------------------------------------------------
726f76b90b924b47f2cb8ab5a712c82b182a85800138bbc5294f53d517d25e7c 0 10000000 lovelace + TxOutDatumNone
Build command automatically calculates fees and the change to send to the specified change-address. Note that build command overestimates the fees a little bit compared to build-raw.
cardano-cli transaction build --testnet-magic 2
--tx-in 726f76b90b924b47f2cb8ab5a712c82b182a85800138bbc5294f53d517d25e7c#0
--tx-out $(cat payment.addr)+5000000
--change-address $(cat payment2.addr)
--protocol-params-file protocol.json
--out-file tx.raw Estimated transaction fee: Lovelace 166777
Sign and submit the transaction
cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment2.skey \
--testnet-magic 2 \
--out-file tx.signed
cardano-cli transaction submit \
--tx-file tx.signed \
--testnet-magic 2
Transaction successfully submitted.
Check the balances
cardano-cli query utxo --address $(cat payment2.addr) --testnet-magic 2
TxHash TxIx Amount
--------------------------------------------------------------------------------------
30231e5cff4048ee20ee864c708ac304c64d4cccd6975d629d5b7a8b5fab7db3 0 4833223 lovelace + TxOutDatumNone
​
​
cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2
TxHash TxIx Amount
--------------------------------------------------------------------------------------
30231e5cff4048ee20ee864c708ac304c64d4cccd6975d629d5b7a8b5fab7db3 1 5000000 lovelace + TxOutDatumNone
726f76b90b924b47f2cb8ab5a712c82b182a85800138bbc5294f53d517d25e7c 1 9989824599 lovelace + TxOutDatumNone
Interestingly, now we have 2 utxos on our payment.addr When building a new transaction we can choose between those utxos or use both.
Copy link