|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from dotenv import load_dotenv |
| 4 | + |
| 5 | +from hiero_sdk_python import ( |
| 6 | + Client, |
| 7 | + AccountId, |
| 8 | + PrivateKey, |
| 9 | + Network |
| 10 | +) |
| 11 | +from hiero_sdk_python.response_code import ResponseCode |
| 12 | +from hiero_sdk_python.tokens.supply_type import SupplyType |
| 13 | +from hiero_sdk_python.tokens.token_type import TokenType |
| 14 | +from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction |
| 15 | +from hiero_sdk_python.tokens.token_pause_transaction import TokenPauseTransaction |
| 16 | +from hiero_sdk_python.tokens.token_delete_transaction import TokenDeleteTransaction |
| 17 | +from hiero_sdk_python.query.token_info_query import TokenInfoQuery |
| 18 | + |
| 19 | +load_dotenv() |
| 20 | + |
| 21 | +def setup_client(): |
| 22 | + """Initialize and set up the client with operator account""" |
| 23 | + # Initialize network and client |
| 24 | + network = Network(network='testnet') |
| 25 | + client = Client(network) |
| 26 | + |
| 27 | + # Set up operator account |
| 28 | + operator_id = AccountId.from_string(os.getenv('OPERATOR_ID')) |
| 29 | + operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY')) |
| 30 | + client.set_operator(operator_id, operator_key) |
| 31 | + |
| 32 | + return client, operator_id, operator_key |
| 33 | + |
| 34 | +def assert_success(receipt, action: str): |
| 35 | + """ |
| 36 | + Verify that a transaction or query succeeded, else raise. |
| 37 | +
|
| 38 | + Args: |
| 39 | + receipt: The receipt object returned by `execute(client)` on a Transaction or Query. |
| 40 | + action (str): A short description of the attempted operation (e.g. "Token creation"). |
| 41 | +
|
| 42 | + Raises: |
| 43 | + RuntimeError: If the receipt’s status is not `ResponseCode.SUCCESS`, with a message |
| 44 | + indicating which action failed and the status name. |
| 45 | + """ |
| 46 | + if receipt.status != ResponseCode.SUCCESS: |
| 47 | + name = ResponseCode.get_name(receipt.status) |
| 48 | + raise RuntimeError(f"{action!r} failed with status {name}") |
| 49 | + |
| 50 | +def create_token(client, operator_id, admin_key, pause_key): |
| 51 | + """Create a fungible token""" |
| 52 | + # Create fungible token |
| 53 | + create_token_transaction = ( |
| 54 | + TokenCreateTransaction() |
| 55 | + .set_token_name("Token123") |
| 56 | + .set_token_symbol("T123") |
| 57 | + .set_decimals(2) |
| 58 | + .set_initial_supply(10) |
| 59 | + .set_treasury_account_id(operator_id) |
| 60 | + .set_token_type(TokenType.FUNGIBLE_COMMON) |
| 61 | + .set_supply_type(SupplyType.FINITE) |
| 62 | + .set_max_supply(100) |
| 63 | + .set_admin_key(admin_key) # Required for token delete |
| 64 | + .set_pause_key(pause_key) # Required for pausing tokens |
| 65 | + .freeze_with(client) |
| 66 | + ) |
| 67 | + |
| 68 | + receipt = create_token_transaction.execute(client) |
| 69 | + assert_success(receipt, "Token creation") |
| 70 | + |
| 71 | + # Get token ID from receipt |
| 72 | + token_id = receipt.tokenId |
| 73 | + print(f"Token created with ID: {token_id}") |
| 74 | + |
| 75 | + return token_id |
| 76 | + |
| 77 | +def pause_token(client, token_id, pause_key): |
| 78 | + """Pause token""" |
| 79 | + # Note: This requires the pause key that was specified during token creation |
| 80 | + pause_transaction = ( |
| 81 | + TokenPauseTransaction() |
| 82 | + .set_token_id(token_id) |
| 83 | + .freeze_with(client) |
| 84 | + .sign(pause_key) |
| 85 | + ) |
| 86 | + |
| 87 | + receipt = pause_transaction.execute(client) |
| 88 | + assert_success(receipt, "Token pause") |
| 89 | + |
| 90 | + print(f"Successfully paused token {token_id}") |
| 91 | + |
| 92 | +def check_pause_status(client, token_id): |
| 93 | + """ |
| 94 | + Query and print the current paused/unpaused status of a token. |
| 95 | + """ |
| 96 | + info = TokenInfoQuery().set_token_id(token_id).execute(client) |
| 97 | + print(f"Token status is now: {info.pause_status.name}") |
| 98 | + |
| 99 | +def delete_token(client, token_id, admin_key): |
| 100 | + """Delete token""" |
| 101 | + # Note: This requires the admin key that was specified during token creation |
| 102 | + delete_transaction = ( |
| 103 | + TokenDeleteTransaction() |
| 104 | + .set_token_id(token_id) |
| 105 | + .freeze_with(client) |
| 106 | + .sign(admin_key) |
| 107 | + ) |
| 108 | + |
| 109 | + receipt = delete_transaction.execute(client) |
| 110 | + assert_success(receipt, "Token delete") |
| 111 | + |
| 112 | + print(f"Successfully deleted token {token_id}") |
| 113 | + |
| 114 | +def token_pause(): |
| 115 | + """ |
| 116 | + Demonstrates the token pause functionality by: |
| 117 | + 1. Creating a fungible token with pause and delete capability |
| 118 | + 2. Pausing the token |
| 119 | + 3. Verifying pause status |
| 120 | + 4. Attempting (and failing) to delete the paused token because it is paused |
| 121 | + """ |
| 122 | + client, operator_id, operator_key = setup_client() |
| 123 | + |
| 124 | + pause_key = operator_key # for token pause |
| 125 | + admin_key = operator_key # for token delete |
| 126 | + |
| 127 | + # Create token with required keys for pause and delete. |
| 128 | + token_id = create_token(client, operator_id, admin_key, pause_key) |
| 129 | + |
| 130 | + # Pause token using pause key – should succeed |
| 131 | + pause_token(client, token_id, pause_key) |
| 132 | + |
| 133 | + # Verify it is paused |
| 134 | + check_pause_status(client, token_id) |
| 135 | + |
| 136 | + # Try deleting token with admin key – should fail with TOKEN_IS_PAUSED |
| 137 | + try: |
| 138 | + delete_token(client, token_id, admin_key) |
| 139 | + print("❌ Whoops, delete succeeded—but it should have failed on a paused token!") |
| 140 | + except RuntimeError as e: |
| 141 | + print(f"✅ Unable to delete token as expected as it is paused: {e}") |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + token_pause() |
0 commit comments