Backend Helpers | Automation and Software Development for Cloud Applicationses

Creating Transactions in Cardano

Cardano is a public, open source, research driven decentralized blockchain platform that uses proof of stake consensus algorithm. In this post, we are going to demonstrate how to create transaction in the test network.

Getting a Cardano Node Running in the Testnet


In order to send create transactions, you will need a cardano node running. We demonstrate in a previous post how to run a cardano node using docker. You can also check other options for running your development node in the stake pool operators course


Querying the Cardano Network


cardano-cli is an application that is installed as part of the cardano node. The first step we need to do in order to use it is to export theCARDANO_NODE_SOCKET_PATH and point to the cardano socket file, for example:



export CARDANO_NODE_SOCKET_PATH="/opt/cardano/db/node.socket"

The query tip command queries the blockchain and returns some basic information:


$cardano-cli query tip --testnet-magic 1

{
    "block": 897914,
    "epoch": 67,
    "era": "Babbage",
    "hash": "bfe59e384f3df28fb275d427ee13058c61d2e99194ca4ebe139f9817aff422bb",
    "slot": 27437634,
    "syncProgress": "100.00"
}



The --testnet-magic 1 parameter indicates that we are querying the testnet

Cardano Wallet Key Pairs and Addresses

Cardano key pairs follow the ed25519 which involves a public verification key and a private key.Please remember that public keys can be shared with anyone but private keys must be ketp safe and secret

Cardano wallet addresses have two parts:

  • Payment address: Used to store, receive, and send money
  • Stake address: Used to store and withdraw rewards. I also defines stake pool parameters such as ownership and rewards.

The following command will create a set of payment keypair that includes a public keypayment.vkey and a private key payment.skey


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



We can create as many keypair as we want, the following command creates a second set of keys that we are going to use to demonstrate how to send ADAs from one address to another:


cardano-cli address key-gen \
 --verification-key-file payment2.vkey \
 --signing-key-file payment2.skey



The following command creates a keypair set of stake keys:


cardano-cli stake-address key-gen     --verification-key-file stake.vkey     --signing-key-file stake.skey


After creating the keypair set we can create now two payment addresses: one where we are going to deposition some ADAs for testing porpoises and a second one for demonstrate how to create a transaction:


cardano-cli address build \
    --payment-verification-key-file payment.vkey \
    --stake-verification-key-file stake.vkey \
    --testnet-magic 1 \
    --out-file payment.addr     


We use the second set of keys for creating the second payment address:


cardano-cli address build \
    --payment-verification-key-file payment2.vkey \
    --stake-verification-key-file stake2.vkey \
    --testnet-magic 1 \
    --out-file payment2.addr     


Getting some ADAs for Testing

You can request some funds from the Testnets faucet in order to create transactions in the test network


After getting your ADAs you can query the balance of your address:


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


You will see an output showing the balance for your address:


                           TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
b7a4e98d6bc0758d7bfd2453712e89abb9c27cca5a77477c6ef890b06839c137     0        10000000000 lovelace + TxOutDatumNone


Creating a transaction

Cardano implements theUXTO model for recording the transactions in the blockchain. We are going to create a transaction to send 2 ADAs from address1 to address2. The following steps are required in order to create a transaction in the blockchain:

  • Get the protocol parameters
  • Calculate the fee
  • Define the time-to-live (TTL) for the transaction
  • Build the transaction
  • Sign the transaction
  • Submit the transaction

Step 1: Get the protocol parameters

The following command gets the parameters for the testnet:


    cardano-cli query protocol-parameters \
    --testnet-magic 1 \
    --out-file protocol_test.json

Step 2: Calculate the fee

For calculating the fee, we need to create a draft transaction first. The cardano-cli transaction build-rawcreates a raw transaction that we will use as draft to compute the fee. This command expects the following parameters:

  • --tx-in: Transaction input with the syntax TxHash#TxIx where TxHash is the transaction hash and TxIx is the index
  • --tx-out: Transaction output. Every transaction have at least 2 outputs: the address that receives the transaction and the address that receives the change of the transaction

