11"""
22uv run examples/transfer_token.py
33python examples/transfer_token.py
4-
54"""
65import os
76import sys
@@ -34,7 +33,6 @@ def setup_client():
3433 operator_key = PrivateKey .from_string (os .getenv ('OPERATOR_KEY' ,'' ))
3534 client .set_operator (operator_id , operator_key )
3635 print (f"Client set up with operator id { client .operator_account_id } " )
37-
3836 return client , operator_id , operator_key
3937 except (TypeError , ValueError ):
4038 print ("❌ Error: Creating client, Please check your .env file" )
@@ -55,12 +53,13 @@ def create_account(client, operator_key):
5553 recipient_id = receipt .account_id
5654 print (f"✅ Success! Created a new recipient account with ID: { recipient_id } " )
5755 return recipient_id , recipient_key
58-
5956 except Exception as e :
6057 print (f"Error creating new account: { e } " )
6158 sys .exit (1 )
6259
60+
6361def create_token (client , operator_id , operator_key ):
62+ """Create a new token"""
6463 print ("\n STEP 2: Creating a new token..." )
6564 try :
6665 token_tx = (
@@ -81,7 +80,9 @@ def create_token(client, operator_id, operator_key):
8180 print (f"❌ Error creating token: { e } " )
8281 sys .exit (1 )
8382
83+
8484def associate_token (client , recipient_id , recipient_key , token_id ):
85+ """Associate token with the recipient"""
8586 print ("\n STEP 3: Associating Token..." )
8687 try :
8788 association_tx = (
@@ -96,57 +97,53 @@ def associate_token(client, recipient_id, recipient_key, token_id):
9697 print (f"❌ Error associating token: { e } " )
9798 sys .exit (1 )
9899
99- def transfer_tokens ():
100- """
101- A full example to create a new recipent account, a fungible token, and
102- transfer the token to that account
103- """
104- # Config Client
105- client , operator_id , operator_key = setup_client ()
106-
107- # Create a new recipient account.
108- recipient_id , recipient_key = create_account (client , operator_key )
109-
110- # Create new tokens.
111- token_id = create_token (client , operator_id , operator_key )
112-
113- # Associate Token
114- associate_token (client , recipient_id , recipient_key , token_id )
115100
116- # Transfer Token
117- print ("\n STEP 4: Transfering Token..." )
101+ def transfer_transaction (client , operator_id , operator_key , recipient_id , token_id ):
102+ """Perform the token transfer"""
103+ print ("\n STEP 4: Transferring Token..." )
118104 try :
119- # Check balance before transfer
105+ # Check balance before
120106 balance_before = (
121107 CryptoGetAccountBalanceQuery (account_id = recipient_id )
122108 .execute (client )
123109 .token_balances
124110 )
125- print ("Token balance before token transfer:" )
111+ print ("Token balance before transfer:" )
126112 print (f"{ token_id } : { balance_before .get (token_id )} " )
127113
128- transfer_tx = (
114+ tx = (
129115 TransferTransaction ()
130116 .add_token_transfer (token_id , operator_id , - 1 )
131117 .add_token_transfer (token_id , recipient_id , 1 )
132118 .freeze_with (client )
133119 .sign (operator_key )
134120 )
135- transfer_tx .execute (client )
136-
121+ tx .execute (client )
122+
137123 print ("\n ✅ Success! Token transfer complete.\n " )
138124
139- # Check balance after transfer
125+ # Check balance after
140126 balance_after = (
141127 CryptoGetAccountBalanceQuery (account_id = recipient_id )
142128 .execute (client )
143129 .token_balances
144130 )
145- print ("Token balance after token transfer:" )
131+ print ("Token balance after transfer:" )
146132 print (f"{ token_id } : { balance_after .get (token_id )} " )
133+
147134 except Exception as e :
148135 print (f"❌ Error transferring token: { str (e )} " )
149136 sys .exit (1 )
150137
138+
139+ def main ():
140+ """Main script runner"""
141+ client , operator_id , operator_key = setup_client ()
142+ recipient_id , recipient_key = create_account (client , operator_key )
143+ token_id = create_token (client , operator_id , operator_key )
144+ associate_token (client , recipient_id , recipient_key , token_id )
145+ transfer_transaction (client , operator_id , operator_key , recipient_id , token_id )
146+
147+
151148if __name__ == "__main__" :
152- transfer_tokens ()
149+ main ()
0 commit comments