Skip to content

Commit 78657fb

Browse files
authored
chore: Add training documentation for transaction receipts (#873)
Signed-off-by: Adityarya11 <arya050411@gmail.com>
1 parent 35b90bb commit 78657fb

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
3131
- Added `examples/token_create_transaction_token_fee_schedule.py` to demonstrate creating tokens with custom fee schedules and the consequences of not having it.
3232
- Added `examples/token_create_transaction_wipe_key.py` to demonstrate token wiping and the role of the wipe key.
3333
- Added `examples/account_allowance_approve_transaction_hbar.py` and `examples/account_allowance_delete_transaction_hbar.py`, deleted `examples/account_allowance_hbar.py`. [#775]
34-
34+
- Added `docs\sdk_developers\training\receipts.md` as a training guide for users to understand hedera receipts.
3535

3636
### Changed
3737
- Upgraded step-security/harden-runner v2.13.2
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Understanding Transaction Receipts
2+
3+
Every transaction you submit to the Hedera network—whether it creates a token, transfers HBAR, or deletes an account—produces a **Receipt**.
4+
5+
This guide explains what a receipt is, why you must fetch it, and how to inspect it using the Python SDK.
6+
7+
## What Is a Transaction Receipt?
8+
9+
A transaction receipt is the network’s official confirmation of the transaction's final outcome. It tells you:
10+
11+
1. **Status:** Whether the transaction reached consensus and succeeded (`SUCCESS`) or failed.
12+
2. **Created Objects:** If you created something (like a Token or Account), the IDs are stored here.
13+
3. **Consensus Timestamp:** The exact moment the network agreed the transaction happened.
14+
15+
### ⚠️ Why You Must Check the Receipt
16+
17+
When you run `transaction.execute(client)`, the SDK submits the transaction to a node. If the node accepts it, your script continues. **However, this does not mean the transaction succeeded.**
18+
19+
Without checking the receipt:
20+
21+
- A token might not have been created.
22+
- A transfer might have failed due to insufficient balance.
23+
- **Your script could silently fail** while appearing to work.
24+
25+
## 🛠️ How to Get a Receipt in Python
26+
27+
In the Hiero Python SDK, the `execute()` method resolves to the transaction receipt. You should immediately check the `.status` property.
28+
29+
```python
30+
import sys
31+
from hiero_sdk_python import TokenAssociateTransaction, ResponseCode
32+
33+
# 1. Execute the transaction
34+
receipt = (
35+
TokenAssociateTransaction()
36+
.set_account_id(account_id)
37+
.add_token_id(token_id)
38+
.freeze_with(client)
39+
.sign(account_private_key)
40+
.execute(client)
41+
)
42+
43+
# 2. Check the status
44+
if receipt.status != ResponseCode.SUCCESS:
45+
# The transaction failed (e.g., wrong key, account full, etc.)
46+
print(f"Token association failed with status: {ResponseCode(receipt.status).name}")
47+
sys.exit(1)
48+
else:
49+
print("Token associated successfully!")
50+
```
51+
52+
## 🔍 Extracting Information from a Receipt
53+
54+
Receipts contain specific fields depending on the type of transaction you executed.
55+
56+
| Transaction Type | Key Receipt Field | Description |
57+
| :----------------- | :------------------- | :---------------------------------------------------- |
58+
| **Token Create** | `receipt.token_id` | The ID of the newly created token (e.g., `0.0.12345`) |
59+
| **Account Create** | `receipt.account_id` | The ID of the new account |
60+
| **Topic Create** | `receipt.topic_id` | The ID of the new consensus topic |
61+
| **File Create** | `receipt.file_id` | The ID of the uploaded file |
62+
63+
### Example: Getting a New Token ID
64+
65+
When creating a token, you need the receipt to find out what your new Token ID is so you can use it later.
66+
67+
```python
68+
from hiero_sdk_python import TokenCreateTransaction, ResponseCode
69+
70+
# 1. Create the transaction
71+
create_tx = (
72+
TokenCreateTransaction()
73+
.set_token_name("Example Token")
74+
.set_symbol("EXT")
75+
.set_initial_supply(1000)
76+
.set_treasury_account_id(operator_id)
77+
.freeze_with(client)
78+
.sign(operator_key)
79+
)
80+
81+
# 2. Execute and get receipt
82+
receipt = create_tx.execute(client)
83+
84+
# 3. Validate Success
85+
if receipt.status != ResponseCode.SUCCESS:
86+
print(f"Token creation failed: {ResponseCode(receipt.status).name}")
87+
sys.exit(1)
88+
89+
# 4. Extract the Token ID
90+
token_id = receipt.token_id
91+
print(f"🎉 Created new token with ID: {token_id}")
92+
```
93+
94+
## Summary
95+
96+
1. **Always check the status:** `if receipt.status != ResponseCode.SUCCESS`.
97+
2. **Use the receipt for IDs:** Retrieve `token_id` or `account_id` from the receipt object after a successful creation.
98+
3. **Handle errors:** Never assume a transaction worked just because the code didn't crash during execution.

0 commit comments

Comments
 (0)