forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_create_transaction_pause_key.py
More file actions
266 lines (207 loc) · 7.83 KB
/
token_create_transaction_pause_key.py
File metadata and controls
266 lines (207 loc) · 7.83 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
This example demonstrates the pause key capabilities for token management using the Hiero Python SDK.
It shows:
1. Creating a token *without* a pause key
2. Attempting to pause it — expected failure
3. Creating a token *with* a pause key
4. Successfully pausing and unpausing the token
5. Demonstrating that transfers fail while the token is paused
Required environment variables:
- OPERATOR_ID
- OPERATOR_KEY
Usage:
uv run examples/token_create_transaction_pause_key.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
Network,
TokenCreateTransaction,
TokenPauseTransaction,
TokenUnpauseTransaction,
TokenUpdateTransaction,
TokenInfoQuery,
TransferTransaction,
AccountCreateTransaction,
Hbar,
)
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.tokens.token_type import TokenType
from hiero_sdk_python.tokens.supply_type import SupplyType
load_dotenv()
network_name = os.getenv("NETWORK", "testnet").lower()
# -------------------------------------------------------
# CLIENT SETUP
# -------------------------------------------------------
def setup_client():
"""Create client from environment variables"""
network = Network(network_name)
print(f"Connecting to Hedera {network_name} network...")
client = Client(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)
print(f"Client ready — Operator: {client.operator_account_id}\n")
return client, operator_id, operator_key
except Exception:
print("❌ ERROR: Invalid OPERATOR_ID or OPERATOR_KEY in .env")
sys.exit(1)
# -------------------------------------------------------
# TOKEN CREATION (NO PAUSE KEY)
# -------------------------------------------------------
def create_token_without_pause_key(client, operator_id, operator_key):
print("🔹 Creating token WITHOUT pause key...")
tx = (
TokenCreateTransaction()
.set_token_name("PauseKeyMissing")
.set_token_symbol("NOPAUSE")
.set_decimals(0)
.set_initial_supply(100)
.set_treasury_account_id(operator_id)
.set_token_type(TokenType.FUNGIBLE_COMMON)
.set_supply_type(SupplyType.INFINITE)
.freeze_with(client)
.sign(operator_key)
)
receipt = tx.execute(client)
if receipt.status != ResponseCode.SUCCESS:
print("❌ Token creation failed")
sys.exit(1)
token_id = receipt.token_id
print(f"✅ Token created WITHOUT pause key → {token_id}\n")
return token_id
def attempt_pause_should_fail(client, token_id, operator_key):
print("🔹 Attempting to pause token WITHOUT a pause key... (expected failure)")
tx = (
TokenPauseTransaction()
.set_token_id(token_id)
.freeze_with(client)
.sign(operator_key)
)
receipt = tx.execute(client)
if receipt.status == ResponseCode.TOKEN_HAS_NO_PAUSE_KEY:
print(
"✅ Expected failure: token cannot be paused because no pause key exists.\n"
)
else:
print(f"❌ Unexpected status: {ResponseCode(receipt.status).name}\n")
# -------------------------------------------------------
# TOKEN CREATION WITH PAUSE KEY
# -------------------------------------------------------
def create_token_with_pause_key(client, operator_id, operator_key, pause_key):
print("🔹 Creating token WITH pause key...")
tx = (
TokenCreateTransaction()
.set_token_name("PauseKeyDemo")
.set_token_symbol("PAUSE")
.set_decimals(0)
.set_initial_supply(100)
.set_treasury_account_id(operator_id)
.set_token_type(TokenType.FUNGIBLE_COMMON)
.set_supply_type(SupplyType.INFINITE)
.set_pause_key(pause_key) # NEW
.freeze_with(client)
)
tx.sign(operator_key)
tx.sign(pause_key)
receipt = tx.execute(client)
if receipt.status != ResponseCode.SUCCESS:
print("❌ Token creation failed")
sys.exit(1)
token_id = receipt.token_id
print(f"✅ Token created WITH pause key → {token_id}\n")
return token_id
# -------------------------------------------------------
# PAUSE / UNPAUSE DEMO
# -------------------------------------------------------
def pause_token(client, token_id, pause_key):
print("🔹 Pausing token...")
tx = (
TokenPauseTransaction()
.set_token_id(token_id)
.freeze_with(client)
.sign(pause_key)
)
receipt = tx.execute(client)
if receipt.status == ResponseCode.SUCCESS:
print("✅ Token paused successfully!\n")
else:
print(f"❌ Pause failed: {ResponseCode(receipt.status).name}")
def unpause_token(client, token_id, pause_key):
print("🔹 Unpausing token...")
tx = (
TokenUnpauseTransaction()
.set_token_id(token_id)
.freeze_with(client)
.sign(pause_key)
)
receipt = tx.execute(client)
if receipt.status == ResponseCode.SUCCESS:
print("✅ Token unpaused successfully!\n")
else:
print(f"❌ Unpause failed: {ResponseCode(receipt.status).name}")
# -------------------------------------------------------
# TRANSFERS WHILE PAUSED SHOULD FAIL
# -------------------------------------------------------
def create_temp_account(client, operator_key):
"""Creates a small account for transfer testing."""
new_key = PrivateKey.generate_ed25519()
pub_key = new_key.public_key()
print("🔹 Creating a temporary recipient account...")
tx = (
AccountCreateTransaction()
.set_key_without_alias(pub_key) # MUST use public key
.set_initial_balance(Hbar.from_tinybars(1000))
.freeze_with(client)
.sign(operator_key)
)
receipt = tx.execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"❌ Failed to create temp account: {ResponseCode(receipt.status).name}")
sys.exit(1)
account_id = receipt.account_id
print(f"✅ Temp account created: {account_id}\n")
return account_id, new_key
def test_transfer_while_paused(
client, operator_id, operator_key, recipient_id, token_id
):
print("🔹 Attempting transfer WHILE token is paused (expected failure)...")
tx = (
TransferTransaction()
.add_token_transfer(token_id, operator_id, -10)
.add_token_transfer(token_id, recipient_id, 10)
.freeze_with(client)
.sign(operator_key)
)
receipt = tx.execute(client)
if receipt.status == ResponseCode.TOKEN_IS_PAUSED:
print("✅ Transfer failed as expected: TOKEN_IS_PAUSED\n")
else:
print(f"⚠️ Unexpected status: {ResponseCode(receipt.status).name}\n")
# -------------------------------------------------------
# MAIN
# -------------------------------------------------------
def main():
client, operator_id, operator_key = setup_client()
print("\n==================== PART 1 — NO PAUSE KEY ====================\n")
token_no_pause = create_token_without_pause_key(client, operator_id, operator_key)
attempt_pause_should_fail(client, token_no_pause, operator_key)
print("\n==================== PART 2 — WITH PAUSE KEY ====================\n")
pause_key = PrivateKey.generate_ed25519()
token_with_pause = create_token_with_pause_key(
client, operator_id, operator_key, pause_key
)
pause_token(client, token_with_pause, pause_key)
recipient_id, _ = create_temp_account(client, operator_key)
test_transfer_while_paused(
client, operator_id, operator_key, recipient_id, token_with_pause
)
unpause_token(client, token_with_pause, pause_key)
print("\n🎉 Pause key demonstration completed!")
if __name__ == "__main__":
main()