Skip to content

Commit 80db3e3

Browse files
committed
fix: more cyclo complexity
Signed-off-by: exploreriii <[email protected]>
1 parent d81eac5 commit 80db3e3

File tree

3 files changed

+90
-56
lines changed

3 files changed

+90
-56
lines changed

src/hiero_sdk_python/tokens/token_create_transaction.py

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from hiero_sdk_python.account.account_id import AccountId
2424
from hiero_sdk_python.crypto.private_key import PrivateKey
2525
from hiero_sdk_python.tokens.custom_fee import CustomFee
26+
from hiero_sdk_python.crypto.public_key import PublicKey
2627

2728
DEFAULT_TRANSACTION_FEE = 3_000_000_000
2829

@@ -401,29 +402,21 @@ def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
401402
# Validate freeze status
402403
TokenCreateValidator._validate_token_freeze_status(self._keys, self._token_params)
403404

404-
admin_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.admin_key)
405-
supply_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.supply_key)
406-
freeze_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.freeze_key)
407-
wipe_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.wipe_key)
408-
metadata_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.metadata_key)
409-
pause_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.pause_key)
410-
kyc_key_proto: Optional[basic_types_pb2.Key] = self._to_proto_key(self._keys.kyc_key)
411-
412-
# Ensure token type is correctly set with default to fungible
413-
if self._token_params.token_type is None:
414-
token_type_value = 0 # default FUNGIBLE_COMMON
415-
elif isinstance(self._token_params.token_type, TokenType):
416-
token_type_value = self._token_params.token_type.value
417-
else:
418-
token_type_value = self._token_params.token_type
419-
420-
# Ensure supply type is correctly set with default to infinite
421-
if self._token_params.supply_type is None:
422-
supply_type_value = 0 # default INFINITE
423-
elif isinstance(self._token_params.supply_type, SupplyType):
424-
supply_type_value = self._token_params.supply_type.value
425-
else:
426-
supply_type_value = self._token_params.supply_type
405+
# --- small helpers to keep this method simple ---
406+
def _enum_value(val, enum_cls, default_int: int) -> int:
407+
"""Return enum int for None/enum/int input, with a default."""
408+
if val is None:
409+
return default_int
410+
if isinstance(val, enum_cls):
411+
return val.value
412+
return int(val)
413+
414+
def _key_proto(pk: Optional[PublicKey]) -> Optional[basic_types_pb2.Key]:
415+
return self._to_proto_key(pk)
416+
417+
# Resolve enum-like params with defaults (FUNGIBLE_COMMON=0, INFINITE=0)
418+
token_type_value = _enum_value(self._token_params.token_type, TokenType, 0)
419+
supply_type_value = _enum_value(self._token_params.supply_type, SupplyType, 0)
427420

428421
# Construct the TokenCreateTransactionBody
429422
token_create_body = token_create_pb2.TokenCreateTransactionBody(
@@ -438,29 +431,27 @@ def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
438431
treasury=self._token_params.treasury_account_id._to_proto(),
439432
)
440433

441-
# Conditionally attach each optional sub-message
442-
if admin_key_proto is not None:
443-
token_create_body.adminKey.CopyFrom(admin_key_proto)
444-
if supply_key_proto is not None:
445-
token_create_body.supplyKey.CopyFrom(supply_key_proto)
446-
if freeze_key_proto is not None:
447-
token_create_body.freezeKey.CopyFrom(freeze_key_proto)
448-
if wipe_key_proto is not None:
449-
token_create_body.wipeKey.CopyFrom(wipe_key_proto)
450-
if metadata_key_proto is not None:
451-
token_create_body.metadata_key.CopyFrom(metadata_key_proto)
452-
if pause_key_proto is not None:
453-
token_create_body.pause_key.CopyFrom(pause_key_proto)
454-
if kyc_key_proto is not None:
455-
token_create_body.kycKey.CopyFrom(kyc_key_proto)
434+
# Conditionally attach each optional sub-message (data-driven)
435+
key_map = (
436+
(_key_proto(self._keys.admin_key), ("adminKey",)),
437+
(_key_proto(self._keys.supply_key), ("supplyKey",)),
438+
(_key_proto(self._keys.freeze_key), ("freezeKey",)),
439+
(_key_proto(self._keys.wipe_key), ("wipeKey",)),
440+
(_key_proto(self._keys.metadata_key), ("metadata_key",)), # snake_case in recent protos
441+
(_key_proto(self._keys.pause_key), ("pause_key",)), # snake_case
442+
(_key_proto(self._keys.kyc_key), ("kycKey",)),
443+
)
444+
for key_proto, path in key_map:
445+
if key_proto is not None:
446+
getattr(token_create_body, path[0]).CopyFrom(key_proto)
447+
448+
# Custom fees (append if present)
456449
if self._token_params.custom_fees:
457-
for fee in self._token_params.custom_fees:
458-
token_create_body.custom_fees.append(fee._to_proto())
450+
token_create_body.custom_fees.extend(f._to_proto() for f in self._token_params.custom_fees)
459451

460452
# Build the base transaction body and attach the token creation details
461453
transaction_body: transaction_body_pb2.TransactionBody = self.build_base_transaction_body()
462454
transaction_body.tokenCreation.CopyFrom(token_create_body)
463-
464455
return transaction_body
465456

466457
def _get_method(self, channel: _Channel) -> _Method:

