Skip to content

Commit 14cda1e

Browse files
refactor: use Client.from_env() in topic_create_transaction.py (#1619)
Signed-off-by: Siddhartha Ganguly <[email protected]> Signed-off-by: gangulysiddhartha22-cmyk <[email protected]>
1 parent 9607c9c commit 14cda1e

File tree

2 files changed

+16
-56
lines changed

2 files changed

+16
-56
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
133133
- Added dry-run support and refactored `.github/workflows/bot-workflows.yml` to use dedicated script `.github/scripts/bot-workflows.js` for improved maintainability and testability. (`#1288`)
134134

135135
### Changed
136-
136+
- Refactored `examples/consensus/topic_create_transaction.py` to use `Client.from_env()` (#1611)
137137
- Updated GitHub Actions setup-node action to v6.2.0.
138138
- chore: format tests/unit/mock_server.py with black (#1542)
139139
- Updated actions/checkout to v6.0.1 and actions/github-script v8.0.0 in bot-next-issue-recommendation workflow (#1586)

examples/consensus/topic_create_transaction.py

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,17 @@
22
uv run examples/consensus/topic_create_transaction.py
33
python examples/consensus/topic_create_transaction.py
44
"""
5+
from hiero_sdk_python import Client, TopicCreateTransaction, ResponseCode, PrivateKey
56

6-
import os
7-
import sys
8-
from typing import Tuple
9-
from dotenv import load_dotenv
10-
11-
from hiero_sdk_python import (
12-
Client,
13-
AccountId,
14-
PrivateKey,
15-
TopicCreateTransaction,
16-
Network,
17-
)
18-
19-
# Load environment variables from .env file
20-
load_dotenv()
21-
network_name = os.getenv("NETWORK", "testnet").lower()
22-
23-
24-
def setup_client() -> Tuple[Client, PrivateKey]:
7+
def setup_client():
258
"""
26-
Sets up and configures the Hiero client for the testnet.
27-
Reads OPERATOR_ID and OPERATOR_KEY from environment variables.
9+
Sets up and configures the Hiero client.
10+
Reads OPERATOR_ID and OPERATOR_KEY from environment variables via Client.from_env().
2811
"""
29-
network = Network(network_name)
30-
print(f"Connecting to Hedera {network_name} network!")
31-
client = Client(network)
32-
33-
operator_id_str = os.getenv("OPERATOR_ID")
34-
operator_key_str = os.getenv("OPERATOR_KEY")
35-
36-
# Check if the environment variables are loaded correctly
37-
if not operator_id_str or not operator_key_str:
38-
print("Error: OPERATOR_ID or OPERATOR_KEY not found in environment.")
39-
print("Please create a .env file in the project's root directory with:")
40-
print("\nOPERATOR_ID=your_id_here")
41-
print("OPERATOR_KEY=your_key_here\n")
42-
sys.exit(1)
43-
44-
try:
45-
operator_id = AccountId.from_string(operator_id_str)
46-
operator_key = PrivateKey.from_string(operator_key_str)
47-
except (TypeError, ValueError) as e:
48-
print(f"Error: Invalid OPERATOR_ID or OPERATOR_KEY format: {e}")
49-
sys.exit(1)
50-
51-
client.set_operator(operator_id, operator_key)
12+
client = Client.from_env()
13+
print(f"Network: {client.network.network}")
5214
print(f"Client set up with operator id {client.operator_account_id}")
53-
return client, operator_key
54-
15+
return client, client.operator_private_key
5516

5617
def create_topic(client: Client, operator_key: PrivateKey):
5718
"""
@@ -64,18 +25,18 @@ def create_topic(client: Client, operator_key: PrivateKey):
6425
.freeze_with(client)
6526
.sign(operator_key)
6627
)
67-
6828
try:
6929
receipt = transaction.execute(client)
70-
if receipt and receipt.topic_id:
71-
print(f"Success! Topic created with ID: {receipt.topic_id}")
72-
else:
30+
if receipt.status != ResponseCode.SUCCESS:
31+
print(f"Topic creation failed: {ResponseCode(receipt.status).name}")
32+
raise SystemExit(1)
33+
if not receipt.topic_id:
7334
print("Topic creation failed: Topic ID not returned in receipt.")
74-
sys.exit(1)
35+
raise SystemExit(1)
36+
print(f"Success! Topic created with ID: {receipt.topic_id}")
7537
except Exception as e:
7638
print(f"Topic creation failed: {str(e)}")
77-
sys.exit(1)
78-
39+
raise SystemExit(1)
7940

8041
def main():
8142
"""
@@ -84,6 +45,5 @@ def main():
8445
client, operator_key = setup_client()
8546
create_topic(client, operator_key)
8647

87-
8848
if __name__ == "__main__":
89-
main()
49+
main()

0 commit comments

Comments
 (0)