Skip to content

Commit 0903abe

Browse files
feat(examples): add pause key example for token create (#833)
Signed-off-by: undefinedIsMyLife <shinetina169@gmail.com>
1 parent 9be928d commit 0903abe

File tree

2 files changed

+263
-2
lines changed

2 files changed

+263
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Added
10-
-
11-
10+
- Add examples/tokens/token_create_transaction_pause_key.py example demonstrating token pause/unpause behavior and pause key usage (#833)
1211
### Changed
1312
-
1413

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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+
11+
Required environment variables:
12+
- OPERATOR_ID
13+
- OPERATOR_KEY
14+
15+
Usage:
16+
uv run examples/token_create_transaction_pause_key.py
17+
"""
18+
19+
import os
20+
import sys
21+
from dotenv import load_dotenv
22+
23+
from hiero_sdk_python import (
24+
Client,
25+
AccountId,
26+
PrivateKey,
27+
Network,
28+
TokenCreateTransaction,
29+
TokenPauseTransaction,
30+
TokenUnpauseTransaction,
31+
TokenUpdateTransaction,
32+
TokenInfoQuery,
33+
TransferTransaction,
34+
AccountCreateTransaction,
35+
Hbar,
36+
)
37+
38+
from hiero_sdk_python.response_code import ResponseCode
39+
from hiero_sdk_python.tokens.token_type import TokenType
40+
from hiero_sdk_python.tokens.supply_type import SupplyType
41+
42+
load_dotenv()
43+
network_name = os.getenv("NETWORK", "testnet").lower()
44+
45+
46+
# -------------------------------------------------------
47+
# CLIENT SETUP
48+
# -------------------------------------------------------
49+
def setup_client():
50+
"""Create client from environment variables"""
51+
network = Network(network_name)
52+
print(f"Connecting to Hedera {network_name} network...")
53+
client = Client(network)
54+
55+
try:
56+
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
57+
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
58+
client.set_operator(operator_id, operator_key)
59+
print(f"Client ready — Operator: {client.operator_account_id}\n")
60+
return client, operator_id, operator_key
61+
62+
except Exception:
63+
print("❌ ERROR: Invalid OPERATOR_ID or OPERATOR_KEY in .env")
64+
sys.exit(1)
65+
66+
67+
# -------------------------------------------------------
68+
# TOKEN CREATION (NO PAUSE KEY)
69+
# -------------------------------------------------------
70+
def create_token_without_pause_key(client, operator_id, operator_key):
71+
print("🔹 Creating token WITHOUT pause key...")
72+
73+
tx = (
74+
TokenCreateTransaction()
75+
.set_token_name("PauseKeyMissing")
76+
.set_token_symbol("NOPAUSE")
77+
.set_decimals(0)
78+
.set_initial_supply(100)
79+
.set_treasury_account_id(operator_id)
80+
.set_token_type(TokenType.FUNGIBLE_COMMON)
81+
.set_supply_type(SupplyType.INFINITE)
82+
.freeze_with(client)
83+
.sign(operator_key)
84+
)
85+
86+
receipt = tx.execute(client)
87+
if receipt.status != ResponseCode.SUCCESS:
88+
print("❌ Token creation failed")
89+
sys.exit(1)
90+
91+
token_id = receipt.token_id
92+
print(f"✅ Token created WITHOUT pause key → {token_id}\n")
93+
return token_id
94+
95+
96+
def attempt_pause_should_fail(client, token_id, operator_key):
97+
print("🔹 Attempting to pause token WITHOUT a pause key... (expected failure)")
98+
99+
tx = (
100+
TokenPauseTransaction()
101+
.set_token_id(token_id)
102+
.freeze_with(client)
103+
.sign(operator_key)
104+
)
105+
106+
receipt = tx.execute(client)
107+
108+
if receipt.status == ResponseCode.TOKEN_HAS_NO_PAUSE_KEY:
109+
print("✅ Expected failure: token cannot be paused because no pause key exists.\n")
110+
else:
111+
print(f"❌ Unexpected status: {ResponseCode(receipt.status).name}\n")
112+
113+
114+
# -------------------------------------------------------
115+
# TOKEN CREATION WITH PAUSE KEY
116+
# -------------------------------------------------------
117+
def create_token_with_pause_key(client, operator_id, operator_key, pause_key):
118+
print("🔹 Creating token WITH pause key...")
119+
120+
tx = (
121+
TokenCreateTransaction()
122+
.set_token_name("PauseKeyDemo")
123+
.set_token_symbol("PAUSE")
124+
.set_decimals(0)
125+
.set_initial_supply(100)
126+
.set_treasury_account_id(operator_id)
127+
.set_token_type(TokenType.FUNGIBLE_COMMON)
128+
.set_supply_type(SupplyType.INFINITE)
129+
.set_pause_key(pause_key) # NEW
130+
.freeze_with(client)
131+
)
132+
133+
tx.sign(operator_key)
134+
tx.sign(pause_key)
135+
136+
receipt = tx.execute(client)
137+
if receipt.status != ResponseCode.SUCCESS:
138+
print("❌ Token creation failed")
139+
sys.exit(1)
140+
141+
token_id = receipt.token_id
142+
print(f"✅ Token created WITH pause key → {token_id}\n")
143+
return token_id
144+
145+
146+
# -------------------------------------------------------
147+
# PAUSE / UNPAUSE DEMO
148+
# -------------------------------------------------------
149+
def pause_token(client, token_id, pause_key):
150+
print("🔹 Pausing token...")
151+
152+
tx = (
153+
TokenPauseTransaction()
154+
.set_token_id(token_id)
155+
.freeze_with(client)
156+
.sign(pause_key)
157+
)
158+
159+
receipt = tx.execute(client)
160+
if receipt.status == ResponseCode.SUCCESS:
161+
print("✅ Token paused successfully!\n")
162+
else:
163+
print(f"❌ Pause failed: {ResponseCode(receipt.status).name}")
164+
165+
166+
def unpause_token(client, token_id, pause_key):
167+
print("🔹 Unpausing token...")
168+
169+
tx = (
170+
TokenUnpauseTransaction()
171+
.set_token_id(token_id)
172+
.freeze_with(client)
173+
.sign(pause_key)
174+
)
175+
176+
receipt = tx.execute(client)
177+
if receipt.status == ResponseCode.SUCCESS:
178+
print("✅ Token unpaused successfully!\n")
179+
else:
180+
print(f"❌ Unpause failed: {ResponseCode(receipt.status).name}")
181+
182+
183+
# -------------------------------------------------------
184+
# TRANSFERS WHILE PAUSED SHOULD FAIL
185+
# -------------------------------------------------------
186+
def create_temp_account(client, operator_key):
187+
"""Creates a small account for transfer testing."""
188+
new_key = PrivateKey.generate_ed25519()
189+
pub_key = new_key.public_key()
190+
191+
print("🔹 Creating a temporary recipient account...")
192+
193+
tx = (
194+
AccountCreateTransaction()
195+
.set_key(pub_key) # MUST use public key
196+
.set_initial_balance(Hbar.from_tinybars(1000))
197+
.freeze_with(client)
198+
.sign(operator_key)
199+
)
200+
201+
receipt = tx.execute(client)
202+
203+
if receipt.status != ResponseCode.SUCCESS:
204+
print(f"❌ Failed to create temp account: {ResponseCode(receipt.status).name}")
205+
sys.exit(1)
206+
207+
account_id = receipt.account_id
208+
print(f"✅ Temp account created: {account_id}\n")
209+
210+
return account_id, new_key
211+
212+
213+
214+
def test_transfer_while_paused(client, operator_id, operator_key, recipient_id, token_id):
215+
print("🔹 Attempting transfer WHILE token is paused (expected failure)...")
216+
217+
tx = (
218+
TransferTransaction()
219+
.add_token_transfer(token_id, operator_id, -10)
220+
.add_token_transfer(token_id, recipient_id, 10)
221+
.freeze_with(client)
222+
.sign(operator_key)
223+
)
224+
225+
receipt = tx.execute(client)
226+
227+
if receipt.status == ResponseCode.TOKEN_IS_PAUSED:
228+
print("✅ Transfer failed as expected: TOKEN_IS_PAUSED\n")
229+
else:
230+
print(f"⚠️ Unexpected status: {ResponseCode(receipt.status).name}\n")
231+
232+
# -------------------------------------------------------
233+
# MAIN
234+
# -------------------------------------------------------
235+
def main():
236+
client, operator_id, operator_key = setup_client()
237+
238+
print("\n==================== PART 1 — NO PAUSE KEY ====================\n")
239+
token_no_pause = create_token_without_pause_key(client, operator_id, operator_key)
240+
attempt_pause_should_fail(client, token_no_pause, operator_key)
241+
242+
print("\n==================== PART 2 — WITH PAUSE KEY ====================\n")
243+
pause_key = PrivateKey.generate_ed25519()
244+
245+
token_with_pause = create_token_with_pause_key(
246+
client, operator_id, operator_key, pause_key
247+
)
248+
249+
pause_token(client, token_with_pause, pause_key)
250+
251+
recipient_id, _ = create_temp_account(client, operator_key)
252+
test_transfer_while_paused(
253+
client, operator_id, operator_key, recipient_id, token_with_pause
254+
)
255+
256+
unpause_token(client, token_with_pause, pause_key)
257+
258+
print("\n🎉 Pause key demonstration completed!")
259+
260+
261+
if __name__ == "__main__":
262+
main()

0 commit comments

Comments
 (0)