diff --git a/CHANGELOG.md b/CHANGELOG.md index e1e56852c..dadd432ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,8 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1. - Rename test files across the repository to ensure they consistently end with _test.py (#1055) - Cleaned up `token_airdrop_claim_signature_required` example for pylint compliance (no functional changes). (#1080) - Rename the file 'test_token_fee_schedule_update_transaction_e2e.py' to make it ends with _test.py as all other test files.(#1117) +- Format token examples with Black for consistent code style and improved readability (#1119) +- Transformed `examples/tokens/custom_fee_fixed.py` to be an end-to-end example, that interacts with the Hedera network, rather than a static object demo. - Format token examples with Black for consistent code style and improved readability (#1119) - Replaced `ResponseCode.get_name(receipt.status)` with the `ResponseCode(receipt.status).name` across examples and integration tests for consistency. (#1136) diff --git a/examples/tokens/custom_fee_fixed.py b/examples/tokens/custom_fee_fixed.py index 0576c6d1b..a4a33a2e4 100644 --- a/examples/tokens/custom_fee_fixed.py +++ b/examples/tokens/custom_fee_fixed.py @@ -4,26 +4,119 @@ python examples/tokens/custom_fixed_fee.py """ -from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee +import os +import sys +from dotenv import load_dotenv + +from hiero_sdk_python.client.network import Network +from hiero_sdk_python.crypto.private_key import PrivateKey +from hiero_sdk_python.client.client import Client +from hiero_sdk_python.query.token_info_query import TokenInfoQuery from hiero_sdk_python.account.account_id import AccountId -from hiero_sdk_python.tokens.token_id import TokenId +from hiero_sdk_python.response_code import ResponseCode +from hiero_sdk_python.tokens.supply_type import SupplyType +from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee +from hiero_sdk_python.hbar import Hbar +from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction +from hiero_sdk_python.tokens.token_type import TokenType + + +load_dotenv() +network_name = os.getenv("NETWORK", "testnet").lower() + +def setup_client(): + """Initialize and set up the client with operator account""" + # Initialize network and client + network = Network(network_name) + print(f"Connecting to Hedera {network_name} network!") + client = Client(network) + + # This disables the SSL error in the local development environment (Keep commented for production) # + + # client.set_transport_security(False) + # client.set_verify_certificates(False) + + try: + operator_id_str = os.getenv('OPERATOR_ID') + operator_key_str = os.getenv('OPERATOR_KEY') + + if not operator_id_str or not operator_key_str: + raise ValueError("Environment variables OPERATOR_ID or OPERATOR_KEY are missing.") + + operator_id = AccountId.from_string(operator_id_str) + operator_key = PrivateKey.from_string(operator_key_str) + + client.set_operator(operator_id, operator_key) + print(f"Client set up with operator id {client.operator_account_id}") + return client, operator_id, operator_key + + except (TypeError, ValueError) as e: + print(f"Error: {e}") + print("Please check OPERATOR_ID and OPERATOR_KEY in your .env file.") + sys.exit(1) +def custom_fixed_fee_example(): + """ + + Demonstrates how to create a token with a Custom Fixed Fee. + + """ + + client, operator_id, operator_key = setup_client() + + print("\n--- Creating Custom Fixed Fee ---") -def custom_fixed_fee(): fixed_fee = CustomFixedFee( - amount=100, - denominating_token_id=TokenId(0, 0, 123), - fee_collector_account_id=AccountId(0, 0, 456), - all_collectors_are_exempt=False, + amount=Hbar(1).to_tinybars(), + fee_collector_account_id=operator_id, + all_collectors_are_exempt=False ) - print("\n--- Custom Fixed Fee ---") - print(fixed_fee) - # Convert to protobuf - fixed_fee_proto = fixed_fee._to_proto() + print(f"Fee Definition: Pay 1 HBAR to {operator_id}") + + print("\n--- Creating Token with Fee ---") + transaction = ( + TokenCreateTransaction() + .set_token_name("Fixed Fee Example Token") + .set_token_symbol("FFET") + .set_decimals(2) + .set_treasury_account_id(operator_id) + .set_token_type(TokenType.FUNGIBLE_COMMON) + .set_supply_type(SupplyType.INFINITE) + .set_initial_supply(1000) + .set_admin_key(operator_key) + .set_custom_fees([fixed_fee]) + .freeze_with(client) + .sign(operator_key) + ) + + try: + receipt = transaction.execute(client) + + # Check if the status is explicitly SUCCESS + if receipt.status != ResponseCode.SUCCESS: + print(f"Transaction failed with status: {ResponseCode(receipt.status).name}") + + token_id = receipt.token_id + print(f"Token created successfully with ID: {token_id}") + + print("\n--- Verifying Fee on Network ---") + token_info = TokenInfoQuery().set_token_id(token_id).execute(client) - print("Fixed Fee Protobuf:", fixed_fee_proto) + retrieved_fees = token_info.custom_fees + if retrieved_fees: + print(f"Success! Found {len(retrieved_fees)} custom fee(s) on token.") + for fee in retrieved_fees: + print(f"Fee Collector: {fee.fee_collector_account_id}") + print(f"Fee Details: {fee}") + else: + print("Error: No custom fees found on the token.") + except Exception as e: + print(f"Transaction failed: {e}") + sys.exit(1) + finally: + client.close() if __name__ == "__main__": - custom_fixed_fee() + custom_fixed_fee_example() \ No newline at end of file