Skip to content

Commit c76143c

Browse files
authored
chore(examples): refactor examples/topic_delete.py to be more modular (#528)
Signed-off-by: Adityarya11 <[email protected]>
1 parent 88be864 commit c76143c

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
3434
### Fixed
3535

3636
- Add type hints to `setup_client()` and `create_new_account()` functions in `examples/account_create.py` (#418)
37+
- refactored examples/topic_delete.py to be more modular (`added topic_delete_transaction()` and `main()`).
38+
39+
### Fixed
40+
3741
- Added explicit read and write permissions to test.yml
3842

3943
## [0.1.6] - 2025-10-21

examples/topic_delete.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
uv run examples/topic_delete.py
33
python 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+
610
import os
711
import sys
812
from dotenv import load_dotenv
@@ -19,21 +23,28 @@
1923

2024
load_dotenv()
2125

26+
2227
def 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+
3748
def create_topic(client, operator_key):
3849
"""Create a new topic"""
3950
print("\nSTEP 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("\nSTEP 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+
85105
if __name__ == "__main__":
86-
delete_topic()
106+
main()

0 commit comments

Comments
 (0)