You can use cardano-cli
to create simple transactions:
Copy cardano-cli transaction
Creating a transaction with build-raw
To create a transaction using build-raw
, you will need the protocol parameters. These parameters are necessary for calculating the transaction fee at a later stage:
Copy cardano-cli query protocol-parameters --testnet-magic 2 --out-file pparams.json
You will also need to know the transaction hash and index from which you intend to send funds:
Copy cardano-cli query utxo --address $( cat payment.addr ) --testnet-magic 2
Let's send 5,000 ada to the paymentwithstake.addr
:
Copy cardano-cli transaction build-raw --babbage-era \
--tx-in $(cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2 --out-file /dev/stdout | jq -r 'keys[0]') \
--tx-out $( cat paymentwithstake.addr ) +5000000000 \
--tx-out $( cat payment.addr ) +5000000000 \
--fee 0 \
--protocol-params-file pparams.json \
--out-file tx.draft
Copy cardano-cli transaction calculate-min-fee --tx-body-file tx.draft \
--testnet-magic 2 \
--protocol-params-file pparams.json \
--tx-in-count 1 \
--tx-out-count 2 \
--witness-count 1
This information will enable you to determine the transaction fee that needs to be paid, for example:
Now, you need to reconstruct the transaction body by including the fees and recalculating the change that you intend to send to payment.addr
Copy echo $((10000000000 - 5000000000 - 171353))
code
> 4999828647
Copy cardano-cli transaction build-raw --babbage-era \
--tx-in $(cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2 --out-file /dev/stdout | jq -r 'keys[0]') \
--tx-out $(cat paymentwithstake.addr)+5000000000 \
--tx-out $(cat payment.addr)+4999828647 \
--fee 171353 \
--protocol-params-file pparams.json \
--out-file tx.raw
Sign and submit the transaction using the payment.skey
generated earlier:
Copy cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--testnet-magic 2 \
--out-file tx.signed
Copy cardano-cli transaction submit \
--testnet-magic 2 \
--tx-file tx.signed
Creating a transaction with build
Now, let's send the rest of the funds from payment.addr
to paymentwithstake.addr
. This time, you can use the build command, which automatically handles the fees, streamlining the process:
Copy cardano-cli transaction build --babbage-era \
--testnet-magic 2 \
--tx-in $(cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2 --out-file /dev/stdout | jq -r 'keys[0]') \
--change-address $(cat paymentwithstake.addr) \
--out-file tx.raw
Sign and submit:
Copy cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--testnet-magic 2 \
--out-file tx.signed
Copy cardano-cli transaction submit \
--testnet-magic 2 \
--tx-file tx.signed
Last updated 9 months ago