~zinigor/cardano-node/trunk

« back to all changes in this revision

Viewing changes to doc/stake-pool-operations/simple_transaction.md

  • Committer: Igor Zinovyev
  • Date: 2021-08-13 19:12:27 UTC
  • Revision ID: zinigor@gmail.com-20210813191227-stlnsj3mc5ypwn0c
Tags: upstream-1.27.0
ImportĀ upstreamĀ versionĀ 1.27.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Create a simple transaction
 
2
 
 
3
Creating a transaction requires various steps:
 
4
 
 
5
* Get the protocol parameters
 
6
* Calculate the fee
 
7
* Define the time-to-live (TTL) for the transaction
 
8
* Build the transaction
 
9
* Sign the transaction
 
10
* Submit the transaction
 
11
 
 
12
#### Get protocol parameters
 
13
 
 
14
Get the protocol parameters and save them to `protocol.json` with:
 
15
 
 
16
```
 
17
cardano-cli query protocol-parameters \
 
18
  --mainnet \
 
19
  --out-file protocol.json
 
20
```
 
21
 
 
22
#### Get the transaction hash and index of the **UTXO** to spend:
 
23
 
 
24
```
 
25
cardano-cli query utxo \
 
26
  --address $(cat payment.addr) \
 
27
  --mainnet
 
28
```
 
29
 
 
30
```
 
31
                            TxHash                                 TxIx        Amount
 
32
----------------------------------------------------------------------------------------
 
33
4e3a6e7fdcb0d0efa17bf79c13aed2b4cb9baf37fb1aa2e39553d5bd720c5c99     4         20000000 lovelace
 
34
```
 
35
 
 
36
#### Draft the transaction
 
37
 
 
38
Create a draft for the transaction and save it in tx.draft
 
39
 
 
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.
 
41
 
 
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 \
 
47
    --fee 0 \
 
48
    --out-file tx.draft
 
49
 
 
50
#### Calculate the fee
 
51
 
 
52
A simple transaction needs one input, a valid UTXO from `payment.addr`, and two outputs:
 
53
 
 
54
* Output1: The address that receives the transaction.
 
55
* Output2: The address that receives the change of the transaction.
 
56
 
 
57
Note that to calculate the fee you need to include the draft transaction
 
58
 
 
59
    cardano-cli transaction calculate-min-fee \
 
60
    --tx-body-file tx.draft \
 
61
    --tx-in-count 1 \
 
62
    --tx-out-count 2 \
 
63
    --witness-count 1 \
 
64
    --byron-witness-count 0 \
 
65
    --mainnet \
 
66
    --protocol-params-file protocol.json
 
67
 
 
68
    > 167965
 
69
 
 
70
#### Calculate the change to send back to payment.addr,
 
71
all amounts must be in Lovelace:
 
72
 
 
73
    expr <UTXO BALANCE> - <AMOUNT TO SEND> - <TRANSACTION FEE>
 
74
 
 
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
 
76
 
 
77
    expr 20000000 - 10000000 - 167965
 
78
 
 
79
    > 9832035
 
80
 
 
81
#### Determine the TTL (time to Live) for the transaction
 
82
 
 
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.
 
84
 
 
85
Query the tip of the blockchain:
 
86
 
 
87
    cardano-cli query tip --mainnet
 
88
 
 
89
Look for the value of `slot`
 
90
 
 
91
    {
 
92
        "epoch": 259,
 
93
        "hash": "dbf5104ab91a7a0b405353ad31760b52b2703098ec17185bdd7ff1800bb61aca",
 
94
        "slot": 26633911,
 
95
        "block": 5580350
 
96
    }
 
97
 
 
98
Calculate your `invalid-hereafter`, for example:  26633911 + 200 slots = 26634111
 
99
 
 
100
#### Build the transaction
 
101
 
 
102
We write the transaction in a file, we will name it `tx.raw`.
 
103
 
 
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 \
 
109
    --fee 167965 \
 
110
    --out-file tx.raw
 
111
 
 
112
#### Sign the transaction
 
113
 
 
114
Sign the transaction with the signing key **payment.skey** and save the signed transaction in **tx.signed**
 
115
 
 
116
    cardano-cli transaction sign \
 
117
    --tx-body-file tx.raw \
 
118
    --signing-key-file payment.skey \
 
119
    --mainnet \
 
120
    --out-file tx.signed
 
121
 
 
122
#### Submit the transaction
 
123
 
 
124
    cardano-cli transaction submit \
 
125
    --tx-file tx.signed \
 
126
    --mainnet
 
127
 
 
128
#### Check the balances
 
129
 
 
130
We must give it some time to get incorporated into the blockchain, but eventually, we will see the effect:
 
131
 
 
132
    cardano-cli query utxo \
 
133
        --address $(cat payment.addr) \
 
134
        --mainnet
 
135
 
 
136
    >                            TxHash                                 TxIx         Amount
 
137
    > ----------------------------------------------------------------------------------------
 
138
    > b64ae44e1195b04663ab863b62337e626c65b0c9855a9fbb9ef4458f81a6f5ee     1         9832035 lovelace
 
139
 
 
140
    cardano-cli query utxo \
 
141
        --address $(cat payment2.addr) \
 
142
        --mainnet
 
143
 
 
144
    >                            TxHash                                 TxIx         Amount
 
145
    > ----------------------------------------------------------------------------------------
 
146
    > b64ae44e1195b04663ab863b62337e626c65b0c9855a9fbb9ef4458f81a6f5ee     0         10000000 lovelace
 
147
 
 
148
 
 
149
**Note**`--mainnet` identifies the Cardano mainnet, for testnets use `--testnet-magic 1097911063` instead.