diff --git a/CHANGELOG.md b/CHANGELOG.md index dba45074c..b3b4eed07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1. - 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) +- Transformed `examples/tokens/custom_fee_royalty.py` to be an end-to-end example, that interacts with the Hedera network, rather than a static object demo. diff --git a/examples/tokens/custom_fee_royalty.py b/examples/tokens/custom_fee_royalty.py index 0c0c8c2b0..a13321021 100644 --- a/examples/tokens/custom_fee_royalty.py +++ b/examples/tokens/custom_fee_royalty.py @@ -4,31 +4,122 @@ python examples/tokens/custom_royalty_fee.py """ +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.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.tokens.custom_royalty_fee import CustomRoyaltyFee -from hiero_sdk_python.account.account_id import AccountId -from hiero_sdk_python.tokens.token_id import TokenId +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""" + + network = Network(network_name) + print(f"Connecting to the Hedera {network_name} network") + client = Client(network) + + 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.") -def custom_royalty_fee(): + 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_royalty_fee_example(): + """Demonstrates how to create a token with a custom royalty fee.""" + + client, operator_id, operator_key = setup_client() + + + + print("\n--- Creating Custom Royalty Fee ---") + fallback_fee = CustomFixedFee( - amount=50, - denominating_token_id=TokenId(0, 0, 789), + amount=Hbar(1).to_tinybars(), + fee_collector_account_id=operator_id, + all_collectors_are_exempt=False ) + royalty_fee = CustomRoyaltyFee( - numerator=5, + numerator=5, denominator=100, fallback_fee=fallback_fee, - fee_collector_account_id=AccountId(0, 0, 456), - all_collectors_are_exempt=True, + fee_collector_account_id=operator_id, + all_collectors_are_exempt=False ) - print(royalty_fee) + + print(f"Royalty Fee Configured: 5/100 (5%)") + print(f"Fallback Fee: 1 HBAR") + + print(f"\n--- Creating Token with Royalty Fee ---") + transaction = ( + TokenCreateTransaction() + .set_token_name("Royalty NFT Collection") + .set_token_symbol("RNFT") + .set_treasury_account_id(operator_id) + .set_admin_key(operator_key) + .set_supply_key(operator_key) + .set_token_type(TokenType.NON_FUNGIBLE_UNIQUE) + .set_decimals(0) + .set_initial_supply(0) + .set_supply_type(SupplyType.FINITE) + .set_max_supply(100) + .set_custom_fees([royalty_fee]) + .freeze_with(client) + .sign(operator_key) + ) + + try: + # Execute the transaction + receipt = transaction.execute(client) + + if receipt.status != ResponseCode.SUCCESS: + print(f"Transaction failed with status: {ResponseCode(receipt.status).name}") + return + + token_id = receipt.token_id + print(f"Token created successfully with ID: {token_id}") - # Convert to protobuf - royalty_fee_proto = royalty_fee._to_proto() + print("\n--- Verifying Fee on Network ---") + token_info = TokenInfoQuery().set_token_id(token_id).execute(client) - print("Royalty Fee Protobuf:", royalty_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(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_royalty_fee() + custom_royalty_fee_example() \ No newline at end of file