Skip to content

Commit baa212b

Browse files
feat: add __str__ to CustomFixedFee and update examples/tests (#976)
Signed-off-by: undefinedIsMyLife <shinetina169@gmail.com>
1 parent 1a4bbe5 commit baa212b

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Added
10+
- Added __str__() to CustomFixedFee and updated examples and tests accordingly.
1011
- Added a github template for good first issues
1112
- Added `.github/workflows/bot-assignment-check.yml` to limit non-maintainers to 2 concurrent issue assignments.
1213
- Added all missing fields to __str__() method and updated `test_tokem_info.py`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ This project is licensed under the [Apache License 2.0](LICENSE).
134134

135135
---
136136

137-
**Latest release:** Check [PyPI](https://pypi.org/project/hiero-sdk-python/) or [GitHub Releases](https://github.com/hiero-ledger/hiero-sdk-python/releases)
137+
**Latest release:** Check [PyPI](https://pypi.org/project/hiero-sdk-python/) or [GitHub Releases](https://github.com/hiero-ledger/hiero-sdk-python/releases)

examples/tokens/custom_fixed_fee.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ def custom_fixed_fee():
1414
fee_collector_account_id=AccountId(0, 0, 456),
1515
all_collectors_are_exempt=False,
1616
)
17-
print("CustomFixedFee:")
18-
print(f"Amount: {fixed_fee.amount}")
19-
print(f"Denominating Token ID: {fixed_fee.denominating_token_id}")
20-
print(f"Fee Collector Account ID: {fixed_fee.fee_collector_account_id}")
21-
print(f"All Collectors Exempt: {fixed_fee.all_collectors_are_exempt}")
17+
print("\n--- Custom Fixed Fee ---")
18+
print(fixed_fee)
19+
2220
# Convert to protobuf
2321
fixed_fee_proto = fixed_fee._to_proto()
2422

src/hiero_sdk_python/tokens/custom_fixed_fee.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ def __init__(
6161
super().__init__(fee_collector_account_id, all_collectors_are_exempt)
6262
self.amount = amount
6363
self.denominating_token_id = denominating_token_id
64+
65+
def __str__(self) -> str:
66+
"""Return a user-friendly string representation of the CustomFixedFee.
67+
68+
Displays all fields in insertion order with aligned formatting.
69+
TokenId and AccountId objects are displayed in Hedera notation (e.g., 0.0.123).
70+
"""
71+
72+
fields = {
73+
key.replace("_", " ").title(): value
74+
for key, value in self.__dict__.items()
75+
}
76+
77+
if not fields:
78+
return f"{self.__class__.__name__}()"
79+
80+
max_len = max(len(name) for name in fields)
81+
82+
lines = [f"{self.__class__.__name__}:"]
83+
for name, value in fields.items(): # preserves insertion order
84+
lines.append(f" {name:<{max_len}} = {value}") # uses __str__()
85+
86+
return "\n".join(lines)
87+
6488

6589
def set_amount_in_tinybars(self, amount: int) -> "CustomFixedFee":
6690
"""Sets the fee amount in tinybars.

tests/unit/test_custom_fee.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,60 @@
99

1010
pytestmark = pytest.mark.unit
1111

12-
def test_custom_fixed_fee():
12+
def test_custom_fixed_fee_proto_round_trip():
13+
"""Ensure CustomFixedFee protobuf serialization and deserialization behave correctly."""
1314
fee = CustomFixedFee(
1415
amount=100,
1516
denominating_token_id=TokenId(0, 0, 123),
1617
fee_collector_account_id=AccountId(0, 0, 456),
1718
all_collectors_are_exempt=True,
1819
)
1920

20-
proto = fee._to_proto() # Changed from _to_protobuf
21-
new_fee = CustomFixedFee._from_proto(proto) # Changed from CustomFee._from_protobuf
21+
proto = fee._to_proto()
22+
new_fee = CustomFixedFee._from_proto(proto)
2223

2324
assert isinstance(new_fee, CustomFixedFee)
2425
assert new_fee.amount == 100
2526
assert new_fee.denominating_token_id == TokenId(0, 0, 123)
2627
assert new_fee.fee_collector_account_id == AccountId(0, 0, 456)
2728
assert new_fee.all_collectors_are_exempt is True
2829

30+
def test_custom_fixed_fee_str():
31+
"""Test the string representation of CustomFixedFee."""
32+
fee = CustomFixedFee(
33+
amount=100,
34+
denominating_token_id=TokenId(0, 0, 123),
35+
fee_collector_account_id=AccountId(0, 0, 456),
36+
all_collectors_are_exempt=False,
37+
)
38+
39+
fee_str = fee.__str__()
40+
41+
# Basic checks
42+
assert "CustomFixedFee" in fee_str
43+
assert "Amount" in fee_str
44+
assert "Denominating Token Id" in fee_str
45+
assert "Fee Collector Account Id" in fee_str
46+
assert "All Collectors Are Exempt" in fee_str
47+
48+
# Key-value extraction
49+
kv = {}
50+
for ln in fee_str.splitlines()[1:]:
51+
if "=" in ln:
52+
key, val = ln.split("=", 1)
53+
kv[key.strip()] = val.strip()
54+
55+
expected = {
56+
"Amount": "100",
57+
"Denominating Token Id": "0.0.123",
58+
"Fee Collector Account Id": "0.0.456",
59+
"All Collectors Are Exempt": "False",
60+
}
61+
62+
for key, val in expected.items():
63+
assert kv[key] == val, f"{key} incorrect in string representation"
64+
65+
2966
def test_custom_fractional_fee_str():
3067
"""Test the string representation of CustomFractionalFee."""
3168
fee = CustomFractionalFee(

0 commit comments

Comments
 (0)