1+ # uv run examples/topic_create.py
2+ # python examples/topic_create.py
13"""Create a new topic on Hedera"""
24
3- # Usage:
4- """
5- uv run examples/topic_create.py
6- python examples/topic_create.py
7- """
85import os
96import sys
107from dotenv import load_dotenv
1916
2017load_dotenv ()
2118
22- # set up the Hedera client with operator credentials
2319def setup_client ():
24- network = Network (network = 'testnet' )
25- client = Client (network )
20+ """Setup and return a Hedera client."""
21+ print ("Connecting to Hedera testnet..." )
22+ client = Client (Network (network = 'testnet' ))
2623
24+ try :
25+ operator_id = AccountId .from_string (os .getenv ('OPERATOR_ID' ))
26+ operator_key = PrivateKey .from_string (os .getenv ('OPERATOR_KEY' ))
27+ client .set_operator (operator_id , operator_key )
28+ print (f"Using operator account: { operator_id } " )
29+ return client , operator_key
30+ except (TypeError , ValueError ):
31+ print ("❌ Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file." )
32+ sys .exit (1 )
2733
28- client .set_operator (operator_id , operator_key )
29- return client , operator_key
3034
31- # create a new topic on Hedera network
3235def create_topic (client , operator_key ):
36+ """
37+ Create a new topic on Hedera
38+
39+ 1. Setup a Hedera client and operator key
40+ 2. Build a TopicCreateTransaction with a memo and admin key
41+ 3. Freeze and sign the transaction
42+ 4. Execute the transaction and get the receipt
43+ 5. Print the new topic ID if successful, or an error if not
44+ """
45+ print ("\n Creating a new topic..." )
46+
3347 transaction = (
3448 TopicCreateTransaction (
3549 memo = "Python SDK created topic" ,
@@ -50,7 +64,6 @@ def create_topic(client, operator_key):
5064 print (f"Topic creation failed: { str (e )} " )
5165 sys .exit (1 )
5266
53-
5467def main ():
5568 client , operator_key = setup_client ()
5669 create_topic (client , operator_key )
0 commit comments