22uv run examples/topic_delete.py
33python examples/topic_delete.py
44
5+ Refactored to be more modular:
6+ - topic_delete_transaction() performs the create+delete transaction steps
7+ - main() orchestrates setup and calls helper functions
58"""
9+
610import os
711import sys
812from dotenv import load_dotenv
1923
2024load_dotenv ()
2125
26+
2227def setup_client ():
2328 """Initialize and set up the client with operator account"""
2429 print ("Connecting to Hedera testnet..." )
2530 client = Client (Network (network = 'testnet' ))
2631
2732 try :
28- operator_id = AccountId .from_string (os .getenv ('OPERATOR_ID' ))
29- operator_key = PrivateKey .from_string (os .getenv ('OPERATOR_KEY' ))
33+ operator_id_str = os .getenv ('OPERATOR_ID' )
34+ operator_key_str = os .getenv ('OPERATOR_KEY' )
35+ if not operator_id_str or not operator_key_str :
36+ print ("Error: OPERATOR_ID or OPERATOR_KEY not set in .env file" )
37+ sys .exit (1 )
38+ operator_id = AccountId .from_string (operator_id_str )
39+ operator_key = PrivateKey .from_string (operator_key_str )
3040 client .set_operator (operator_id , operator_key )
3141
3242 return client , operator_id , operator_key
3343 except (TypeError , ValueError ):
34- print ("❌ Error: Creating client, Please check your .env file" )
44+ print ("Error: Creating client, Please check your .env file" )
3545 sys .exit (1 )
3646
47+
3748def create_topic (client , operator_key ):
3849 """Create a new topic"""
3950 print ("\n STEP 1: Creating a Topic..." )
@@ -52,19 +63,15 @@ def create_topic(client, operator_key):
5263
5364 return topic_id
5465 except Exception as e :
55- print (f"❌ Error: Creating topic: { e } " )
66+ print (f"Error: Creating topic: { e } " )
5667 sys .exit (1 )
5768
5869
59- def delete_topic ():
60- """A example to create a topic and then delete it"""
61- # Config Client
62- client , _ , operator_key = setup_client ()
63-
64- # Create a new Topic
65- topic_id = create_topic (client , operator_key )
66-
67- # Delete the topic
70+ def topic_delete_transaction (client , operator_key , topic_id ):
71+ """
72+ Perform the topic delete transaction for the given topic_id.
73+ Separated so it can be called independently in tests or other scripts.
74+ """
6875 print ("\n STEP 2: Deleting Topic..." )
6976 transaction = (
7077 TopicDeleteTransaction (topic_id = topic_id )
@@ -79,8 +86,21 @@ def delete_topic():
7986 f"transaction_id: { receipt .transaction_id } )" )
8087 print (f"✅ Success! Topic { topic_id } deleted successfully." )
8188 except Exception as e :
82- print (f"❌ Error: Topic deletion failed: { str (e )} " )
89+ print (f"Error: Topic deletion failed: { str (e )} " )
8390 sys .exit (1 )
8491
92+
93+ def main ():
94+ """Orchestrator — runs the example start-to-finish"""
95+ # Config Client
96+ client , _ , operator_key = setup_client ()
97+
98+ # Create a new Topic
99+ topic_id = create_topic (client , operator_key )
100+
101+ # Delete the topic
102+ topic_delete_transaction (client , operator_key , topic_id )
103+
104+
85105if __name__ == "__main__" :
86- delete_topic ()
106+ main ()
0 commit comments