forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
356 lines (292 loc) · 12.9 KB
/
test.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python.client.network import Network
from hiero_sdk_python.client.client import Client
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction
from hiero_sdk_python.crypto.private_key import PrivateKey
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction
from hiero_sdk_python.tokens.token_associate_transaction import TokenAssociateTransaction
from hiero_sdk_python.tokens.token_dissociate_transaction import TokenDissociateTransaction
from hiero_sdk_python.tokens.token_mint_transaction import TokenMintTransaction
from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction
from hiero_sdk_python.tokens.token_delete_transaction import TokenDeleteTransaction
from hiero_sdk_python.tokens.token_freeze_transaction import TokenFreezeTransaction
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.consensus.topic_create_transaction import TopicCreateTransaction
from hiero_sdk_python.consensus.topic_message_submit_transaction import TopicMessageSubmitTransaction
from hiero_sdk_python.consensus.topic_update_transaction import TopicUpdateTransaction
from hiero_sdk_python.consensus.topic_delete_transaction import TopicDeleteTransaction
from hiero_sdk_python.consensus.topic_id import TopicId
from hiero_sdk_python.query.topic_info_query import TopicInfoQuery
from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery
load_dotenv()
def load_operator_credentials():
"""Load operator credentials from environment variables."""
try:
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
except Exception as e:
print(f"Error parsing operator credentials: {e}")
sys.exit(1)
return operator_id, operator_key
def create_new_account(client, initial_balance=100000000):
new_account_private_key = PrivateKey.generate()
new_account_public_key = new_account_private_key.public_key()
transaction = AccountCreateTransaction(
key=new_account_public_key,
initial_balance=initial_balance,
memo="Recipient Account"
)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
try:
receipt = transaction.execute(client)
new_account_id = receipt.accountId
if new_account_id is not None:
print(f"Account creation successful. New Account ID: {new_account_id}")
print(f"New Account Private Key: {new_account_private_key.to_string()}")
print(f"New Account Public Key: {new_account_public_key.to_string()}")
else:
raise Exception("AccountID not found in receipt. Account may not have been created.")
except Exception as e:
print(f"Account creation failed: {str(e)}")
sys.exit(1)
return new_account_id, new_account_private_key
def query_balance(client, account_id):
balance = CryptoGetAccountBalanceQuery(account_id=account_id).execute(client)
print(f"Account {account_id} balance: {balance.hbars}")
return balance
def create_token(client, operator_id, admin_key, supply_key, freeze_key):
transaction = TokenCreateTransaction(
token_name="ExampleToken",
token_symbol="EXT",
decimals=2,
initial_supply=1000,
treasury_account_id=operator_id,
admin_key=admin_key,
supply_key=supply_key,
freeze_key=freeze_key
)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(admin_key)
try:
receipt = transaction.execute(client)
except Exception as e:
print(f"Token creation failed: {str(e)}")
sys.exit(1)
if not receipt.tokenId:
print("Token creation failed: Token ID not returned in receipt.")
sys.exit(1)
token_id = receipt.tokenId
print(f"Token creation successful. Token ID: {token_id}")
return token_id
def associate_token(client, recipient_id, recipient_private_key, token_ids):
transaction = TokenAssociateTransaction(
account_id=recipient_id,
token_ids=token_ids
)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(recipient_private_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token association failed with status: {status_message}")
print("Token association successful.")
except Exception as e:
print(f"Token association failed: {str(e)}")
sys.exit(1)
def dissociate_token(client, recipient_id, recipient_private_key, token_id):
"""Dissociate the specified token with the recipient account."""
transaction = TokenDissociateTransaction(
account_id = recipient_id,
token_ids = token_id)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(recipient_private_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token dissociation failed with status: {status_message}")
print("Token dissociation successful.")
except Exception as e:
print(f"Token dissociation failed: {str(e)}")
sys.exit(1)
def transfer_token(client, source_id, source_private_key, recipient_id, token_id):
"""Transfer the specified token to the recipient account."""
transaction = (
TransferTransaction()
.add_token_transfer(token_id, source_id, -1)
.add_token_transfer(token_id, recipient_id, 1)
.freeze_with(client)
)
transaction.sign(source_private_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token transfer failed with status: {status_message}")
print("Token transfer successful.")
except Exception as e:
print(f"Token transfer failed: {str(e)}")
sys.exit(1)
def delete_token(client, token_id, admin_key):
transaction = TokenDeleteTransaction(token_id=token_id)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(admin_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token deletion failed with status: {status_message}")
print("Token deletion successful.")
except Exception as e:
print(f"Token deletion failed: {str(e)}")
sys.exit(1)
def freeze_token(client, token_id, account_id, freeze_key):
transaction = TokenFreezeTransaction(token_id=token_id, account_id=account_id)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(freeze_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token freeze failed with status: {status_message}")
print("Token freeze successful.")
except Exception as e:
print(f"Token freeze failed: {str(e)}")
sys.exit(1)
def mint_fungible_token(client, token_id, supply_key, amount=2000):
transaction = TokenMintTransaction(token_id=token_id, amount=amount)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(supply_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token minting failed with status: {status_message}")
print("Token minting successful.")
except Exception as e:
print(f"Token minting failed: {str(e)}")
sys.exit(1)
def mint_nft_token(client, token_id, supply_key, metadata=[b"Token A"]):
transaction = TokenMintTransaction(token_id=token_id, metadata=metadata )
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
transaction.sign(supply_key)
try:
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Token minting failed with status: {status_message}")
print("Token minting successful.")
except Exception as e:
print(f"Token minting failed: {str(e)}")
sys.exit(1)
def create_topic(client):
key = client.operator_private_key
transaction = TopicCreateTransaction(
memo="Python SDK created topic",
admin_key=key.public_key()
)
transaction.freeze_with(client)
transaction.sign(key)
try:
receipt = transaction.execute(client)
except Exception as e:
print(f"Topic creation failed: {str(e)}")
sys.exit(1)
if not receipt.topicId:
print("Topic creation failed: Topic ID not returned in receipt.")
sys.exit(1)
topic_id = receipt.topicId
print(f"Topic creation successful. Topic ID: {topic_id}")
return topic_id
def submit_message(client, topic_id):
transaction = TopicMessageSubmitTransaction(
topic_id=topic_id,
message="Hello, Python SDK!"
)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
try:
receipt = transaction.execute(client)
except Exception as e:
print(f"Message submission failed: {str(e)}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Message submission failed with status: {status_message}")
print("Message submitted successfully.")
def update_topic(client, topic_id):
key = client.operator_private_key
transaction = TopicUpdateTransaction(
topic_id=topic_id,
memo="Python SDK updated topic"
)
transaction.freeze_with(client)
transaction.sign(key)
try:
receipt = transaction.execute(client)
except Exception as e:
print(f"Topic update failed: {str(e)}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Topic update failed with status: {status_message}")
print("Topic updated successfully.")
def delete_topic(client, topic_id):
transaction = TopicDeleteTransaction(topic_id=topic_id)
transaction.freeze_with(client)
transaction.sign(client.operator_private_key)
try:
receipt = transaction.execute(client)
except Exception as e:
print(f"Topic deletion failed: {str(e)}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
status_message = ResponseCode.get_name(receipt.status)
raise Exception(f"Topic deletion failed with status: {status_message}")
print("Topic deleted successfully.")
def query_topic_info(client, topic_id):
"""Optional method to show how to query topic info."""
try:
topic_info = TopicInfoQuery(topic_id=topic_id).execute(client)
print(f"Topic Info: {topic_info}")
except Exception as e:
print(f"Failed to retrieve topic info: {str(e)}")
def main():
operator_id, operator_key = load_operator_credentials()
admin_key = PrivateKey.generate()
supply_key = PrivateKey.generate()
freeze_key = PrivateKey.generate()
network_type = os.getenv('NETWORK')
network = Network(network=network_type)
client = Client(network)
client.set_operator(operator_id, operator_key)
recipient_id, recipient_private_key = create_new_account(client)
query_balance(client, recipient_id)
token_id_1 = create_token(client, operator_id, admin_key, supply_key, freeze_key)
token_id_2 = create_token(client, operator_id, admin_key, supply_key, freeze_key)
mint_fungible_token(client, token_id_1, supply_key)
mint_nft_token(client, token_id_1, supply_key)
associate_token(client, recipient_id, recipient_private_key, [token_id_1, token_id_2])
transfer_token(client, operator_id, operator_key, recipient_id, token_id_1)
freeze_token(client, token_id_1, recipient_id, freeze_key)
dissociate_token(client, recipient_id, recipient_private_key, [token_id_2])
delete_token(client, token_id_1, admin_key)
topic_id = create_topic(client)
submit_message(client, topic_id)
update_topic(client, topic_id)
query_topic_info(client, topic_id)
delete_topic(client, topic_id)
if __name__ == "__main__":
main()