1- # uv run examples/tokens/token_freeze.py
2- # python examples/tokens/token_freeze.py
31"""
42Creates a freezeable token and demonstrates freezing and unfreezing
53the token for the operator (treasury) account.
4+
65uv run examples/tokens/token_freeze_transaction.py
76python examples/tokens/token_freeze_transaction.py
87"""
8+
99import os
1010import sys
1111
1919)
2020
2121
22-
2322def setup_client ():
23+ """Setup client from environment variables"""
2424 client = Client .from_env ()
25+ operator_id = client .operator_account_id
26+ operator_key = client .operator_private_key
27+
2528 print (f"Network: { client .network .network } " )
26- print (f"Client set up with operator id { client .operator_account_id } " )
27- return client
29+ print (f"Client set up with operator id { operator_id } " )
30+
31+ return client , operator_id , operator_key
32+
2833
2934def generate_freeze_key ():
3035 """Generate a Freeze Key"""
@@ -38,59 +43,61 @@ def create_freezeable_token(client, operator_id, operator_key):
3843 """Create a token with the freeze key"""
3944 freeze_key = generate_freeze_key ()
4045 print ("\n STEP 2: Creating a new freezeable token..." )
46+
4147 try :
4248 tx = (
4349 TokenCreateTransaction ()
4450 .set_token_name ("Freezeable Token" )
4551 .set_token_symbol ("FRZ" )
4652 .set_initial_supply (1000 )
4753 .set_treasury_account_id (operator_id )
48- .set_freeze_key (freeze_key ) # <-- THE FIX: Pass the private key directly
54+ .set_freeze_key (freeze_key )
4955 )
5056
51- # Freeze, sign with BOTH operator and the new freeze key, then execute
5257 receipt = (
5358 tx .freeze_with (client )
5459 .sign (operator_key )
55- .sign (freeze_key ) # The new freeze key must sign to give consent
60+ .sign (freeze_key )
5661 .execute (client )
5762 )
63+
5864 token_id = receipt .token_id
5965 print (f"✅ Success! Created token with ID: { token_id } " )
66+
6067 return freeze_key , token_id , client , operator_id , operator_key
61- except RuntimeError as e :
68+
69+ except Exception as e :
6270 print (f"❌ Error creating token: { e } " )
6371 sys .exit (1 )
6472
6573
6674def freeze_token (token_id , client , operator_id , freeze_key ):
67- """
68- Freeze the token for the operator account.
69- """
75+ """Freeze the token for the operator account"""
7076 print (f"\n STEP 3: Freezing token { token_id } for operator account { operator_id } ..." )
77+
7178 try :
7279 receipt = (
7380 TokenFreezeTransaction ()
7481 .set_token_id (token_id )
75- .set_account_id (operator_id ) # Target the operator account
82+ .set_account_id (operator_id )
7683 .freeze_with (client )
77- .sign (freeze_key ) # Must be signed by the freeze key
84+ .sign (freeze_key )
7885 .execute (client )
7986 )
87+
8088 print (
8189 f"✅ Success! Token freeze complete. Status: { ResponseCode (receipt .status ).name } "
8290 )
8391
84- except RuntimeError as e :
92+ except Exception as e :
8593 print (f"❌ Error freezing token: { e } " )
8694 sys .exit (1 )
8795
8896
8997def verify_freeze (token_id , client , operator_id , operator_key ):
90- """Attempt a token transfer to confirm the account
91- cannot perform the operation while frozen."""
98+ """Attempt a token transfer to confirm the account is frozen"""
9299 print ("\n Verifying freeze: Attempting token transfer..." )
93- # Try to transfer 1 token from operator to itself (should fail if frozen)
100+
94101 try :
95102 transfer_receipt = (
96103 TransferTransaction ()
@@ -100,9 +107,9 @@ def verify_freeze(token_id, client, operator_id, operator_key):
100107 .sign (operator_key )
101108 .execute (client )
102109 )
103- # Handle status code 165 (ACCOUNT_FROZEN_FOR_TOKEN) and print a clear message
104- status_code = transfer_receipt .status
105- status_name = ResponseCode ( status_code ). name
110+
111+ status_name = ResponseCode ( transfer_receipt .status ). name
112+
106113 if status_name in ["ACCOUNT_FROZEN_FOR_TOKEN" , "ACCOUNT_FROZEN" ]:
107114 print (
108115 f"✅ Verified: Transfer blocked as expected due to freeze. Status: { status_name } "
@@ -112,28 +119,28 @@ def verify_freeze(token_id, client, operator_id, operator_key):
112119 "❌ Error: Transfer succeeded, but should have failed because the account is frozen."
113120 )
114121 else :
115- print (f"❌ Unexpected: Transfer result. Status: { status_name } " )
116- except RuntimeError as e :
117- print (f"✅ Verified: Transfer failed as expected due to freeze. Error: { e } " )
122+ print (f"❌ Unexpected transfer result. Status: { status_name } " )
123+
124+ except Exception as e :
125+ print (f"❌ Error during transfer verification: { e } " )
126+ sys .exit (1 )
118127
119128
120129def main ():
121130 """
122131 1. Create a freezeable token with a freeze key.
123132 2. Freeze the token for the operator account using the freeze key.
124133 3. Attempt a token transfer to verify the freeze (should fail).
125- 4. Return token details for further operations."""
126-
127- client = setup_client ()
128- operator_id = client .operator_account_id
129- operator_key = client .operator_private_key
134+ """
135+ client , operator_id , operator_key = setup_client ()
130136
131137 freeze_key , token_id , client , operator_id , operator_key = create_freezeable_token (
132138 client , operator_id , operator_key
133139 )
140+
134141 freeze_token (token_id , client , operator_id , freeze_key )
135142 verify_freeze (token_id , client , operator_id , operator_key )
136143
137144
138145if __name__ == "__main__" :
139- main ()
146+ main ()
0 commit comments