Skip to content

Commit 6ce63da

Browse files
authored
chore: format examples/tokens directory using black code formatter (#1318)
Signed-off-by: Mounil <[email protected]>
1 parent 8980910 commit 6ce63da

File tree

6 files changed

+125
-96
lines changed

6 files changed

+125
-96
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
1818
- Added method chaining examples to the developer training guide (`docs/sdk_developers/training/coding_token_transactions.md`) (#1194)
1919
- Added documentation explaining how to pin GitHub Actions to specific commit SHAs (`docs/sdk_developers/how-to-pin-github-actions.md`)(#1211)
2020
- examples/mypy.ini for stricter type checking in example scripts
21+
- Formatted examples/tokens directory using black code formatter for consistent code style
2122
- Added a GitHub Actions workflow that reminds contributors to link pull requests to issues.
2223
- Added `__str__` and `__repr__` methods to `AccountInfo` class for improved logging and debugging experience (#1098)
2324
- Added Good First Issue (GFI) management and frequency documentation to clarify maintainer expectations and SDK-level GFI governance.

examples/tokens/custom_fee_fixed.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,41 @@
2424
load_dotenv()
2525
network_name = os.getenv("NETWORK", "testnet").lower()
2626

27+
2728
def setup_client():
2829
"""Initialize and set up the client with operator account"""
2930
# Initialize network and client
3031
network = Network(network_name)
3132
print(f"Connecting to Hedera {network_name} network!")
3233
client = Client(network)
33-
34+
3435
# This disables the SSL error in the local development environment (Keep commented for production) #
35-
36+
3637
# client.set_transport_security(False)
3738
# client.set_verify_certificates(False)
3839

3940
try:
40-
operator_id_str = os.getenv('OPERATOR_ID')
41-
operator_key_str = os.getenv('OPERATOR_KEY')
41+
operator_id_str = os.getenv("OPERATOR_ID")
42+
operator_key_str = os.getenv("OPERATOR_KEY")
4243

4344
if not operator_id_str or not operator_key_str:
44-
raise ValueError("Environment variables OPERATOR_ID or OPERATOR_KEY are missing.")
45+
raise ValueError(
46+
"Environment variables OPERATOR_ID or OPERATOR_KEY are missing."
47+
)
4548

4649
operator_id = AccountId.from_string(operator_id_str)
4750
operator_key = PrivateKey.from_string(operator_key_str)
4851

4952
client.set_operator(operator_id, operator_key)
5053
print(f"Client set up with operator id {client.operator_account_id}")
5154
return client, operator_id, operator_key
52-
55+
5356
except (TypeError, ValueError) as e:
5457
print(f"Error: {e}")
5558
print("Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
5659
sys.exit(1)
5760

61+
5862
def custom_fixed_fee_example():
5963
"""
6064
@@ -67,9 +71,9 @@ def custom_fixed_fee_example():
6771
print("\n--- Creating Custom Fixed Fee ---")
6872

6973
fixed_fee = CustomFixedFee(
70-
amount=Hbar(1).to_tinybars(),
74+
amount=Hbar(1).to_tinybars(),
7175
fee_collector_account_id=operator_id,
72-
all_collectors_are_exempt=False
76+
all_collectors_are_exempt=False,
7377
)
7478

7579
print(f"Fee Definition: Pay 1 HBAR to {operator_id}")
@@ -85,17 +89,19 @@ def custom_fixed_fee_example():
8589
.set_supply_type(SupplyType.INFINITE)
8690
.set_initial_supply(1000)
8791
.set_admin_key(operator_key)
88-
.set_custom_fees([fixed_fee])
92+
.set_custom_fees([fixed_fee])
8993
.freeze_with(client)
9094
.sign(operator_key)
9195
)
9296

9397
try:
9498
receipt = transaction.execute(client)
95-
99+
96100
# Check if the status is explicitly SUCCESS
97101
if receipt.status != ResponseCode.SUCCESS:
98-
print(f"Transaction failed with status: {ResponseCode(receipt.status).name}")
102+
print(
103+
f"Transaction failed with status: {ResponseCode(receipt.status).name}"
104+
)
99105

100106
token_id = receipt.token_id
101107
print(f"Token created successfully with ID: {token_id}")
@@ -118,5 +124,6 @@ def custom_fixed_fee_example():
118124
finally:
119125
client.close()
120126

127+
121128
if __name__ == "__main__":
122-
custom_fixed_fee_example()
129+
custom_fixed_fee_example()

examples/tokens/custom_royalty_fee.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
uv run examples/tokens/custom_royalty_fee.py
44
python examples/tokens/custom_royalty_fee.py
55
"""
6-
6+
77
import os
88
import sys
99
from dotenv import load_dotenv
@@ -23,84 +23,92 @@
2323

2424
load_dotenv()
2525

26+
2627
def 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+
5256
def 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+
7176
def 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+
101109
def 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+
116125
def 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+
128140
if __name__ == "__main__":
129-
main()
141+
main()

0 commit comments

Comments
 (0)