Skip to content

Commit e269f57

Browse files
feat(examples): add pause key example for token create
Signed-off-by: undefinedIsMyLife <shinetina169@gmail.com>
1 parent 46931e8 commit e269f57

File tree

1 file changed

+319
-0
lines changed

1 file changed

+319
-0
lines changed
Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
"""
2+
This example demonstrates the pause key capabilities for token management using the Hiero Python SDK.
3+
4+
It shows:
5+
1. Creating a token *without* a pause key
6+
2. Attempting to pause it — expected failure
7+
3. Creating a token *with* a pause key
8+
4. Successfully pausing and unpausing the token
9+
5. Demonstrating that transfers fail while the token is paused
10+
6. (Bonus) Removing the pause key while the token is paused → permanently paused
11+
12+
Required environment variables:
13+
- OPERATOR_ID
14+
- OPERATOR_KEY
15+
16+
Usage:
17+
uv run examples/token_create_transaction_pause_key.py
18+
"""
19+
20+
import os
21+
import sys
22+
from dotenv import load_dotenv
23+
24+
from hiero_sdk_python import (
25+
Client,
26+
AccountId,
27+
PrivateKey,
28+
Network,
29+
TokenCreateTransaction,
30+
TokenPauseTransaction,
31+
TokenUnpauseTransaction,
32+
TokenUpdateTransaction,
33+
TokenInfoQuery,
34+
TransferTransaction,
35+
AccountCreateTransaction,
36+
Hbar,
37+
)
38+
39+
from hiero_sdk_python.response_code import ResponseCode
40+
from hiero_sdk_python.tokens.token_type import TokenType
41+
from hiero_sdk_python.tokens.supply_type import SupplyType
42+
43+
load_dotenv()
44+
network_name = os.getenv("NETWORK", "testnet").lower()
45+
46+
47+
# -------------------------------------------------------
48+
# CLIENT SETUP
49+
# -------------------------------------------------------
50+
def setup_client():
51+
"""Create client from environment variables"""
52+
network = Network(network_name)
53+
print(f"Connecting to Hedera {network_name} network...")
54+
client = Client(network)
55+
56+
try:
57+
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
58+
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
59+
client.set_operator(operator_id, operator_key)
60+
print(f"Client ready — Operator: {client.operator_account_id}\n")
61+
return client, operator_id, operator_key
62+
63+
except Exception:
64+
print("❌ ERROR: Invalid OPERATOR_ID or OPERATOR_KEY in .env")
65+
sys.exit(1)
66+
67+
68+
# -------------------------------------------------------
69+
# TOKEN CREATION (NO PAUSE KEY)
70+
# -------------------------------------------------------
71+
def create_token_without_pause_key(client, operator_id, operator_key):
72+
print("🔹 Creating token WITHOUT pause key...")
73+
74+
tx = (
75+
TokenCreateTransaction()
76+
.set_token_name("PauseKeyMissing")
77+
.set_token_symbol("NOPAUSE")
78+
.set_decimals(0)
79+
.set_initial_supply(100)
80+
.set_treasury_account_id(operator_id)
81+
.set_token_type(TokenType.FUNGIBLE_COMMON)
82+
.set_supply_type(SupplyType.INFINITE)
83+
.freeze_with(client)
84+
.sign(operator_key)
85+
)
86+
87+
receipt = tx.execute(client)
88+
if receipt.status != ResponseCode.SUCCESS:
89+
print("❌ Token creation failed")
90+
sys.exit(1)
91+
92+
token_id = receipt.token_id
93+
print(f"✅ Token created WITHOUT pause key → {token_id}\n")
94+
return token_id
95+
96+
97+
def attempt_pause_should_fail(client, token_id, operator_key):
98+
print("🔹 Attempting to pause token WITHOUT a pause key... (expected failure)")
99+
try:
100+
tx = (
101+
TokenPauseTransaction()
102+
.set_token_id(token_id)
103+
.freeze_with(client)
104+
.sign(operator_key)
105+
)
106+
receipt = tx.execute(client)
107+
108+
print("⚠️ Unexpected success! Pause should NOT be possible.")
109+
print(f"Status: {ResponseCode(receipt.status).name}\n")
110+
111+
except Exception as e:
112+
print(f"✅ Expected failure occurred: {e}\n")
113+
114+
115+
# -------------------------------------------------------
116+
# TOKEN CREATION WITH PAUSE KEY
117+
# -------------------------------------------------------
118+
def create_token_with_pause_key(client, operator_id, operator_key, pause_key):
119+
print("🔹 Creating token WITH pause key...")
120+
121+
tx = (
122+
TokenCreateTransaction()
123+
.set_token_name("PauseKeyDemo")
124+
.set_token_symbol("PAUSE")
125+
.set_decimals(0)
126+
.set_initial_supply(100)
127+
.set_treasury_account_id(operator_id)
128+
.set_token_type(TokenType.FUNGIBLE_COMMON)
129+
.set_supply_type(SupplyType.INFINITE)
130+
.set_pause_key(pause_key) # NEW
131+
.freeze_with(client)
132+
)
133+
134+
tx.sign(operator_key)
135+
tx.sign(pause_key)
136+
137+
receipt = tx.execute(client)
138+
if receipt.status != ResponseCode.SUCCESS:
139+
print("❌ Token creation failed")
140+
sys.exit(1)
141+
142+
token_id = receipt.token_id
143+
print(f"✅ Token created WITH pause key → {token_id}\n")
144+
return token_id
145+
146+
147+
# -------------------------------------------------------
148+
# PAUSE / UNPAUSE DEMO
149+
# -------------------------------------------------------
150+
def pause_token(client, token_id, pause_key):
151+
print("🔹 Pausing token...")
152+
153+
tx = (
154+
TokenPauseTransaction()
155+
.set_token_id(token_id)
156+
.freeze_with(client)
157+
.sign(pause_key)
158+
)
159+
160+
receipt = tx.execute(client)
161+
if receipt.status == ResponseCode.SUCCESS:
162+
print("✅ Token paused successfully!\n")
163+
else:
164+
print(f"❌ Pause failed: {ResponseCode(receipt.status).name}")
165+
166+
167+
def unpause_token(client, token_id, pause_key):
168+
print("🔹 Unpausing token...")
169+
170+
tx = (
171+
TokenUnpauseTransaction()
172+
.set_token_id(token_id)
173+
.freeze_with(client)
174+
.sign(pause_key)
175+
)
176+
177+
receipt = tx.execute(client)
178+
if receipt.status == ResponseCode.SUCCESS:
179+
print("✅ Token unpaused successfully!\n")
180+
else:
181+
print(f"❌ Unpause failed: {ResponseCode(receipt.status).name}")
182+
183+
184+
# -------------------------------------------------------
185+
# TRANSFERS WHILE PAUSED SHOULD FAIL
186+
# -------------------------------------------------------
187+
def create_temp_account(client, operator_key):
188+
"""Creates a small account for transfer testing."""
189+
new_key = PrivateKey.generate_ed25519()
190+
pub_key = new_key.public_key()
191+
192+
print("🔹 Creating a temporary recipient account...")
193+
194+
tx = (
195+
AccountCreateTransaction()
196+
.set_key(pub_key) # MUST use public key
197+
.set_initial_balance(Hbar.from_tinybars(1000))
198+
.freeze_with(client)
199+
.sign(operator_key)
200+
)
201+
202+
receipt = tx.execute(client)
203+
account_id = receipt.account_id
204+
print(f"✅ Temp account created: {account_id}\n")
205+
return account_id, new_key
206+
207+
208+
209+
def test_transfer_while_paused(client, operator_id, operator_key, recipient_id, token_id):
210+
print("🔹 Attempting transfer WHILE token is paused (expected failure)...")
211+
212+
tx = (
213+
TransferTransaction()
214+
.add_token_transfer(token_id, operator_id, -10)
215+
.add_token_transfer(token_id, recipient_id, 10)
216+
.freeze_with(client)
217+
.sign(operator_key)
218+
)
219+
220+
receipt = tx.execute(client)
221+
222+
if receipt.status == ResponseCode.TOKEN_IS_PAUSED:
223+
print("✅ Transfer failed as expected: TOKEN_IS_PAUSED\n")
224+
else:
225+
print(f"⚠️ Unexpected status: {ResponseCode(receipt.status).name}\n")
226+
227+
228+
229+
# -------------------------------------------------------
230+
# BONUS: REMOVE PAUSE KEY WHILE PAUSED → PERMANENT
231+
# -------------------------------------------------------
232+
def remove_pause_key(client, token_id, pause_key):
233+
print("🔹 Removing pause key while token is UNPAUSED...")
234+
235+
tx = (
236+
TokenUpdateTransaction()
237+
.set_token_id(token_id)
238+
.set_pause_key(None)
239+
.freeze_with(client)
240+
.sign(pause_key)
241+
)
242+
243+
receipt = tx.execute(client)
244+
245+
if receipt.status == ResponseCode.SUCCESS:
246+
print("✅ Pause key removed successfully.\n")
247+
return True
248+
else:
249+
print(f"❌ Failed to remove pause key: {ResponseCode(receipt.status).name}\n")
250+
return False
251+
252+
253+
def attempt_unpause_after_removal_should_fail(client, token_id):
254+
print("🔹 Attempting to unpause a token with NO pause key (should fail)...")
255+
256+
try:
257+
tx = (
258+
TokenUnpauseTransaction()
259+
.set_token_id(token_id)
260+
.freeze_with(client)
261+
)
262+
263+
# Do NOT sign — because pause key no longer exists
264+
receipt = tx.execute(client)
265+
266+
print(f"⚠️ Unexpected status: {ResponseCode(receipt.status).name}\n")
267+
268+
except Exception as e:
269+
print(f"✅ Permanent unpause failure occurred as expected: {e}\n")
270+
271+
272+
273+
# -------------------------------------------------------
274+
# MAIN
275+
# -------------------------------------------------------
276+
def main():
277+
client, operator_id, operator_key = setup_client()
278+
279+
print("\n==================== PART 1 — NO PAUSE KEY ====================\n")
280+
token_no_pause = create_token_without_pause_key(client, operator_id, operator_key)
281+
attempt_pause_should_fail(client, token_no_pause, operator_key)
282+
283+
print("\n==================== PART 2 — WITH PAUSE KEY ====================\n")
284+
pause_key = PrivateKey.generate_ed25519()
285+
286+
token_with_pause = create_token_with_pause_key(
287+
client, operator_id, operator_key, pause_key
288+
)
289+
290+
pause_token(client, token_with_pause, pause_key)
291+
292+
recipient_id, _ = create_temp_account(client, operator_key)
293+
test_transfer_while_paused(
294+
client, operator_id, operator_key, recipient_id, token_with_pause
295+
)
296+
297+
unpause_token(client, token_with_pause, pause_key)
298+
299+
print("\n==================== BONUS — PERMANENT PAUSE ====================\n")
300+
# 1. Pause
301+
pause_token(client, token_with_pause, pause_key)
302+
303+
# 2. Unpause (required to modify keys)
304+
unpause_token(client, token_with_pause, pause_key)
305+
306+
# 3. Remove pause key
307+
if remove_pause_key(client, token_with_pause, pause_key):
308+
309+
# 4. Pause again
310+
pause_token(client, token_with_pause, pause_key)
311+
312+
# 5. Unpause should now fail permanently
313+
attempt_unpause_after_removal_should_fail(client, token_with_pause)
314+
315+
print("\n🎉 Pause key demonstration completed!")
316+
317+
318+
if __name__ == "__main__":
319+
main()

0 commit comments

Comments
 (0)