Submitting Transactions to the Cardano Blockchain with cardano-cli

The Cardano blockchain allows users to submit transactions and interact with decentralized applications through its native CLI wallet, cardano-cli. As Cardano continues to grow in adoption and popularity, more developers and users are leveraging the cardano-cli to build dApps and utilize the network. Learning how to properly compose and submit transactions with the cardano-cli is key for any Cardano user. In this comprehensive guide, we'll walk through the entire process step-by-step.

Introduction to cardano-cli

The cardano-cli command line interface (CLI) allows you to interact with the Cardano node and blockchain directly from your terminal or command prompt. It's the native wallet for the Cardano protocol, providing a powerful set of tools for developers and advanced users. With the cardano-cli, you can:

  • Generate payment addresses
  • Create and submit transactions
  • Query information from the blockchain
  • Register and manage stake pool operations
  • And many other advanced functionalities

The cardano-cli uses ADA cryptocurrency under the hood for all transactions and operations. To start using it, you'll need access to a Cardano node synced with the latest blockchain data. Daedalus and Cardano wallets rely on the cardano-cli running in the background to enable native asset capabilities.

Overall, learning how to leverage the cardano-cli will empower you to fully unlock Cardano's capabilities and decentralized app potential. Now let's dive into the steps for composing and submitting transactions.

Generating a Payment Address

The first step is to generate a payment address that you can use to receive ADA. Your Cardano wallet like Daedalus normally handles this behind the scenes. But with the CLI, you'll manually need to create a payment address.

Run this command to generate a new payment address:

Copy codecardano-cli address key-gen \ --verification-key-file payment.vkey \ --signing-key-file payment.skey

This will generate a public and private key pair stored in the payment.vkey and payment.skey files. The public key is your payment address that others can send ADA to.

Next, you need to build the actual address itself:

Copy codecardano-cli address build \ --payment-verification-key-file payment.vkey \ --out-file payment.addr \ --testnet-magic 1097911063

This will take the public key and generate your readable payment address in payment.addr. The --testnet-magic parameter customizes this for Cardano's testnet.

Now you have a payment address to receive funds!

Checking Your ADA Balance

Before sending a transaction, you'll want to check your ADA balance. Run this command:

Copy codecardano-cli query utxo \ --address $(cat payment.addr) --testnet-magic 1097911063

This will query the Unspent Transaction Output (UTXO) associated with your payment address, showing your available ADA balance.

Drafting and Signing Transactions

To move ADA, you need to build and sign a transaction. Here are the steps:

  1. Draft the raw transaction

<!---->

Copy codecardano-cli transaction build \ --alonzo-era \ --tx-in <TX-IN> \ --tx-out <RECIPIENT-ADDRESS>+<AMOUNT> \ --tx-out <CHANGE-ADDRESS>+<AMOUNT> \ --out-file tx.draft

2.  Calculate the fee for the transaction size:

<!---->

Copy codecardano-cli transaction calculate-min-fee \ --tx-body-file tx.draft \ --tx-in-count 1 \ --tx-out-count 2 \ --witness-count 1 \ --testnet-magic 1097911063 \ --protocol-params-file protocol.json

3.  Add the fee to the transaction body:

<!---->

Copy codecardano-cli transaction build-raw \ --tx-in <TX-IN> --tx-out <RECIPIENT-ADDRESS>+<AMOUNT> \ --tx-out <CHANGE-ADDRESS>+<ADA-AMOUNT-MINUS-FEE> \ --fee <FEE> \ --out-file tx.raw

4.  Sign the transaction:

<!---->

Copy codecardano-cli transaction sign \ --tx-body-file tx.raw \ --signing-key-file payment.skey \ --testnet-magic 1097911063 \ --out-file tx.signed

This will take your raw transaction and add your digital signature authorizing it.

Submitting the Transaction

Finally, you can submit the signed transaction to the Cardano blockchain:

Copy codecardano-cli transaction submit \ --tx-file tx.signed \ --testnet-magic 1097911063

The CLI will output a transaction ID if successful. Congrats, you just sent ADA using the cardano-cli!

The cardano-cli provides granular control, but can be complex for beginners. As a developer and advanced user, taking the time to learn the cardano-cli will give you a strong foundation for building decentralized apps on Cardano.

"The cardano-cli unlocks the full capabilities of the Cardano blockchain. With some upfront learning investment, you'll have the flexibility to compose any type of transaction needed for the future of decentralized finance."
  • Blockchain developer and cardano-cli expert

Frequently Asked Questions

How do I get the cardano-cli?

The cardano-cli comes bundled with the Cardano node software, available on GitHub. Make sure to follow the build instructions to get it configured properly.

  • Build from source code
  • Download binaries
  • Use a package manager like Nix or Brew

What are some key cardano-cli commands?

Here are some of the most common and useful cardano-cli commands:

  • address - Payment address generation
  • query - Query node info and blockchain data
  • stake-address - For staking and stake pools
  • transaction - Build, sign, and submit transactions
  • node - Start, stop and query node status
  • governance - Voting/governance operations
  • wallet - Manage CLI wallets

How is the cardano-cli different from graphical wallets?

The main differences are:

  • CLI vs graphical interface
  • Advanced functionality vs ease-of-use
  • Granular control vs abstraction

The CLI gives developers and power users access to fine-grained control over transactions. Graphical wallets like Daedalus and Yoroi provide an accessible user experience that abstracts away much of the complexity behind the scenes.

Conclusion

Learning how to leverage the cardano-cli to interact with Cardano is an investment that will enable you to build robust decentralized apps and truly unlock the potential of the blockchain. With the power of the CLI, you can script customized transactions, integrate payments into apps, and manage stake pools at a low level.

Start by getting familiar with generating addresses, checking balances, and sending test transactions. As you build knowledge, refer to the cardano-cli docs and community forums for guidance on more advanced operations. The cardano-cli is a gateway to the future of decentralized finance - so begin your journey and get hands-on with it today!

Read more