1
# Create a simple transaction
3
Creating a transaction requires various steps:
5
* Get the protocol parameters
7
* Define the time-to-live (TTL) for the transaction
8
* Build the transaction
10
* Submit the transaction
12
#### Get protocol parameters
14
Get the protocol parameters and save them to `protocol.json` with:
17
cardano-cli query protocol-parameters \
19
--out-file protocol.json
22
#### Get the transaction hash and index of the **UTXO** to spend:
25
cardano-cli query utxo \
26
--address $(cat payment.addr) \
32
----------------------------------------------------------------------------------------
33
4e3a6e7fdcb0d0efa17bf79c13aed2b4cb9baf37fb1aa2e39553d5bd720c5c99 4 20000000 lovelace
36
#### Draft the transaction
38
Create a draft for the transaction and save it in tx.draft
40
Note that for `--tx-in` we use the following syntax: `TxHash#TxIx` where `TxHash` is the transaction hash and `TxIx` is the index; for `--tx-out` we use: `TxOut+Lovelace` where `TxOut` is the hex encoded address followed by the amount in `Lovelace`. For the transaction draft --tx-out, --invalid-hereafter and --fee can be set to zero.
42
cardano-cli transaction build-raw \
43
--tx-in 4e3a6e7fdcb0d0efa17bf79c13aed2b4cb9baf37fb1aa2e39553d5bd720c5c99#4 \
44
--tx-out $(cat payment2.addr)+0 \
45
--tx-out $(cat payment.addr)+0 \
46
--invalid-hereafter 0 \
50
#### Calculate the fee
52
A simple transaction needs one input, a valid UTXO from `payment.addr`, and two outputs:
54
* Output1: The address that receives the transaction.
55
* Output2: The address that receives the change of the transaction.
57
Note that to calculate the fee you need to include the draft transaction
59
cardano-cli transaction calculate-min-fee \
60
--tx-body-file tx.draft \
64
--byron-witness-count 0 \
66
--protocol-params-file protocol.json
70
#### Calculate the change to send back to payment.addr,
71
all amounts must be in Lovelace:
73
expr <UTXO BALANCE> - <AMOUNT TO SEND> - <TRANSACTION FEE>
75
For example, if we send 10 ADA from a UTxO containing 20 ADA, the change to send back to `payment.addr` after paying the fee is: 9.832035 ADA
77
expr 20000000 - 10000000 - 167965
81
#### Determine the TTL (time to Live) for the transaction
83
To build the transaction we need to specify the **TTL (Time to live)**, this is the slot height limit for our transaction to be included in a block, if it is not in a block by that slot the transaction will be cancelled. So TTL = slot + N slots. Where N is the amount of slots you want to add to give the transaction a window to be included in a block.
85
Query the tip of the blockchain:
87
cardano-cli query tip --mainnet
89
Look for the value of `slot`
93
"hash": "dbf5104ab91a7a0b405353ad31760b52b2703098ec17185bdd7ff1800bb61aca",
98
Calculate your `invalid-hereafter`, for example: 26633911 + 200 slots = 26634111
100
#### Build the transaction
102
We write the transaction in a file, we will name it `tx.raw`.
104
cardano-cli transaction build-raw \
105
--tx-in 4e3a6e7fdcb0d0efa17bf79c13aed2b4cb9baf37fb1aa2e39553d5bd720c5c99#4 \
106
--tx-out $(cat payment2.addr)+10000000 \
107
--tx-out $(cat payment.addr)+9832035 \
108
--invalid-hereafter 26634111 \
112
#### Sign the transaction
114
Sign the transaction with the signing key **payment.skey** and save the signed transaction in **tx.signed**
116
cardano-cli transaction sign \
117
--tx-body-file tx.raw \
118
--signing-key-file payment.skey \
122
#### Submit the transaction
124
cardano-cli transaction submit \
125
--tx-file tx.signed \
128
#### Check the balances
130
We must give it some time to get incorporated into the blockchain, but eventually, we will see the effect:
132
cardano-cli query utxo \
133
--address $(cat payment.addr) \
137
> ----------------------------------------------------------------------------------------
138
> b64ae44e1195b04663ab863b62337e626c65b0c9855a9fbb9ef4458f81a6f5ee 1 9832035 lovelace
140
cardano-cli query utxo \
141
--address $(cat payment2.addr) \
145
> ----------------------------------------------------------------------------------------
146
> b64ae44e1195b04663ab863b62337e626c65b0c9855a9fbb9ef4458f81a6f5ee 0 10000000 lovelace
149
**Note**`--mainnet` identifies the Cardano mainnet, for testnets use `--testnet-magic 1097911063` instead.