Skip to content

Commit 6d1d4fc

Browse files
committed
feat: add transaction lifecycle guide to documentation
Signed-off-by: Cynthia Fotso <cynthiafotso8@gmail.com>
1 parent b0a7c9d commit 6d1d4fc

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Transaction Lifecycle in the Python SDK
2+
3+
This guide explains the typical lifecycle of executing a transaction using the Hedera Python SDK. Transactions are requests to change the state of the Hedera network, such as creating accounts, transferring HBAR, or minting tokens. Understanding the lifecycle helps you avoid common pitfalls and ensures your transactions succeed.
4+
5+
## Overview
6+
7+
A typical transaction follows this flow:
8+
9+
1. **Construct** the transaction
10+
2. **Freeze** the transaction
11+
3. **Sign** the transaction
12+
4. **Execute** the transaction
13+
5. **Check the receipt**
14+
15+
The order matters because each step builds on the previous one. Skipping or reordering steps can cause errors.
16+
17+
## 1. Construct the Transaction
18+
19+
Start by creating a transaction object and populating it with the necessary data. You can use either Pythonic syntax (constructor arguments) or method chaining (fluent API).
20+
21+
### Pythonic Syntax
22+
```python
23+
from hiero_sdk_python import TokenAssociateTransaction
24+
25+
transaction = TokenAssociateTransaction(
26+
account_id=account_id,
27+
token_ids=[token_id]
28+
)
29+
```
30+
31+
### Method Chaining
32+
```python
33+
transaction = (
34+
TokenAssociateTransaction()
35+
.set_account_id(account_id)
36+
.add_token_id(token_id)
37+
)
38+
```
39+
40+
This step collects all information for the transaction body. Fields can still be modified at this point.
41+
42+
## 2. Freeze the Transaction
43+
44+
Freezing finalizes the transaction payload and makes it immutable. It sets the transaction ID, node ID, and builds the protobuf body.
45+
46+
```python
47+
transaction.freeze_with(client)
48+
```
49+
50+
- **Why freeze?** Hedera requires a consistent payload for signing and execution. Freezing prevents accidental changes.
51+
- **When to freeze?** Always before signing. Some transactions auto-freeze during execution, but manual freezing is recommended for clarity.
52+
- **What happens if you don't freeze?** Signing or executing may fail or behave unexpectedly.
53+
54+
## 3. Sign the Transaction
55+
56+
Hedera uses cryptographic signatures for authorization. Sign with the required keys (e.g., operator key, admin keys).
57+
58+
```python
59+
transaction.sign(account_private_key)
60+
```
61+
62+
- **Who signs?** The operator (client account) often signs automatically, but additional keys may be needed (e.g., supply key for minting).
63+
- **Multiple signatures?** Call `.sign()` multiple times if required.
64+
- **Order?** Sign after freezing, as the payload must be finalized.
65+
66+
## 4. Execute the Transaction
67+
68+
Submit the transaction to the Hedera network. This returns a `TransactionResponse` indicating the network received it.
69+
70+
```python
71+
response = transaction.execute(client)
72+
```
73+
74+
- **Does this guarantee success?** No! It only confirms receipt. The network processes it asynchronously.
75+
- **What if you skip signing?** Execution will fail with an authorization error.
76+
77+
## 5. Check the Receipt
78+
79+
Fetch and verify the transaction receipt to confirm processing.
80+
81+
```python
82+
receipt = response.get_receipt(client)
83+
84+
if receipt.status != ResponseCode.SUCCESS:
85+
print(f"Transaction failed: {ResponseCode(receipt.status).name}")
86+
else:
87+
print("Transaction successful!")
88+
```
89+
90+
- **Why check?** Receipts provide the final status and any generated IDs (e.g., new account ID).
91+
- **Common statuses:** `SUCCESS`, `INSUFFICIENT_ACCOUNT_BALANCE`, `INVALID_SIGNATURE`.
92+
93+
## Complete Example
94+
95+
Here's a clean example associating a token with an account:
96+
97+
```python
98+
import sys
99+
from hiero_sdk_python import TokenAssociateTransaction, ResponseCode
100+
101+
def associate_token_with_account(client, account_id, account_private_key, token_id):
102+
"""Associate a token with an account."""
103+
104+
receipt = (
105+
TokenAssociateTransaction()
106+
.set_account_id(account_id)
107+
.add_token_id(token_id)
108+
.freeze_with(client) # Lock fields
109+
.sign(account_private_key) # Authorize
110+
.execute(client) # Submit to Hedera
111+
.get_receipt(client) # Fetch result
112+
)
113+
114+
if receipt.status != ResponseCode.SUCCESS:
115+
print(f"Token association failed: {ResponseCode(receipt.status).name}")
116+
sys.exit(1)
117+
118+
print("Token associated successfully!")
119+
```
120+
121+
For more examples, see `examples/token_grant_kyc.py` or `examples/token_associate.py`.
122+
123+
## Correct vs. Incorrect Order
124+
125+
### Correct
126+
```python
127+
transaction = TokenAssociateTransaction().set_account_id(account_id).freeze_with(client).sign(key).execute(client)
128+
receipt = response.get_receipt(client)
129+
```
130+
131+
### Incorrect (Signing before freezing)
132+
```python
133+
transaction = TokenAssociateTransaction().set_account_id(account_id).sign(key).freeze_with(client) # Error: Cannot sign unfrozen transaction
134+
```
135+
136+
### Incorrect (Modifying after freezing)
137+
```python
138+
transaction = TokenAssociateTransaction().set_account_id(account_id).freeze_with(client)
139+
transaction.set_account_id(new_id) # Error: Transaction is immutable
140+
```
141+
142+
## Flow Diagram
143+
144+
```
145+
[Construct] → [Freeze] → [Sign] → [Execute] → [Check Receipt]
146+
↓ ↓ ↓ ↓ ↓
147+
Build data Finalize Authorize Submit Verify status
148+
```
149+
150+
## Common Pitfalls
151+
152+
- **Forgetting to freeze:** Leads to runtime errors during signing.
153+
- **Wrong signer:** Use the correct key (e.g., supply key for minting).
154+
- **Ignoring receipt:** Always check status; don't assume success.
155+
- **Auto-freeze/sign:** Works for simple transactions but can hide issues in complex ones.
156+
- **Order dependency:** Construct → Freeze → Sign → Execute → Receipt.
157+
158+
For more details, refer to the SDK documentation or community calls on [Discord](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/discord.md).

0 commit comments

Comments
 (0)