Skip to content

Commit 74a947a

Browse files
authored
feat: add freeze_with method to TopicCreateTransaction for auto-renew account handling (#2307)
Signed-off-by: Ntege Daniel <danientege785@gmail.com>
1 parent 33ea368 commit 74a947a

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

src/hiero_sdk_python/consensus/topic_create_transaction.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from __future__ import annotations
1111

12+
from typing import TYPE_CHECKING
13+
1214
from hiero_sdk_python.account.account_id import AccountId
1315
from hiero_sdk_python.channels import _Channel
1416
from hiero_sdk_python.crypto.key import Key
@@ -23,6 +25,10 @@
2325
from hiero_sdk_python.utils.key_utils import key_to_proto
2426

2527

28+
if TYPE_CHECKING:
29+
from hiero_sdk_python.client.client import Client
30+
31+
2632
class TopicCreateTransaction(Transaction):
2733
"""
2834
Represents a transaction to create a new topic in the Hedera
@@ -187,6 +193,27 @@ def set_fee_exempt_keys(self, keys: list[Key]) -> TopicCreateTransaction:
187193
self.fee_exempt_keys = keys
188194
return self
189195

196+
def freeze_with(self, client: Client) -> TopicCreateTransaction:
197+
"""
198+
Freeze the transaction with the given client.
199+
200+
If `auto_renew_account` was not explicitly set, automatically assigns it:
201+
- If `transaction_id` is already set, use its `account_id`.
202+
- Otherwise, fall back to the client's `operator_account_id`.
203+
204+
Args:
205+
client (Client): The client instance to use for setting defaults.
206+
207+
Returns:
208+
TopicCreateTransaction: The current transaction instance for method chaining.
209+
"""
210+
if client is not None and client.operator_account_id is not None and self.auto_renew_account is None:
211+
self.auto_renew_account = (
212+
self.transaction_id.account_id if self.transaction_id is not None else client.operator_account_id
213+
)
214+
215+
return super().freeze_with(client)
216+
190217
def _to_proto_key(self, key: Key | None):
191218
"""
192219
Backwards-compatible wrapper around `key_to_proto` for converting SDK keys

tck/handlers/topic.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ def create_topic(params: CreateTopicParams) -> CreateTopicResponse:
7373

7474
transaction = _build_create_topic_transaction(params)
7575

76-
if params.autoRenewAccountId is None and client is not None and client.operator_account_id is not None:
77-
transaction.set_auto_renew_account(client.operator_account_id)
78-
7976
if params.commonTransactionParams is not None:
8077
params.commonTransactionParams.apply_common_params(transaction, client)
8178

tests/unit/topic_create_transaction_test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from hiero_sdk_python.response_code import ResponseCode
2424
from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
2525
from hiero_sdk_python.tokens.token_id import TokenId
26+
from hiero_sdk_python.transaction.transaction_id import TransactionId
2627
from tests.unit.mock_server import mock_hedera_servers
2728

2829

@@ -574,3 +575,55 @@ def test_mixed_key_types_in_constructor(mock_account_ids):
574575
assert transaction_body.consensusCreateTopic.submitKey.ed25519 == ed25519_public.to_bytes_raw()
575576
assert transaction_body.consensusCreateTopic.fee_schedule_key.HasField("ECDSA_secp256k1")
576577
assert len(transaction_body.consensusCreateTopic.fee_exempt_key_list) == 2
578+
579+
580+
def test_freeze_with_defaults_auto_renew_account_to_operator(mock_client):
581+
"""Test that freeze_with sets auto_renew_account to operator when not explicitly set."""
582+
tx = TopicCreateTransaction(memo="test topic")
583+
frozen_tx = tx.freeze_with(mock_client)
584+
body = frozen_tx.build_transaction_body()
585+
586+
assert body.consensusCreateTopic.autoRenewAccount == mock_client.operator_account_id._to_proto()
587+
588+
589+
def test_freeze_with_preserves_explicit_auto_renew_account(mock_client):
590+
"""Test that freeze_with does not override an explicitly set auto_renew_account."""
591+
explicit_account = AccountId(0, 0, 9999)
592+
tx = TopicCreateTransaction(memo="test topic", auto_renew_account=explicit_account)
593+
frozen_tx = tx.freeze_with(mock_client)
594+
body = frozen_tx.build_transaction_body()
595+
596+
assert body.consensusCreateTopic.autoRenewAccount == explicit_account._to_proto()
597+
598+
599+
def test_freeze_with_uses_transaction_id_account_when_set(mock_client):
600+
"""Test that freeze_with uses transaction_id.account_id when transaction_id is set."""
601+
tx_account = AccountId(0, 0, 5555)
602+
tx = TopicCreateTransaction(memo="test topic")
603+
tx.transaction_id = TransactionId.generate(tx_account)
604+
frozen_tx = tx.freeze_with(mock_client)
605+
body = frozen_tx.build_transaction_body()
606+
607+
assert body.consensusCreateTopic.autoRenewAccount == tx_account._to_proto()
608+
609+
610+
def test_freeze_with_auto_renew_account_set_via_setter(mock_client):
611+
"""Test that freeze_with preserves auto_renew_account set via set_auto_renew_account."""
612+
explicit_account = AccountId(0, 0, 7777)
613+
tx = TopicCreateTransaction(memo="test topic")
614+
tx.set_auto_renew_account(explicit_account)
615+
frozen_tx = tx.freeze_with(mock_client)
616+
body = frozen_tx.build_transaction_body()
617+
618+
assert body.consensusCreateTopic.autoRenewAccount == explicit_account._to_proto()
619+
620+
621+
def test_freeze_with_leaves_auto_renew_account_unset_without_operator(mock_client):
622+
"""Test that freeze_with leaves auto_renew_account unset when operator_account_id is None."""
623+
mock_client.operator_account_id = None
624+
tx = TopicCreateTransaction(memo="test topic")
625+
tx.transaction_id = TransactionId.generate(AccountId(0, 0, 1234))
626+
frozen_tx = tx.freeze_with(mock_client)
627+
body = frozen_tx.build_transaction_body()
628+
629+
assert not body.consensusCreateTopic.HasField("autoRenewAccount")

0 commit comments

Comments
 (0)