src/hiero_sdk_python/tokens/token_info.py

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,52 @@ def _from_proto(cls, proto_obj: hapi_pb.TokenInfo) -> "TokenInfo":
333333

334334
return tokenInfoObject
335335

336+
# === helpers ===
337+
@staticmethod
338+
def _copy_key_if_present(dst: "TokenInfo", setter: str, key_msg) -> None:
339+
# In proto3, keys are a oneof; check presence via WhichOneof
340+
if key_msg is not None and hasattr(key_msg, "WhichOneof") and key_msg.WhichOneof("key"):
341+
getattr(dst, setter)(PublicKey._from_proto(key_msg))
342+
343+
@staticmethod
344+
def _parse_custom_fees(proto_obj) -> List[CustomFee]:
345+
out: List[CustomFee] = []
346+
for fee_proto in getattr(proto_obj, "custom_fees", []): # snake_case matches your generated proto
347+
if fee_proto.HasField("fixed_fee"):
348+
out.append(CustomFixedFee._from_proto(fee_proto))
349+
elif fee_proto.HasField("fractional_fee"):
350+
out.append(CustomFractionalFee._from_proto(fee_proto))
351+
elif fee_proto.HasField("royalty_fee"):
352+
out.append(CustomRoyaltyFee._from_proto(fee_proto))
353+
return out
354+
355+
@staticmethod
356+
def _copy_msg_to_proto(src_obj, dst_proto, src_attr: str, dst_field: str) -> None:
357+
"""Copy a submessage if present, using _to_proto or _to_protobuf as available."""
358+
val = getattr(src_obj, src_attr)
359+
if not val:
360+
return
361+
to_fn = getattr(val, "_to_proto", None) or getattr(val, "_to_protobuf", None)
362+
if to_fn is None:
363+
raise AttributeError(f"{type(val).__name__} has neither _to_proto nor _to_protobuf")
364+
getattr(dst_proto, dst_field).CopyFrom(to_fn())
365+
366+
@staticmethod
367+
def _set_bool(dst_proto, field_name: str, value: Optional[bool], default: bool = False) -> None:
368+
"""Assign a boolean, using default when value is None."""
369+
setattr(dst_proto, field_name, default if value is None else bool(value))
370+
371+
@staticmethod
372+
def _set_enum(dst_proto, field_name: str, enum_val) -> None:
373+
"""Assign an enum value or 0 if None."""
374+
setattr(dst_proto, field_name, (enum_val.value if enum_val is not None else 0))
375+
376+
@staticmethod
377+
def _append_custom_fees(dst_proto, fees: Optional[List[Any]]) -> None:
378+
"""Append serialized custom fees if any."""
379+
if fees:
380+
dst_proto.custom_fees.extend(fee._to_proto() for fee in fees)
381+
336382
def _to_proto(self) -> hapi_pb.TokenInfo:
337383
"""
338384
Converts the TokenInfo instance to a protobuf TokenInfo object.
@@ -352,10 +398,10 @@ def _to_proto(self) -> hapi_pb.TokenInfo:
352398
metadata=self.metadata,
353399
)
354400

355-
for fee in self.custom_fees or []:
356-
proto.custom_fees.append(fee._to_proto())
401+
# Custom fees
402+
self._append_custom_fees(proto, self.custom_fees)
357403

358-
for src_attr, dst_field in [
404+
msg_fields = [
359405
("admin_key", "adminKey"),
360406
("kyc_key", "kycKey"),
361407
("freeze_key", "freezeKey"),
@@ -365,20 +411,16 @@ def _to_proto(self) -> hapi_pb.TokenInfo:
365411
("fee_schedule_key", "fee_schedule_key"),
366412
("pause_key", "pause_key"),
367413
("auto_renew_account", "autoRenewAccount"),
368-
]:
414+
("auto_renew_period", "autoRenewPeriod"),
415+
("expiry", "expiry"),
416+
]
417+
for src_attr, dst_field in msg_fields:
369418
self._copy_msg_to_proto(self, proto, src_attr, dst_field)
370419

371-
if self.is_deleted is not None:
372-
proto.deleted = self.is_deleted
373-
374-
proto.defaultFreezeStatus = self.default_freeze_status.value
375-
proto.defaultKycStatus = self.default_kyc_status.value
376-
proto.pause_status = self.pause_status.value
377-
378-
if self.auto_renew_period:
379-
proto.autoRenewPeriod.CopyFrom(self.auto_renew_period._to_proto())
380-
if self.expiry:
381-
proto.expiry.CopyFrom(self.expiry._to_protobuf())
420+
self._set_bool(proto, "deleted", self.is_deleted, default=False)
421+
self._set_enum(proto, "defaultFreezeStatus", self.default_freeze_status)
422+
self._set_enum(proto, "defaultKycStatus", self.default_kyc_status)
423+
self._set_enum(proto, "pause_status", self.pause_status)
382424

383425
return proto
384426

tests/unit/test_token_create_transaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from hiero_sdk_python.exceptions import PrecheckError
3535
from hiero_sdk_python.crypto.private_key import PrivateKey
3636
from hiero_sdk_python.hapi.services import basic_types_pb2
37+
from hiero_sdk_python.crypto.public_key import PublicKey
3738

3839
pytestmark = pytest.mark.unit
3940

0 commit comments

Comments
 (0)