forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_payment.py
More file actions
174 lines (143 loc) · 6.23 KB
/
query_payment.py
File metadata and controls
174 lines (143 loc) · 6.23 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
uv run examples/query_payment.py
python examples/query_payment.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
Network,
)
from hiero_sdk_python.hapi.services.basic_types_pb2 import TokenType
from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery
from hiero_sdk_python.hbar import Hbar
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.tokens.supply_type import SupplyType
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
network = Network(os.getenv('NETWORK'))
client = Client(network)
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
def create_fungible_token(client, operator_id, operator_key):
"""Create a fungible token"""
receipt = (
TokenCreateTransaction()
.set_token_name("MyExampleFT")
.set_token_symbol("EXFT")
.set_decimals(2)
.set_initial_supply(100)
.set_treasury_account_id(operator_id)
.set_token_type(TokenType.FUNGIBLE_COMMON)
.set_supply_type(SupplyType.FINITE)
.set_max_supply(1000)
.set_admin_key(operator_key)
.set_supply_key(operator_key)
.execute(client)
)
if receipt.status != ResponseCode.SUCCESS:
print(f"Fungible token creation failed with status: {ResponseCode.get_name(receipt.status)}")
sys.exit(1)
token_id = receipt.token_id
print(f"Fungible token created with ID: {token_id}")
return token_id
def demonstrate_zero_cost_balance_query(client, account_id):
"""
Demonstrate cost calculation for queries that don't require payment.
CryptoGetAccountBalanceQuery is an example of a query that doesn't require payment.
For such queries:
- get_cost() returns 0 Hbar when no payment is set
- get_cost() returns the set payment amount when payment is set
"""
print("\nQueries that DON'T require payment:\n")
# Case 1: No payment set - should return 0 Hbar cost
print("When no payment is set:")
query_no_payment = CryptoGetAccountBalanceQuery().set_account_id(account_id)
cost_no_payment = query_no_payment.get_cost(client)
print(f"Cost: {cost_no_payment} Hbar")
print("Expected: 0 Hbar (payment not required)")
# Execute the query (should work without payment)
print("\nExecuting query without payment...")
result = query_no_payment.execute(client)
print(f"Query executed successfully!")
print(f" Account balance (only hbars): {result.hbars}")
# Case 2: Payment set - should return the set payment amount
print("\nWhen custom payment is set:")
custom_payment = Hbar(2)
query_with_payment = (
CryptoGetAccountBalanceQuery()
.set_account_id(account_id)
.set_query_payment(custom_payment)
)
cost_with_payment = query_with_payment.get_cost(client)
print(f"Cost: {cost_with_payment} Hbar")
print(f"Expected: {custom_payment} Hbar")
# Execute the query (should work with custom payment)
print("\nExecuting query with custom payment...")
result = query_with_payment.execute(client)
print(f"Query executed successfully!")
print(f" Account balance (only hbars): {result.hbars}")
def demonstrate_payment_required_queries(client, token_id):
"""
Demonstrate cost calculation for queries that require payment.
TokenInfoQuery is an example of a query that requires payment.
For such queries:
- get_cost() asks the network for the actual cost when no payment is set
- get_cost() returns the set payment amount when payment is set
"""
print("\nQueries that DO require payment:\n")
# Case 1: No payment set - should ask network for cost
print("When no payment is set:")
query_no_payment = TokenInfoQuery().set_token_id(token_id)
print("Asking network for query cost...")
cost_from_network = query_no_payment.get_cost(client)
print(f"Cost: {cost_from_network} Hbar")
print("This is the actual cost calculated by the network")
# Execute the query (should work with network-determined cost)
print("\nExecuting query with network-determined cost...")
result = query_no_payment.execute(client)
print(f"Query executed successfully!")
print(f" Token info: {result}")
# Case 2: Payment set - should return the set payment amount
print("\nWhen custom payment is set:")
custom_payment = Hbar(2)
query_with_payment = (
TokenInfoQuery()
.set_token_id(token_id)
.set_query_payment(custom_payment)
)
cost_with_payment = query_with_payment.get_cost(client)
print(f"Cost: {cost_with_payment} Hbar")
print(f"Expected: {custom_payment} Hbar")
# Execute the query (should work with custom payment)
print("\nExecuting query with custom payment...")
result = query_with_payment.execute(client)
print(f"Query executed successfully!")
print(f" Token info: {result}")
# Case 3: Compare network cost vs custom payment
print("\nCost comparison:")
print(f"Network-determined cost: {cost_from_network} Hbar")
print(f"Custom payment: {custom_payment} Hbar")
def query_payment():
"""
Demonstrates the query payment by:
1. Setting up client with operator account
2. Creating a fungible token with the operator account as owner
3. Demonstrating queries that don't require payment (CryptoGetAccountBalanceQuery)
4. Demonstrating queries that do require payment (TokenInfoQuery)
5. Comparing network-determined cost vs custom payment amount
"""
client, operator_id, operator_key = setup_client()
token_id = create_fungible_token(client, operator_id, operator_key)
demonstrate_zero_cost_balance_query(client, operator_id)
demonstrate_payment_required_queries(client, token_id)
if __name__ == "__main__":
query_payment()