|
| 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