Skip to content

Commit b97935d

Browse files
authored
chore: refactor custom_royalty_fee.py into modular functions (hiero-ledger#1176)
Signed-off-by: levitepeter <[email protected]>
1 parent 7e3a67d commit b97935d

File tree

2 files changed

+56
-49
lines changed

2 files changed

+56
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
8686
- Replaced `ResponseCode.get_name(receipt.status)` with the `ResponseCode(receipt.status).name` across examples and integration tests for consistency. (#1136)
8787
- Moved helpful references to Additional Context section and added clickable links.
8888
- 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.
89+
- Refactored `examples/tokens/custom_royalty_fee.py` by splitting monolithic function custom_royalty_fee_example() into modular functions create_royalty_fee_object(), create_token_with_fee(), verify_token_fee(), and main() to improve readability, cleaned up setup_client() (#1169)
8990

9091

9192

examples/tokens/custom_royalty_fee.py

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
from hiero_sdk_python.tokens.token_type import TokenType
2323

2424
load_dotenv()
25-
network_name = os.getenv("NETWORK", "testnet").lower()
2625

2726
def setup_client():
2827
"""Initialize and set up the client with operator account"""
2928

30-
network = Network(network_name)
31-
print(f"Connecting to the Hedera {network_name} network")
32-
client = Client(network)
33-
3429
try:
30+
network_name = os.getenv("NETWORK", "testnet").lower()
31+
network = Network(network_name)
32+
print(f"Connecting to the Hedera {network_name} network")
33+
client = Client(network)
34+
3535
operator_id_str = os.getenv('OPERATOR_ID')
3636
operator_key_str = os.getenv('OPERATOR_KEY')
3737

@@ -47,36 +47,32 @@ def setup_client():
4747

4848
except (TypeError, ValueError) as e:
4949
print(f"Error: {e}")
50-
print("Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
5150
sys.exit(1)
5251

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(
52+
def create_royalty_fee_object(operator_id):
53+
"""Creates the CustomRoyaltyFee object with a fallback fee."""
54+
fallback_fee = CustomFixedFee(
6255
amount=Hbar(1).to_tinybars(),
6356
fee_collector_account_id=operator_id,
6457
all_collectors_are_exempt=False
6558
)
66-
67-
royalty_fee = CustomRoyaltyFee(
59+
60+
royalty_fee = CustomRoyaltyFee(
6861
numerator=5,
6962
denominator=100,
7063
fallback_fee=fallback_fee,
7164
fee_collector_account_id=operator_id,
7265
all_collectors_are_exempt=False
73-
)
66+
)
67+
print(f"Royalty Fee Configured: {royalty_fee.numerator}/{royalty_fee.denominator}")
68+
print(f"Fallback Fee: {Hbar.from_tinybars(fallback_fee.amount)} HBAR")
69+
return royalty_fee
7470

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 = (
71+
def create_token_with_fee(client, operator_id, operator_key, royalty_fee):
72+
"""Creates a token with the specified royalty fee attached."""
73+
74+
print("\n--- Creating Token with Royalty Fee ---")
75+
transaction = (
8076
TokenCreateTransaction()
8177
.set_token_name("Royalty NFT Collection")
8278
.set_token_symbol("RNFT")
@@ -91,33 +87,43 @@ def custom_royalty_fee_example():
9187
.set_custom_fees([royalty_fee])
9288
.freeze_with(client)
9389
.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
90+
)
10291

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.")
92+
receipt = transaction.execute(client)
93+
if receipt.status != ResponseCode.SUCCESS:
94+
print(f"Token creation failed: {ResponseCode(receipt.status).name}")
95+
raise RuntimeError(f"Token creation failed: {ResponseCode(receipt.status).name}")
96+
97+
token_id = receipt.token_id
98+
print(f"Token created successfully with ID: {token_id}")
99+
return token_id
100+
101+
def verify_token_fee(client, token_id):
102+
"""Queries the network to verify the fee exists."""
103+
104+
print("\n--- Verifying Fee on Network ---")
105+
token_info = TokenInfoQuery().set_token_id(token_id).execute(client)
106+
retrieved_fees = token_info.custom_fees
107+
108+
if retrieved_fees:
109+
print(f"Success! Found {len(retrieved_fees)} custom fee(s) on token.")
110+
for fee in retrieved_fees:
111+
print(f"Fee Collector: {fee.fee_collector_account_id}")
112+
print(f"Fee Details: {fee}")
113+
else:
114+
print("Error: No custom fees found on the token.")
115+
116+
def main():
117+
"""Main execution flow."""
118+
client, operator_id, operator_key = setup_client()
117119

120+
with client:
121+
try:
122+
royalty_fee = create_royalty_fee_object(operator_id)
123+
token_id = create_token_with_fee(client, operator_id, operator_key, royalty_fee)
124+
verify_token_fee(client, token_id)
118125
except Exception as e:
119-
print(f"Transaction execution failed: {e}")
120-
sys.exit(1)
121-
126+
print(f"Execution failed: {e}")
127+
122128
if __name__ == "__main__":
123-
custom_royalty_fee_example()
129+
main()

0 commit comments

Comments
 (0)