forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtls_query_balance.py
More file actions
76 lines (53 loc) · 1.97 KB
/
Copy pathtls_query_balance.py
File metadata and controls
76 lines (53 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
75
76
r"""
TLS Query Balance Example.
Demonstrates how to connect to the Hedera network with TLS enabled.
Required environment variables:
- OPERATOR_ID
- OPERATOR_KEY
Optional:
- NETWORK (defaults to testnet)
- VERIFY_CERTS (set to \"true\" to enforce certificate hash checks)
Run with:
uv run examples/tls_query_balance.py
"""
import os
from dotenv import load_dotenv
from hiero_sdk_python import (
AccountId,
Client,
CryptoGetAccountBalanceQuery,
PrivateKey,
)
def _bool_env(name: str, default: bool = False) -> bool:
value = os.getenv(name)
if value is None:
return default
return value.strip().lower() in {"1", "true", "yes"}
def _load_operator_credentials() -> tuple[AccountId, PrivateKey]:
"""Load operator credentials from the environment."""
operator_id_str = os.getenv("OPERATOR_ID")
operator_key_str = os.getenv("OPERATOR_KEY")
if not operator_id_str or not operator_key_str:
raise ValueError("OPERATOR_ID and OPERATOR_KEY must be set in the environment")
operator_id = AccountId.from_string(operator_id_str)
operator_key = PrivateKey.from_string(operator_key_str)
return operator_id, operator_key
def setup_client() -> Client:
"""Setup Client."""
client = Client.from_env()
print(f"Network: {client.network.network}")
print(f"Client set up with operator id {client.operator_account_id}")
return client
def query_account_balance(client: Client, account_id: AccountId):
"""Execute a CryptoGetAccountBalanceQuery for the given account."""
query = CryptoGetAccountBalanceQuery().set_account_id(account_id)
balance = query.execute(client)
print(f"Operator account {account_id} balance: {balance.hbars.to_hbars()} hbars")
def main():
load_dotenv()
operator_id, operator_key = _load_operator_credentials()
client = setup_client()
client.set_operator(operator_id, operator_key)
query_account_balance(client, operator_id)
if __name__ == "__main__":
main()