forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_topic_info.py
More file actions
74 lines (62 loc) · 1.97 KB
/
query_topic_info.py
File metadata and controls
74 lines (62 loc) · 1.97 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
"""
uv run examples/query_topic_info.py
python examples/query_topic_info.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Network,
Client,
AccountId,
PrivateKey,
TopicInfoQuery,
TopicCreateTransaction
)
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 = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
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 query_topic_info():
"""
A full example that create a topic and query topic info for that topic.
"""
# Config Client
client, _, operator_key = setup_client()
# Create a new Topic
topic_id = create_topic(client, operator_key)
# Query Topic Info
print("\nSTEP 2: Querying Topic Info...")
query = TopicInfoQuery().set_topic_id(topic_id)
topic_info = query.execute(client)
print("✅ Success! Topic Info:", topic_info)
if __name__ == "__main__":
query_topic_info()