|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +from tck.param.base import BaseTransactionParams |
| 6 | +from tck.util.param_utils import ( |
| 7 | + non_empty_string_list, |
| 8 | + non_empty_string_or_none, |
| 9 | + parse_common_transaction_params, |
| 10 | + parse_session_id, |
| 11 | + to_bool, |
| 12 | + to_int, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class CreateTopicFixedFeeParams: |
| 18 | + """Parameters for a fixed fee custom fee in topic creation.""" |
| 19 | + |
| 20 | + amount: int | None = None |
| 21 | + denominatingTokenId: str | None = None |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def parse_json_params(cls, params: dict) -> CreateTopicFixedFeeParams: |
| 25 | + return cls( |
| 26 | + amount=to_int(params.get("amount")), |
| 27 | + denominatingTokenId=non_empty_string_or_none(params.get("denominatingTokenId")), |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class CreateTopicCustomFeeParams: |
| 33 | + """Parameters for a custom fee in topic creation.""" |
| 34 | + |
| 35 | + feeCollectorAccountId: str | None = None |
| 36 | + feeCollectorsExempt: bool | None = None |
| 37 | + fixedFee: CreateTopicFixedFeeParams | None = None |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def parse_json_params(cls, params: dict) -> CreateTopicCustomFeeParams: |
| 41 | + fixed_fee = params.get("fixedFee") |
| 42 | + |
| 43 | + fee_collector_account_id = params.get("feeCollectorAccountId") |
| 44 | + if isinstance(fee_collector_account_id, str): |
| 45 | + fee_collector_account_id = fee_collector_account_id.strip() |
| 46 | + |
| 47 | + return cls( |
| 48 | + feeCollectorAccountId=fee_collector_account_id, |
| 49 | + feeCollectorsExempt=to_bool(params.get("feeCollectorsExempt")), |
| 50 | + fixedFee=(CreateTopicFixedFeeParams.parse_json_params(fixed_fee) if isinstance(fixed_fee, dict) else None), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@dataclass |
| 55 | +class CreateTopicParams(BaseTransactionParams): |
| 56 | + """Parameters for creating a topic. Extends BaseTransactionParams to include common transaction parameters.""" |
| 57 | + |
| 58 | + memo: str | None = None |
| 59 | + adminKey: str | None = None |
| 60 | + submitKey: str | None = None |
| 61 | + autoRenewPeriod: int | None = None |
| 62 | + autoRenewAccountId: str | None = None |
| 63 | + feeScheduleKey: str | None = None |
| 64 | + feeExemptKeys: list[str] | None = None |
| 65 | + customFees: list[CreateTopicCustomFeeParams] | None = None |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def parse_json_params(cls, params: dict) -> CreateTopicParams: |
| 69 | + fee_exempt_keys = params.get("feeExemptKeys") |
| 70 | + if fee_exempt_keys is not None and not isinstance(fee_exempt_keys, list): |
| 71 | + raise ValueError("feeExemptKeys must be a list") |
| 72 | + |
| 73 | + custom_fees = params.get("customFees") |
| 74 | + if custom_fees is not None and not isinstance(custom_fees, list): |
| 75 | + raise ValueError("customFees must be a list") |
| 76 | + if custom_fees is not None and any(not isinstance(custom_fee, dict) for custom_fee in custom_fees): |
| 77 | + raise ValueError("each customFees item must be an object") |
| 78 | + return cls( |
| 79 | + memo=params.get("memo"), |
| 80 | + adminKey=non_empty_string_or_none(params.get("adminKey")), |
| 81 | + submitKey=non_empty_string_or_none(params.get("submitKey")), |
| 82 | + autoRenewPeriod=to_int(params.get("autoRenewPeriod")), |
| 83 | + autoRenewAccountId=non_empty_string_or_none(params.get("autoRenewAccountId")), |
| 84 | + feeScheduleKey=non_empty_string_or_none(params.get("feeScheduleKey")), |
| 85 | + feeExemptKeys=non_empty_string_list(fee_exempt_keys), |
| 86 | + customFees=( |
| 87 | + [CreateTopicCustomFeeParams.parse_json_params(custom_fee) for custom_fee in custom_fees] |
| 88 | + if custom_fees is not None |
| 89 | + else None |
| 90 | + ), |
| 91 | + sessionId=parse_session_id(params), |
| 92 | + commonTransactionParams=parse_common_transaction_params(params), |
| 93 | + ) |
0 commit comments