Create a simple transaction
You will need a new payment address (payment2.addr) so that you can send funds to it. First, get a new payment key pair for that:
cardano-cli address key-gen \
--verification-key-file payment2.vkey \
--signing-key-file payment2.skey
This has created two new keys: payment2.vkey and payment2.skey
To generate payment2.addr we can use the same stake key pair that we already have:
cardano-cli address build \
--payment-verification-key-file payment2.vkey \
--out-file payment2.addr \
--testnet-magic 2
Let's send 10 ada (10,000,000 lovelace) from
payment.addr
to payment2.addr
Creating a transaction is a process that requires various steps:
- Get the protocol parameters
- Draft the transaction
- Calculate the fee
- Build the transaction
- Sign the transaction
- Submit the transaction
Get the protocol parameters and save them to
protocol.json
with: cardano-cli query protocol-parameters \
--testnet-magic 2 \
--out-file protocol.jsonDetermine the TTL (Time To Live) for the transactio
We create a draft for the transaction and save it in tx.draft. This will help to calculate the transaction fees that our transaction needs to pay.
Query your address' utxo:
cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2
TxHash TxIx Amount
--------------------------------------------------------------------------------------
ce44140fe7a14a613cd381ded74e298a683973809294786df0aaefd3c5b25ba0 0 10000000000 lovelace + TxOutDatumNone
- tx-In
So at the moment we only have 1 UTXO at our payment.addr, we will use it as input (tx-in) for our transaction. We need to use the Txhash of the UTXO and it's index (TxIx)
- tx-out
We need to specify 2 transaction outputs. In the first output we send 10 ADA to payment2.addr; on the second output we send the CHANGE of the transaction to our own payment.addr.
Note that we don't know yet how much is the change that we need to send to ourselves. All transaction need to pay a transaction fee, we need to calculate the fee and subtract it from our inputs. We will calculate the fee in the next step. For now, we will use 0 for the unknown amounts:
cardano-cli transaction build-raw \
--tx-in ce44140fe7a14a613cd381ded74e298a683973809294786df0aaefd3c5b25ba0#0 \
--tx-out $(cat payment2.addr)+10000000 \
--tx-out $(cat payment.addr)+0 \
--fee 0 \
--out-file tx.draft
The transaction needs one (1) input: a valid UTXO from
payment.addr
, and two (2) outputs: The receiving address payment2.addr and an address to send the change back, in this case we use payment.addr. You also need to include the Draft transaction file. Witnesses are _**_number of signing keys used to sign the transaction.cardano-cli transaction calculate-min-fee \
--tx-body-file tx.draft \
--tx-in-count 1 \
--tx-out-count 2 \
--witness-count 1 \
--testnet-magic 2 \
--protocol-params-file protocol.json
175401 Lovelace
Remember that --testnet-magic 2
identifies the Cardano Preview Testnet. Other testnets will use other numbers, and mainnet uses --mainnet
instead.So we need to pay 175401 lovelace fee to create this transaction.
We want to send 10 ada to payment2.addr so now we need to calculate how much is the CHANGE to send back to payment.addr
expr 10000000000 - 10000000 - 175401
9989824599
Now we are ready to build the transaction body, we use
cardano-cli transaction build-raw
againNote that all the arguments will be the same for the first three lines, but we will now add the values we discovered above for the second tx-out and fees
cardano-cli transaction build-raw \
--tx-in ce44140fe7a14a613cd381ded74e298a683973809294786df0aaefd3c5b25ba0#0 \
--tx-out $(cat payment2.addr)+10000000 \
--tx-out $(cat payment.addr)+9989824599 \
--fee 175401 \
--out-file tx.raw
Sign the transaction with the signing key payment.skey and save the signed transaction in tx.signed
cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--testnet-magic 2 \
--out-file tx.signed
We have done this before, but it's worth reminding that we need to make sure that our node is running and we have set CARDANO_NODE_SOCKET_PATH
Submit the transaction:
cardano-cli transaction submit \
--tx-file tx.signed \
--testnet-magic 2
Transaction successfully submitted.
We must give it some time to get incorporated into the blockchain, but eventually, we will see the effect:
cardano-cli query utxo --address $(cat payment.addr) --testnet-magic 2
TxHash TxIx Amount
--------------------------------------------------------------------------------------
726f76b90b924b47f2cb8ab5a712c82b182a85800138bbc5294f53d517d25e7c 1 9989824599 lovelace + TxOutDatumNone
[email protected] keys % cardano-cli query utxo --address $(cat payment2.addr) --testnet-magic 2
TxHash TxIx Amount
--------------------------------------------------------------------------------------
726f76b90b924b47f2cb8ab5a712c82b182a85800138bbc5294f53d517d25e7c 0 10000000 lovelace + TxOutDatumNone
QUESTIONS AND FEEDBACK
If you have any questions and suggestions while taking the lessons please feel free to ask in the forum and we will respond as soon as possible.