Skip to content

Commit d600576

Browse files
committed
feat(token): update response models to support nullable fields and improve serialization
Signed-off-by: Ntege Daniel <danientege785@gmail.com>
1 parent aef821d commit d600576

3 files changed

Lines changed: 48 additions & 34 deletions

File tree

tck/handlers/registry.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,15 @@ def safe_dispatch(method_name: str, params: Any, request_id: str | int | None) -
8282

8383

8484
def parse_result(result: Any) -> dict:
85-
"""Parse the result from the methods to dict containing non none key:values"""
86-
return {k: v for k, v in asdict(result).items() if v is not None}
85+
"""Parse the result from the methods to dict containing non none key:values.
86+
87+
Fields with metadata={"nullable": True} are preserved even when None.
88+
"""
89+
from dataclasses import fields as dc_fields
90+
91+
nullable_fields: set[str] = set()
92+
for f in dc_fields(result):
93+
if f.metadata.get("nullable"):
94+
nullable_fields.add(f.name)
95+
96+
return {k: v for k, v in asdict(result).items() if v is not None or k in nullable_fields}

tck/handlers/token.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -493,20 +493,20 @@ def _serialize_key(key) -> str:
493493
"""Serialize a key to its DER-encoded hex string representation."""
494494
if key is None:
495495
return ""
496-
497-
to_string_der = getattr(key, "to_string_der", None)
498-
if callable(to_string_der):
499-
return to_string_der()
500-
501-
return key.to_bytes().hex()
496+
return key.to_string_der()
502497

503498

504499
def _serialize_custom_fee(fee: CustomFee) -> CustomFeeResponse:
505500
"""Serialize a CustomFee to a CustomFeeResponse."""
506501
response = CustomFeeResponse()
507502

508503
if fee.fee_collector_account_id is not None:
509-
response.feeCollectorAccountId = str(fee.fee_collector_account_id)
504+
aid = fee.fee_collector_account_id
505+
response.feeCollectorAccountId = {
506+
"realm": str(aid.realm),
507+
"shard": str(aid.shard),
508+
"num": str(aid.num),
509+
}
510510
response.feeCollectorsExempt = fee.all_collectors_are_exempt
511511

512512
if isinstance(fee, CustomFixedFee):
@@ -539,11 +539,11 @@ def _serialize_custom_fee(fee: CustomFee) -> CustomFeeResponse:
539539

540540
def _map_pause_status(pause_status: TokenPauseStatus) -> bool | None:
541541
"""Map TokenPauseStatus enum to TCK boolean representation."""
542-
mapping = {
543-
TokenPauseStatus.PAUSED: True,
544-
TokenPauseStatus.UNPAUSED: False,
545-
}
546-
return mapping.get(pause_status)
542+
if pause_status == TokenPauseStatus.PAUSED:
543+
return True
544+
if pause_status == TokenPauseStatus.UNPAUSED:
545+
return False
546+
return None
547547

548548

549549
def _map_token_type(token_type: TokenType | None) -> str | None:
@@ -568,22 +568,26 @@ def _map_supply_type(supply_type: SupplyType | None) -> str | None:
568568
return mapping.get(supply_type)
569569

570570

571+
def _map_freeze_status(freeze_status: TokenFreezeStatus) -> bool | None:
572+
"""Map TokenFreezeStatus enum to TCK boolean representation."""
573+
if freeze_status == TokenFreezeStatus.FROZEN:
574+
return True
575+
if freeze_status == TokenFreezeStatus.UNFROZEN:
576+
return False
577+
return None
578+
579+
580+
def _map_kyc_status(kyc_status: TokenKycStatus) -> bool | None:
581+
"""Map TokenKycStatus enum to TCK boolean representation."""
582+
if kyc_status == TokenKycStatus.GRANTED:
583+
return True
584+
if kyc_status == TokenKycStatus.REVOKED:
585+
return False
586+
return None
587+
588+
571589
def _build_token_info_response(info: TokenInfo) -> GetTokenInfoResponse:
572590
"""Build a GetTokenInfoResponse from a TokenInfo object."""
573-
# Map default freeze status to boolean
574-
default_freeze: bool | None = None
575-
if info.default_freeze_status == TokenFreezeStatus.FROZEN:
576-
default_freeze = True
577-
elif info.default_freeze_status == TokenFreezeStatus.UNFROZEN:
578-
default_freeze = False
579-
580-
# Map default KYC status to boolean
581-
default_kyc: bool | None = None
582-
if info.default_kyc_status == TokenKycStatus.GRANTED:
583-
default_kyc = True
584-
elif info.default_kyc_status == TokenKycStatus.REVOKED:
585-
default_kyc = False
586-
587591
# Serialize custom fees
588592
custom_fees = [_serialize_custom_fee(fee) for fee in info.custom_fees] if info.custom_fees else []
589593

@@ -602,8 +606,8 @@ def _build_token_info_response(info: TokenInfo) -> GetTokenInfoResponse:
602606
supplyKey=_serialize_key(info.supply_key),
603607
feeScheduleKey=_serialize_key(info.fee_schedule_key),
604608
metadataKey=_serialize_key(info.metadata_key),
605-
defaultFreezeStatus=default_freeze,
606-
defaultKycStatus=default_kyc,
609+
defaultFreezeStatus=_map_freeze_status(info.default_freeze_status),
610+
defaultKycStatus=_map_kyc_status(info.default_kyc_status),
607611
pauseStatus=_map_pause_status(info.pause_status),
608612
isDeleted=info.is_deleted,
609613
autoRenewAccountId=str(info.auto_renew_account) if info.auto_renew_account else None,

tck/response/token.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class CustomFeeResponse:
6161
fixedFee: dict | None = None
6262
fractionalFee: dict | None = None
6363
royaltyFee: dict | None = None
64-
feeCollectorAccountId: str | None = None
64+
feeCollectorAccountId: dict | None = None
6565
feeCollectorsExempt: bool | None = None
6666

6767

@@ -83,9 +83,9 @@ class GetTokenInfoResponse:
8383
supplyKey: str | None = None
8484
feeScheduleKey: str | None = None
8585
metadataKey: str | None = None
86-
defaultFreezeStatus: bool | None = None
87-
defaultKycStatus: bool | None = None
88-
pauseStatus: bool | None = None
86+
defaultFreezeStatus: bool | None = field(metadata={"nullable": True}, default=None)
87+
defaultKycStatus: bool | None = field(metadata={"nullable": True}, default=None)
88+
pauseStatus: bool | None = field(metadata={"nullable": True}, default=None)
8989
isDeleted: bool | None = None
9090
autoRenewAccountId: str | None = None
9191
autoRenewPeriod: str | None = None

0 commit comments

Comments
 (0)