forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic_update_transaction.py
More file actions
314 lines (260 loc) · 11.3 KB
/
Copy pathtopic_update_transaction.py
File metadata and controls
314 lines (260 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
This module provides the `TopicUpdateTransaction` class for updating consensus topics
on the Hedera network using the Hiero SDK.
"""
from __future__ import annotations
from google.protobuf import wrappers_pb2 as _wrappers_pb2
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.channels import _Channel
from hiero_sdk_python.consensus.topic_id import TopicId
from hiero_sdk_python.crypto.key import Key
from hiero_sdk_python.Duration import Duration
from hiero_sdk_python.executable import _Method
from hiero_sdk_python.hapi.services import consensus_update_topic_pb2, duration_pb2, timestamp_pb2, transaction_pb2
from hiero_sdk_python.hapi.services.custom_fees_pb2 import FeeExemptKeyList, FixedCustomFeeList
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
SchedulableTransactionBody,
)
from hiero_sdk_python.timestamp import Timestamp
from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
from hiero_sdk_python.transaction.transaction import Transaction
from hiero_sdk_python.utils.key_utils import key_to_proto
class TopicUpdateTransaction(Transaction):
"""Represents a transaction to update a consensus topic."""
def __init__(
self,
topic_id: TopicId | None = None,
memo: str | None = None,
admin_key: Key | None = None,
submit_key: Key | None = None,
auto_renew_period: Duration | None = Duration(7890000),
auto_renew_account: AccountId | None = None,
expiration_time: Timestamp | None = None,
custom_fees: list[CustomFixedFee] | None = None,
fee_schedule_key: Key | None = None,
fee_exempt_keys: list[Key] | None = None,
) -> None:
"""
Initializes a new instance of the TopicUpdateTransaction class.
Args:
topic_id (TopicId): The ID of the topic to update.
memo (str): The memo associated with the topic.
admin_key (Key): The admin key for the topic.
submit_key (Key): The submit key for the topic.
auto_renew_period (Duration): The auto-renew period for the topic.
auto_renew_account (AccountId): The account ID for auto-renewal.
expiration_time (Timestamp): The expiration time of the topic.
custom_fees (list[CustomFixedFee]): A list of custom fees to set for the topic.
fee_schedule_key (Key): The fee schedule key for the topic.
fee_exempt_keys (list[Key]): A list of fee exempt keys for the topic
"""
super().__init__()
self.topic_id: TopicId | None = topic_id
self.memo: str = memo or ""
self.admin_key: Key | None = admin_key
self.submit_key: Key | None = submit_key
self.auto_renew_period: Duration | None = auto_renew_period
self.auto_renew_account: AccountId | None = auto_renew_account
self.expiration_time: Timestamp | None = expiration_time
self.transaction_fee: int = 10_000_000
self.custom_fees: list[CustomFixedFee] | None = custom_fees
self.fee_schedule_key: Key | None = fee_schedule_key
self.fee_exempt_keys: list[Key] | None = fee_exempt_keys
def set_topic_id(self, topic_id: TopicId) -> TopicUpdateTransaction:
"""
Sets the topic ID for the transaction.
Args:
topic_id: The topic ID to update.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
self.topic_id = topic_id
return self
def set_memo(self, memo: str) -> TopicUpdateTransaction:
"""
Sets the memo for the topic.
Args:
memo: The memo to set.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
self.memo = memo
return self
def set_admin_key(self, key: Key) -> TopicUpdateTransaction:
"""
Sets the admin key for the topic.
Args:
key: The admin key to set.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
self.admin_key = key
return self
def set_submit_key(self, key: Key) -> TopicUpdateTransaction:
"""
Sets the submit key for the topic.
Args:
key: The submit key to set.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
self.submit_key = key
return self
def set_auto_renew_period(self, seconds: Duration | int) -> TopicUpdateTransaction:
"""
Sets the auto-renew period for the topic.
Args:
seconds: The auto-renew period in seconds.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
if isinstance(seconds, int):
self.auto_renew_period = Duration(seconds)
elif isinstance(seconds, Duration):
self.auto_renew_period = seconds
else:
raise TypeError("Duration of invalid type")
return self
def set_auto_renew_account(self, account_id: AccountId) -> TopicUpdateTransaction:
"""
Sets the auto-renew account for the topic.
Args:
account_id: The account ID to set as the auto-renew account.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
self.auto_renew_account = account_id
return self
def set_expiration_time(self, expiration_time: timestamp_pb2.Timestamp) -> TopicUpdateTransaction:
"""
Sets the expiration time for the topic.
Args:
expiration_time: The expiration time to set.
Returns:
TopicUpdateTransaction: Returns the instance for method chaining.
"""
self._require_not_frozen()
self.expiration_time = expiration_time
return self
def set_custom_fees(self, custom_fees: list[CustomFixedFee]) -> TopicUpdateTransaction:
"""
Sets the custom fees for the topic update transaction.
Args:
custom_fees (list[CustomFixedFee]): The custom fees to set for the topic.
Returns:
TopicUpdateTransaction: The current instance for method chaining.
"""
self._require_not_frozen()
self.custom_fees = custom_fees
return self
def set_fee_schedule_key(self, key: Key) -> TopicUpdateTransaction:
"""
Sets the fee schedule key for the topic update transaction.
Args:
key (Key): The fee schedule key to set for the topic.
Returns:
TopicUpdateTransaction: The current instance for method chaining.
"""
self._require_not_frozen()
self.fee_schedule_key = key
return self
def set_fee_exempt_keys(self, keys: list[Key]) -> TopicUpdateTransaction:
"""
Sets the fee exempt keys for the topic update transaction.
Args:
keys (list[Key]): The fee exempt keys to set for the topic.
Returns:
TopicUpdateTransaction: The current instance for method chaining.
"""
self._require_not_frozen()
self.fee_exempt_keys = keys
return self
def clear_custom_fees(self) -> TopicUpdateTransaction:
"""
Clears the custom fees for the topic update transaction and
removes them from the network state.
Returns:
TopicUpdateTransaction: The current instance for method chaining.
"""
self._require_not_frozen()
self.custom_fees = []
return self
def clear_fee_exempt_keys(self) -> TopicUpdateTransaction:
"""
Clears the fee exempt keys for the topic update transaction and
removes them from the network state.
Returns:
TopicUpdateTransaction: The current instance for method chaining.
"""
self._require_not_frozen()
self.fee_exempt_keys = []
return self
def _build_proto_body(self) -> consensus_update_topic_pb2.ConsensusUpdateTopicTransactionBody:
"""
Returns the protobuf body for the topic update transaction.
Returns:
ConsensusUpdateTopicTransactionBody: The protobuf body for this transaction.
Raises:
ValueError: If required fields are missing.
"""
if self.topic_id is None:
raise ValueError("Missing required fields: topic_id")
custom_fees = (
FixedCustomFeeList(fees=[custom_fee._to_topic_fee_proto() for custom_fee in self.custom_fees])
if self.custom_fees is not None
else None
)
fee_exempt_key_list = (
FeeExemptKeyList(keys=[key_to_proto(key) for key in self.fee_exempt_keys])
if self.fee_exempt_keys is not None
else None
)
return consensus_update_topic_pb2.ConsensusUpdateTopicTransactionBody(
topicID=self.topic_id._to_proto(),
adminKey=key_to_proto(self.admin_key) if self.admin_key else None,
submitKey=key_to_proto(self.submit_key) if self.submit_key else None,
autoRenewPeriod=(
duration_pb2.Duration(seconds=self.auto_renew_period.seconds) if self.auto_renew_period else None
),
autoRenewAccount=(self.auto_renew_account._to_proto() if self.auto_renew_account else None),
expirationTime=self.expiration_time._to_protobuf() if self.expiration_time else None,
memo=_wrappers_pb2.StringValue(value=self.memo) if self.memo is not None else None,
custom_fees=custom_fees,
fee_schedule_key=key_to_proto(self.fee_schedule_key) if self.fee_schedule_key else None,
fee_exempt_key_list=fee_exempt_key_list,
)
def build_transaction_body(self) -> transaction_pb2.TransactionBody:
"""
Builds and returns the protobuf transaction body for topic update.
Returns:
TransactionBody: The protobuf transaction body containing the topic update details.
"""
consensus_update_body = self._build_proto_body()
transaction_body = self.build_base_transaction_body()
transaction_body.consensusUpdateTopic.CopyFrom(consensus_update_body)
return transaction_body
def build_scheduled_body(self) -> SchedulableTransactionBody:
"""
Builds the scheduled transaction body for this topic update transaction.
Returns:
SchedulableTransactionBody: The built scheduled transaction body.
"""
consensus_update_body = self._build_proto_body()
schedulable_body = self.build_base_scheduled_body()
schedulable_body.consensusUpdateTopic.CopyFrom(consensus_update_body)
return schedulable_body
def _get_method(self, channel: _Channel) -> _Method:
"""
Returns the method for executing the topic update transaction.
Args:
channel (_Channel): The channel to use for the transaction.
Returns:
_Method: The method to execute the transaction.
"""
return _Method(transaction_func=channel.topic.updateTopic, query_func=None)