Skip to content

Commit 969a859

Browse files
feat: add add_custom_fee to TopicUpdateTransaction (hiero-ledger#2566)
Signed-off-by: achintya2k5 <achintyasin@gmail.com> Signed-off-by: Achintya Sinha <achintyasin@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent c86aeb7 commit 969a859

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/hiero_sdk_python/consensus/topic_update_transaction.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,27 @@ def set_fee_exempt_keys(self, keys: list[Key]) -> TopicUpdateTransaction:
213213
self.fee_exempt_keys = keys
214214
return self
215215

216+
def add_custom_fee(self, custom_fee: CustomFixedFee) -> TopicUpdateTransaction:
217+
"""
218+
Adds a single custom fixed fee to the transaction's custom fee list.
219+
220+
Args:
221+
custom_fee (CustomFixedFee): The custom fixed fee to add.
222+
223+
Returns:
224+
TopicUpdateTransaction: The current instance for method chaining.
225+
"""
226+
227+
self._require_not_frozen()
228+
229+
if not isinstance(custom_fee, CustomFixedFee):
230+
raise TypeError("custom_fee must be a CustomFixedFee")
231+
232+
if self.custom_fees is None:
233+
self.custom_fees = []
234+
self.custom_fees.append(custom_fee)
235+
return self
236+
216237
def clear_custom_fees(self) -> TopicUpdateTransaction:
217238
"""
218239
Clears the custom fees for the topic update transaction and

tests/unit/topic_update_transaction_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,48 @@ def test_auto_renew_period_omitted_when_unset(
441441
body = tx.build_transaction_body().consensusUpdateTopic
442442

443443
assert not body.HasField("autoRenewPeriod")
444+
445+
446+
def test_add_custom_fee():
447+
"""Test adding a custom fee to the transaction."""
448+
tx = TopicUpdateTransaction()
449+
fee1 = CustomFixedFee(100, fee_collector_account_id=AccountId(0, 0, 123))
450+
fee2 = CustomFixedFee(200, fee_collector_account_id=AccountId(0, 0, 456))
451+
452+
result = tx.add_custom_fee(fee1)
453+
454+
assert len(tx.custom_fees) == 1
455+
assert tx.custom_fees[0] == fee1
456+
assert result is tx
457+
458+
tx.add_custom_fee(fee2)
459+
460+
assert len(tx.custom_fees) == 2
461+
assert tx.custom_fees[0] == fee1
462+
assert tx.custom_fees[1] == fee2
463+
464+
465+
def test_add_custom_fee_frozen(mock_client, topic_id):
466+
"""Test calling add_custom_fee() after freezing raises an exception."""
467+
tx = TopicUpdateTransaction()
468+
469+
tx.set_topic_id(topic_id)
470+
tx.freeze_with(mock_client)
471+
472+
fee = CustomFixedFee(100, fee_collector_account_id=AccountId(0, 0, 123))
473+
474+
with pytest.raises(Exception, match="Transaction is immutable; it has been frozen"):
475+
tx.add_custom_fee(fee)
476+
477+
478+
def test_add_custom_fee_type_error():
479+
"""Test passing None or a non-CustomFixedFee argument raises TypeError."""
480+
tx = TopicUpdateTransaction()
481+
482+
with pytest.raises(TypeError, match="custom_fee must be a CustomFixedFee"):
483+
tx.add_custom_fee("this_is_a_string") # type: ignore
484+
assert tx.custom_fees is None, "Invalid input must not change custom_fees"
485+
486+
with pytest.raises(TypeError, match="custom_fee must be a CustomFixedFee"):
487+
tx.add_custom_fee(None) # type: ignore
488+
assert tx.custom_fees is None, "Invalid input must not change custom_fees"

0 commit comments

Comments
 (0)