cardano course
  • Welcome!
  • Video lessons
  • Handbook
    • Module 1. Building and running the node
      • Building the node
      • Running the node and connecting to a network
    • Module 2. Basic operations
      • Creating keys and addresses
      • Creating a simple transaction
      • Registering the stake address and delegating
    • Module 3. Protocol parameters and configuration files
      • Protocol parameters
      • Byron genesis file
      • Shelley genesis file
      • Alonzo genesis file
      • Conway genesis file
      • Node configuration file
      • Topology file
    • Module 4. Peer-to-peer (P2P) networking
    • Module 5. Creating a stake pool
      • The setup
      • Generating keys
      • Registering a stake pool
      • Runtime system options
      • Running the nodes
      • Stake snapshots
      • Upgrading cardano-node and cardano-cli
      • Pool operations and maintenance
      • Retiring a stake pool
    • Module 6. Monitoring the nodes
    • Module 7. Cardano governance
      • Update proposals
      • Polls
    • Module 8. Setting up a local cluster
      • Creating a local cluster
      • Spending the genesis UTXO
      • From Byron to Shelley
      • Moving funds to a Shelley address
      • Creating a first stake pool
      • Bringing decentralization parameter down to .80
      • From Shelley to Alonzo
      • Creating a second stake pool before moving to the Babbage era
      • Redelegating genesis keys
      • Vasil hard fork
      • Creating a local cluster with the mkfiles script
    • Module 9. Simple scripts and Plutus scripts
    • Module 10. New tracing system
    • Module 11. Running the SMASH server
    • Module 12. Running the token metadata server
  • Curated playlist
Powered by GitBook
On this page
  • Creating a transaction with build-raw
  • Creating a transaction with build

Was this helpful?

Edit on GitHub
  1. Handbook
  2. Module 2. Basic operations

Creating a simple transaction

You can use cardano-cli to create simple transactions:

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:

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:

cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2

Let's send 5,000 ada to the paymentwithstake.addr:

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
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:

171353 Lovelace

Now, you need to reconstruct the transaction body by including the fees and recalculating the change that you intend to send to payment.addr

 echo $((10000000000 - 5000000000 - 171353))
 code
 > 4999828647
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:

cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--testnet-magic 2 \
--out-file tx.signed
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:

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:

cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--testnet-magic 2 \
--out-file tx.signed
cardano-cli transaction submit \
--testnet-magic 2 \
--tx-file tx.signed 
PreviousCreating keys and addressesNextRegistering the stake address and delegating

Last updated 1 year ago

Was this helpful?

Page cover image