Skip to content

Commit 61ad8dd

Browse files
fix: callback for _coerce_fee
Signed-off-by: tech0priyanshu <priyanshuyadv101106@gmail.com>
1 parent c744af3 commit 61ad8dd

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

src/hiero_sdk_python/client/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ def set_max_transaction_fee(
293293
self,
294294
max_transaction_fee: int | float | Decimal | Hbar,
295295
) -> Client:
296-
"""Sets the default maximum Hbar fee allowed for any transaction executed by this client."""
297-
value = max_transaction_fee if isinstance(max_transaction_fee, Hbar) else Hbar(max_transaction_fee)
298-
299-
if value < Hbar(0):
296+
"""
297+
Sets the default maximum Hbar fee allowed for any transaction executed by this client.
298+
"""
299+
value = Hbar._coerce_fee(max_transaction_fee)
300+
if value < Hbar.ZERO:
300301
raise ValueError("max_transaction_fee must be non-negative")
301-
302302
self.default_max_transaction_fee = value
303303
return self
304304

src/hiero_sdk_python/hbar.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ def negated(self) -> Hbar:
8383
"""
8484
return Hbar.from_tinybars(-self._amount_in_tinybar)
8585

86+
@staticmethod
87+
def _coerce_fee(value: int | float | Decimal | Hbar) -> Hbar:
88+
"""
89+
Coerce a value to an Hbar instance. Accepts int, float, Decimal, or Hbar (but not bool).
90+
Args:
91+
value: The value to coerce.
92+
Returns:
93+
Hbar: An Hbar instance.
94+
Raises:
95+
TypeError: If value is not a valid type.
96+
"""
97+
if isinstance(value, bool) or not isinstance(value, (int, float, Decimal, Hbar)):
98+
raise TypeError(f"value must be int, float, Decimal, or Hbar, got {type(value).__name__}")
99+
return value if isinstance(value, Hbar) else Hbar(value)
100+
86101
@classmethod
87102
def of(cls, amount: int | float | Decimal, unit: HbarUnit) -> Hbar:
88103
"""

src/hiero_sdk_python/transaction/transaction.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import hashlib
4-
from decimal import Decimal
54
from typing import TYPE_CHECKING, Literal, overload
65

76
from hiero_sdk_python.account.account_id import AccountId
@@ -795,19 +794,10 @@ def from_bytes(transaction_bytes: bytes):
795794
)
796795

797796
def set_max_transaction_fee(self, max_transaction_fee):
798-
# Accept int, float, Decimal, or Hbar (but not bool)
799797
self._require_not_frozen()
800-
801-
if isinstance(max_transaction_fee, bool) or not isinstance(max_transaction_fee, (int, float, Decimal, Hbar)):
802-
raise TypeError(
803-
f"max_transaction_fee must be int, float, Decimal, or Hbar, got {type(max_transaction_fee).__name__}"
804-
)
805-
806-
value = max_transaction_fee if isinstance(max_transaction_fee, Hbar) else Hbar(max_transaction_fee)
807-
808-
if value < Hbar(0):
798+
value = Hbar._coerce_fee(max_transaction_fee)
799+
if value < Hbar.ZERO:
809800
raise ValueError("max_transaction_fee must be non-negative")
810-
811801
self.transaction_fee = value
812802
return self
813803

0 commit comments

Comments
 (0)