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
74import os
85import sys
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"\n STEP 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
103115def main ():
0 commit comments