Skip to content

Commit 64c3d17

Browse files
authored
feat: Refactor custom_royalty_fee.py example for end-to-end execution (hiero-ledger#1160)
Signed-off-by: Adityarya11 <[email protected]>
1 parent 3b7acfa commit 64c3d17

File tree

3 files changed

+125
-34
lines changed

3 files changed

+125
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
8181
- Format token examples with Black for consistent code style and improved readability (#1119)
8282
- Replaced `ResponseCode.get_name(receipt.status)` with the `ResponseCode(receipt.status).name` across examples and integration tests for consistency. (#1136)
8383
- Moved helpful references to Additional Context section and added clickable links.
84+
- Transformed `examples\tokens\custom_royalty_fee.py` to be an end-to-end example, that interacts with the Hedera network, rather than a static object demo.
85+
8486

8587

8688

examples/tokens/custom_fee_royalty.py

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
Run with:
3+
uv run examples/tokens/custom_royalty_fee.py
4+
python examples/tokens/custom_royalty_fee.py
5+
"""
6+
7+
import os
8+
import sys
9+
from dotenv import load_dotenv
10+
11+
from hiero_sdk_python.client.network import Network
12+
from hiero_sdk_python.crypto.private_key import PrivateKey
13+
from hiero_sdk_python.client.client import Client
14+
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
15+
from hiero_sdk_python.account.account_id import AccountId
16+
from hiero_sdk_python.response_code import ResponseCode
17+
from hiero_sdk_python.tokens.supply_type import SupplyType
18+
from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
19+
from hiero_sdk_python.tokens.custom_royalty_fee import CustomRoyaltyFee
20+
from hiero_sdk_python.hbar import Hbar
21+
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction
22+
from hiero_sdk_python.tokens.token_type import TokenType
23+
24+
load_dotenv()
25+
network_name = os.getenv("NETWORK", "testnet").lower()
26+
27+
def setup_client():
28+
"""Initialize and set up the client with operator account"""
29+
30+
network = Network(network_name)
31+
print(f"Connecting to the Hedera {network_name} network")
32+
client = Client(network)
33+
34+
try:
35+
operator_id_str = os.getenv('OPERATOR_ID')
36+
operator_key_str = os.getenv('OPERATOR_KEY')
37+
38+
if not operator_id_str or not operator_key_str:
39+
raise ValueError("Environment variables OPERATOR_ID or OPERATOR_KEY are missing.")
40+
41+
operator_id = AccountId.from_string(operator_id_str)
42+
operator_key = PrivateKey.from_string(operator_key_str)
43+
44+
client.set_operator(operator_id, operator_key)
45+
print(f"Client set up with operator id {client.operator_account_id}")
46+
return client, operator_id, operator_key
47+
48+
except (TypeError, ValueError) as e:
49+
print(f"Error: {e}")
50+
print("Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
51+
sys.exit(1)
52+
53+
def custom_royalty_fee_example():
54+
"""Demonstrates how to create a token with a custom royalty fee."""
55+
56+
client, operator_id, operator_key = setup_client()
57+
58+
with client:
59+
print("\n--- Creating Custom Royalty Fee ---")
60+
61+
fallback_fee = CustomFixedFee(
62+
amount=Hbar(1).to_tinybars(),
63+
fee_collector_account_id=operator_id,
64+
all_collectors_are_exempt=False
65+
)
66+
67+
royalty_fee = CustomRoyaltyFee(
68+
numerator=5,
69+
denominator=100,
70+
fallback_fee=fallback_fee,
71+
fee_collector_account_id=operator_id,
72+
all_collectors_are_exempt=False
73+
)
74+
75+
print(f"Royalty Fee Configured: {royalty_fee.numerator}/{royalty_fee.denominator}")
76+
print(f"Fallback Fee: {Hbar.from_tinybars(fallback_fee.amount)} HBAR")
77+
78+
print("\n--- Creating Token with Royalty Fee ---")
79+
transaction = (
80+
TokenCreateTransaction()
81+
.set_token_name("Royalty NFT Collection")
82+
.set_token_symbol("RNFT")
83+
.set_treasury_account_id(operator_id)
84+
.set_admin_key(operator_key)
85+
.set_supply_key(operator_key)
86+
.set_token_type(TokenType.NON_FUNGIBLE_UNIQUE)
87+
.set_decimals(0)
88+
.set_initial_supply(0)
89+
.set_supply_type(SupplyType.FINITE)
90+
.set_max_supply(100)
91+
.set_custom_fees([royalty_fee])
92+
.freeze_with(client)
93+
.sign(operator_key)
94+
)
95+
96+
try:
97+
receipt = transaction.execute(client)
98+
99+
if receipt.status != ResponseCode.SUCCESS:
100+
print(f"Transaction failed with status: {ResponseCode(receipt.status).name}")
101+
return
102+
103+
token_id = receipt.token_id
104+
print(f"Token created successfully with ID: {token_id}")
105+
106+
print("\n--- Verifying Fee on Network ---")
107+
token_info = TokenInfoQuery().set_token_id(token_id).execute(client)
108+
109+
retrieved_fees = token_info.custom_fees
110+
if retrieved_fees:
111+
print(f"Success! Found {len(retrieved_fees)} custom fee(s) on token.")
112+
for fee in retrieved_fees:
113+
print(f"Fee Collector: {fee.fee_collector_account_id}")
114+
print(f"Fee Details: {fee}")
115+
else:
116+
print("Error: No custom fees found on the token.")
117+
118+
except Exception as e:
119+
print(f"Transaction execution failed: {e}")
120+
sys.exit(1)
121+
122+
if __name__ == "__main__":
123+
custom_royalty_fee_example()

0 commit comments

Comments
 (0)