forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_token.py
More file actions
149 lines (127 loc) · 4.5 KB
/
transfer_token.py
File metadata and controls
149 lines (127 loc) · 4.5 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
"""
uv run examples/transfer_token.py
python examples/transfer_token.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
Network,
TransferTransaction,
AccountCreateTransaction,
Hbar,
TokenCreateTransaction,
CryptoGetAccountBalanceQuery,
TokenAssociateTransaction
)
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_account(client, operator_key):
"""Create a new recipient account"""
print("\nSTEP 1: Creating a new recipient account...")
recipient_key = PrivateKey.generate()
try:
tx = (
AccountCreateTransaction()
.set_key(recipient_key.public_key())
.set_initial_balance(Hbar.from_tinybars(100_000_000))
)
receipt = tx.freeze_with(client).sign(operator_key).execute(client)
recipient_id = receipt.account_id
print(f"✅ Success! Created a new recipient account with ID: {recipient_id}")
return recipient_id, recipient_key
except Exception as e:
print(f"Error creating new account: {e}")
sys.exit(1)
def create_token(client, operator_id, operator_key):
print("\nSTEP 2: Creating a new token...")
try:
token_tx = (
TokenCreateTransaction()
.set_token_name("First Token")
.set_token_symbol("TKA")
.set_initial_supply(1)
.set_treasury_account_id(operator_id)
.freeze_with(client)
.sign(operator_key)
)
token_receipt = token_tx.execute(client)
token_id = token_receipt.token_id
print(f"✅ Success! Created a token with Token ID: {token_id}")
return token_id
except Exception as e:
print(f"❌ Error creating token: {e}")
sys.exit(1)
def associate_token(client, recipient_id, recipient_key, token_id):
print("\nSTEP 3: Associating Token...")
try:
association_tx = (
TokenAssociateTransaction(account_id=recipient_id, token_ids=[token_id])
.freeze_with(client)
.sign(recipient_key)
)
association_tx.execute(client)
print("✅ Success! Token association complete.")
except Exception as e:
print(f"❌ Error associating token: {e}")
sys.exit(1)
def transfer_tokens():
"""
A full example to create a new recipent account, a fungible token, and
transfer the token to that account
"""
# Config Client
client, operator_id, operator_key = setup_client()
# Create a new recipient account.
recipient_id, recipient_key = create_account(client, operator_key)
# Create new tokens.
token_id = create_token(client, operator_id, operator_key)
# Associate Token
associate_token(client, recipient_id, recipient_key, token_id)
# Transfer Token
print("\nSTEP 4: Transfering Token...")
try:
# Check balance before transfer
balance_before = (
CryptoGetAccountBalanceQuery(account_id=recipient_id)
.execute(client)
.token_balances
)
print("Token balance before token transfer:")
print(f"{token_id}: {balance_before.get(token_id)}")
transfer_tx = (
TransferTransaction()
.add_token_transfer(token_id, operator_id, -1)
.add_token_transfer(token_id, recipient_id, 1)
.freeze_with(client)
.sign(operator_key)
)
transfer_tx.execute(client)
print("\n✅ Success! Token transfer complete.\n")
# Check balance after transfer
balance_after = (
CryptoGetAccountBalanceQuery(account_id=recipient_id)
.execute(client)
.token_balances
)
print("Token balance after token transfer:")
print(f"{token_id}: {balance_after.get(token_id)}")
except Exception as e:
print(f"❌ Error transferring token: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
transfer_tokens()