Skip to content

Commit ad0b026

Browse files
refactor: improve token_delete_transaction (#885)
Signed-off-by: tech0priyanshu <[email protected]>
1 parent 874ddcd commit ad0b026

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
1515
- Document error handling patterns and best practices for transaction receipts
1616

1717
- fix `pull_request` to `pull_request_target` in `bot-verified-commits.yml`
18+
- Add more robust receipt checks and removed fallback to `examples/tokens/token_delete_transaction.py`
1819
- Add detail to `token_airdrop.py` and `token_airdrop_cancel.py`
1920
- Add workflow: github bot to respond to unverified PR commits (#750)
2021
- Add workflow: bot workflow which notifies developers of workflow failures in their pull requests.
@@ -57,6 +58,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
5758
- changed to add concurrency to workflow bot
5859

5960
### Fixed
61+
6062
- chore: updated solo action to avoid v5
6163
- chore: fix test.yml workflow to log import errors (#740)
6264
- chore: fixed integration test names without a test prefix or postfix

examples/tokens/token_delete_transaction.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
# uv run examples/tokens/token_delete.py
2-
# python examples/tokens/token_delete.py
3-
"""
4-
A full example that creates a token and then immediately deletes it.
5-
"""
1+
# uv run examples/tokens/token_delete_transaction.py
2+
# python examples/tokens/token_delete_transaction.py
63

74
import os
85
import sys
@@ -15,6 +12,7 @@
1512
Network,
1613
TokenCreateTransaction,
1714
TokenDeleteTransaction,
15+
ResponseCode,
1816
)
1917

2018
# Load environment variables from .env file
@@ -64,17 +62,24 @@ def create_new_token(client, operator_id, operator_key, admin_key):
6462
.set_treasury_account_id(operator_id)
6563
.set_admin_key(admin_key) # Use the newly generated admin key
6664
.freeze_with(client)
67-
.sign(operator_key) # Operator (treasury) must sign
68-
.sign(admin_key) # The new admin key must also sign
65+
.sign(operator_key) # Operator (treasury) must sign
66+
.sign(admin_key) # The new admin key must also sign
6967
)
7068

7169
create_receipt = create_tx.execute(client)
70+
71+
# Verify the receipt status
72+
rc = ResponseCode(create_receipt.status)
73+
if rc != ResponseCode.SUCCESS:
74+
print(f"❌ Token creation failed with status: {rc.name}")
75+
sys.exit(1)
76+
7277
token_id_to_delete = create_receipt.token_id
73-
print(f"✅ Success! Created token with ID: {token_id_to_delete}")
78+
print(f"✅ Token created successfully: {token_id_to_delete}")
7479
return token_id_to_delete
7580

76-
except (ValueError, TypeError) as e:
77-
print(f"❌ Error creating token: {e}")
81+
except Exception as e:
82+
print(f"❌ Error creating token: {repr(e)}")
7883
sys.exit(1)
7984

8085

@@ -87,17 +92,24 @@ def delete_token(admin_key, token_id_to_delete, client, operator_key):
8792
print(f"\nSTEP 2: Deleting token {token_id_to_delete}...")
8893
delete_tx = (
8994
TokenDeleteTransaction()
90-
.set_token_id(token_id_to_delete) # Use the ID from the token we just made
91-
.freeze_with(client)
92-
.sign(operator_key) # Operator must sign
93-
.sign(admin_key) # Sign with the same admin key used to create it
95+
.set_token_id(token_id_to_delete) # Use the ID from the token we just made
96+
.freeze_with(client) # Use the ID from the token we just made
97+
.sign(operator_key) # Operator must sign
98+
.sign(admin_key) # Sign with the same admin key used to create it
9499
)
95100

96-
delete_tx.execute(client)
97-
print("✅ Success! Token deleted.")
101+
delete_receipt = delete_tx.execute(client)
102+
103+
# Verify deletion receipt status
104+
rc = ResponseCode(delete_receipt.status)
105+
if rc != ResponseCode.SUCCESS:
106+
print(f"❌ Token deletion failed with status: {rc.name}")
107+
sys.exit(1)
108+
109+
print(f"✅ Token {token_id_to_delete} deleted successfully!")
98110

99-
except (ValueError, TypeError) as e:
100-
print(f"❌ Error deleting token: {e}")
111+
except Exception as e:
112+
print(f"❌ Error deleting token: {repr(e)}")
101113
sys.exit(1)
102114

103115
def main():

0 commit comments

Comments
 (0)