--invalid-hereafter and --feecan be set to 0 for computing the transaction draft:


cardano-cli transaction build-raw \
    --tx-in b7a4e98d6bc0758d7bfd2453712e89abb9c27cca5a77477c6ef890b06839c137#0 \
    --tx-out $(cat payment.addr)+0 \
    --tx-out $(cat payment2.addr)+0 \
    --invalid-hereafter 0 \
    --fee 0 \
    --out-file tx.draft

After generating the draft, we use the cardano-cli transaction calculate-min-fee to compute the fee:


cardano-cli transaction calculate-min-fee \
    --tx-body-file tx.draft \
    --tx-in-count 1 \
    --tx-out-count 2 \
    --witness-count 1 \
    --byron-witness-count 0 \
    --testnet-magic 1 \
    --protocol-params-file protocol_test.json

The output of the above command is 176721 Lovelace. With this value we can now calculate the change to send back to payment.addr after paying the fee: 9997.8320350 ADA (all values must be expressed in Lovelace)


expr 10000000000 - 2000000 - 176721
9997823279

Step 3: Define the time-to-live (TTL) for the transaction

The TTL (Time to live) is the slot height limit for a transaction to be included in a block. The transaction will be canceled if is not included on a block before this limit.TTL = slot + N slots . For querying the current slot we use the query tip command:


cardano-cli query tip --testnet-magic 1
{
    "block": 897914,
    "epoch": 67,
    "era": "Babbage",
    "hash": "bfe59e384f3df28fb275d427ee13058c61d2e99194ca4ebe139f9817aff422bb",
    "slot": 27437634,
    "syncProgress": "100.00"
}

The query tip command told us that the current slot at that time was 27437634. With this information we can calculate a TTL , for example, 500 slots in the future:


expr 27437634 + 500
27438134

Step 4: Building the transaction

We use the cardano-cli transaction build-rawto create the transaction:


cardano-cli transaction build-raw \
    --tx-in b7a4e98d6bc0758d7bfd2453712e89abb9c27cca5a77477c6ef890b06839c137#0 \
    --tx-out $(cat payment2.addr)+2000000 \
    --tx-out $(cat payment.addr)+9997823279 \
    --invalid-hereafter 27438134 \
    --fee 176721 \
    --out-file tx.raw

Step 5: Sign the transaction

We use the cardano-cli transaction sign command with the signing key payment.skey and save the signed transaction in the output file tx.signed:


cardano-cli transaction sign \
    --tx-body-file tx.raw \
    --signing-key-file payment.skey \
    --testnet-magic 1 \
    --out-file tx.signed

Step 6: Submit the transaction

Finally we can use the cardano-cli transaction submit command to submit the transaction to the blockchain:


    cardano-cli transaction submit \
    --tx-file /root/transactions/tx.signed \
    --testnet-magic 1

Transaction successfully submitted.

Check the transaction status

You can verify if the transaction has been included in the blockchain by querying the balance for addr1 and addr2:


/cardano-cli query utxo \
    --address $(cat payment.addr) \
    --testnet-magic 1


                          TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
daa4f16c9671989f4e79eb2b505e4435c8afcf73a24847c77bde733ae2edf63f     1        9997823279 lovelace + TxOutDatumNone

cardano-cli query utxo \
    --address $(cat payment2.addr) \
    --testnet-magic 1


                           TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
daa4f16c9671989f4e79eb2b505e4435c8afcf73a24847c77bde733ae2edf63f     0        2000000 lovelace + TxOutDatumNone

Conclusion

Cardano is considered a third generation blockchain. It provides very interesting features such as the introduction of proof of stake as consensus algorithm. In this post we learned how to create transactions in the test network.