forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic_delete.py
More file actions
106 lines (86 loc) · 2.98 KB
/
topic_delete.py
File metadata and controls
106 lines (86 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
uv run examples/topic_delete.py
python examples/topic_delete.py
Refactored to be more modular:
- topic_delete_transaction() performs the create+delete transaction steps
- main() orchestrates setup and calls helper functions
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
TopicDeleteTransaction,
Network,
TopicCreateTransaction,
ResponseCode
)
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
print("Connecting to Hedera testnet...")
client = Client(Network(os.getenv('NETWORK')))
try:
operator_id_str = os.getenv('OPERATOR_ID')
operator_key_str = os.getenv('OPERATOR_KEY')
if not operator_id_str or not operator_key_str:
print("Error: OPERATOR_ID or OPERATOR_KEY not set in .env file")
sys.exit(1)
operator_id = AccountId.from_string(operator_id_str)
operator_key = PrivateKey.from_string(operator_key_str)
client.set_operator(operator_id, operator_key)
return client, operator_id, operator_key
except (TypeError, ValueError):
print("Error: Creating client, Please check your .env file")
sys.exit(1)
def create_topic(client, operator_key):
"""Create a new topic"""
print("\nSTEP 1: Creating a Topic...")
try:
topic_tx = (
TopicCreateTransaction(
memo="Python SDK created topic",
admin_key=operator_key.public_key()
)
.freeze_with(client)
.sign(operator_key)
)
topic_receipt = topic_tx.execute(client)
topic_id = topic_receipt.topic_id
print(f"✅ Success! Created topic: {topic_id}")
return topic_id
except Exception as e:
print(f"Error: Creating topic: {e}")
sys.exit(1)
def topic_delete_transaction(client, operator_key, topic_id):
"""
Perform the topic delete transaction for the given topic_id.
Separated so it can be called independently in tests or other scripts.
"""
print("\nSTEP 2: Deleting Topic...")
transaction = (
TopicDeleteTransaction(topic_id=topic_id)
.freeze_with(client)
.sign(operator_key)
)
try:
receipt = transaction.execute(client)
print(f"Topic Delete Transaction completed: "
f"(status: {ResponseCode(receipt.status).name}, "
f"transaction_id: {receipt.transaction_id})")
print(f"✅ Success! Topic {topic_id} deleted successfully.")
except Exception as e:
print(f"Error: Topic deletion failed: {str(e)}")
sys.exit(1)
def main():
"""Orchestrator — runs the example start-to-finish"""
# Config Client
client, _, operator_key = setup_client()
# Create a new Topic
topic_id = create_topic(client, operator_key)
# Delete the topic
topic_delete_transaction(client, operator_key, topic_id)
if __name__ == "__main__":
main()