33uv run examples/tokens/custom_royalty_fee.py
44python examples/tokens/custom_royalty_fee.py
55"""
6-
6+
77import os
88import sys
99from dotenv import load_dotenv
2323
2424load_dotenv ()
2525
26+
2627def setup_client ():
2728 """Initialize and set up the client with operator account"""
28-
29+
2930 try :
3031 network_name = os .getenv ("NETWORK" , "testnet" ).lower ()
3132 network = Network (network_name )
3233 print (f"Connecting to the Hedera { network_name } network" )
3334 client = Client (network )
3435
35- operator_id_str = os .getenv (' OPERATOR_ID' )
36- operator_key_str = os .getenv (' OPERATOR_KEY' )
36+ operator_id_str = os .getenv (" OPERATOR_ID" )
37+ operator_key_str = os .getenv (" OPERATOR_KEY" )
3738
3839 if not operator_id_str or not operator_key_str :
39- raise ValueError ("Environment variables OPERATOR_ID or OPERATOR_KEY are missing." )
40+ raise ValueError (
41+ "Environment variables OPERATOR_ID or OPERATOR_KEY are missing."
42+ )
4043
4144 operator_id = AccountId .from_string (operator_id_str )
4245 operator_key = PrivateKey .from_string (operator_key_str )
4346
4447 client .set_operator (operator_id , operator_key )
4548 print (f"Client set up with operator id { client .operator_account_id } " )
4649 return client , operator_id , operator_key
47-
50+
4851 except (TypeError , ValueError ) as e :
4952 print (f"Error: { e } " )
5053 sys .exit (1 )
5154
55+
5256def create_royalty_fee_object (operator_id ):
5357 """Creates the CustomRoyaltyFee object with a fallback fee."""
5458 fallback_fee = CustomFixedFee (
55- amount = Hbar (1 ).to_tinybars (),
56- fee_collector_account_id = operator_id ,
57- all_collectors_are_exempt = False
58- )
59-
60- royalty_fee = CustomRoyaltyFee (
61- numerator = 5 ,
62- denominator = 100 ,
63- fallback_fee = fallback_fee ,
64- fee_collector_account_id = operator_id ,
65- all_collectors_are_exempt = False
59+ amount = Hbar (1 ).to_tinybars (),
60+ fee_collector_account_id = operator_id ,
61+ all_collectors_are_exempt = False ,
62+ )
63+
64+ royalty_fee = CustomRoyaltyFee (
65+ numerator = 5 ,
66+ denominator = 100 ,
67+ fallback_fee = fallback_fee ,
68+ fee_collector_account_id = operator_id ,
69+ all_collectors_are_exempt = False ,
6670 )
6771 print (f"Royalty Fee Configured: { royalty_fee .numerator } /{ royalty_fee .denominator } " )
6872 print (f"Fallback Fee: { Hbar .from_tinybars (fallback_fee .amount )} HBAR" )
6973 return royalty_fee
7074
75+
7176def create_token_with_fee (client , operator_id , operator_key , royalty_fee ):
7277 """Creates a token with the specified royalty fee attached."""
73-
78+
7479 print ("\n --- Creating Token with Royalty Fee ---" )
7580 transaction = (
76- TokenCreateTransaction ()
77- .set_token_name ("Royalty NFT Collection" )
78- .set_token_symbol ("RNFT" )
79- .set_treasury_account_id (operator_id )
80- .set_admin_key (operator_key )
81- .set_supply_key (operator_key )
82- .set_token_type (TokenType .NON_FUNGIBLE_UNIQUE )
83- .set_decimals (0 )
84- .set_initial_supply (0 )
85- .set_supply_type (SupplyType .FINITE )
86- .set_max_supply (100 )
87- .set_custom_fees ([royalty_fee ])
88- .freeze_with (client )
89- .sign (operator_key )
90- )
81+ TokenCreateTransaction ()
82+ .set_token_name ("Royalty NFT Collection" )
83+ .set_token_symbol ("RNFT" )
84+ .set_treasury_account_id (operator_id )
85+ .set_admin_key (operator_key )
86+ .set_supply_key (operator_key )
87+ .set_token_type (TokenType .NON_FUNGIBLE_UNIQUE )
88+ .set_decimals (0 )
89+ .set_initial_supply (0 )
90+ .set_supply_type (SupplyType .FINITE )
91+ .set_max_supply (100 )
92+ .set_custom_fees ([royalty_fee ])
93+ .freeze_with (client )
94+ .sign (operator_key )
95+ )
9196
9297 receipt = transaction .execute (client )
9398 if receipt .status != ResponseCode .SUCCESS :
9499 print (f"Token creation failed: { ResponseCode (receipt .status ).name } " )
95- raise RuntimeError (f"Token creation failed: { ResponseCode (receipt .status ).name } " )
96-
100+ raise RuntimeError (
101+ f"Token creation failed: { ResponseCode (receipt .status ).name } "
102+ )
103+
97104 token_id = receipt .token_id
98105 print (f"Token created successfully with ID: { token_id } " )
99106 return token_id
100-
107+
108+
101109def verify_token_fee (client , token_id ):
102110 """Queries the network to verify the fee exists."""
103-
111+
104112 print ("\n --- Verifying Fee on Network ---" )
105113 token_info = TokenInfoQuery ().set_token_id (token_id ).execute (client )
106114 retrieved_fees = token_info .custom_fees
@@ -112,18 +120,22 @@ def verify_token_fee(client, token_id):
112120 print (f"Fee Details: { fee } " )
113121 else :
114122 print ("Error: No custom fees found on the token." )
115-
123+
124+
116125def main ():
117126 """Main execution flow."""
118127 client , operator_id , operator_key = setup_client ()
119128
120129 with client :
121130 try :
122131 royalty_fee = create_royalty_fee_object (operator_id )
123- token_id = create_token_with_fee (client , operator_id , operator_key , royalty_fee )
132+ token_id = create_token_with_fee (
133+ client , operator_id , operator_key , royalty_fee
134+ )
124135 verify_token_fee (client , token_id )
125136 except Exception as e :
126137 print (f"Execution failed: { e } " )
127-
138+
139+
128140if __name__ == "__main__" :
129- main ()
141+ main ()
0 commit comments