Skip to content

Commit fc17745

Browse files
authored
chore: enhance client setup
Signed-off-by: MontyPokemon <[email protected]>
1 parent f7b5355 commit fc17745

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

examples/topic_create.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
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-
"""
85
import os
96
import sys
107
from dotenv import load_dotenv
@@ -19,17 +16,34 @@
1916

2017
load_dotenv()
2118

22-
# set up the Hedera client with operator credentials
2319
def 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
3235
def 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("\nCreating 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-
5467
def main():
5568
client, operator_key = setup_client()
5669
create_topic(client, operator_key)

0 commit comments

Comments
 (0)