1
- from typing import Optional
1
+ # pylint: disable=too-many-instance-attributes
2
+ """
3
+ ContractCreateTransaction class.
4
+ """
5
+
2
6
from dataclasses import dataclass
3
- from hiero_sdk_python .contract .contract_function_parameters import (
4
- ContractFunctionParameters
5
- )
6
- from hiero_sdk_python .crypto .private_key import PrivateKey
7
- from hiero_sdk_python .hbar import Hbar
8
- from hiero_sdk_python .file .file_id import FileId
7
+ from typing import Optional
8
+
9
9
from hiero_sdk_python .account .account_id import AccountId
10
- from hiero_sdk_python .transaction .transaction import Transaction
11
10
from hiero_sdk_python .channels import _Channel
11
+ from hiero_sdk_python .contract .contract_function_parameters import (
12
+ ContractFunctionParameters ,
13
+ )
14
+ from hiero_sdk_python .crypto .public_key import PublicKey
15
+ from hiero_sdk_python .Duration import Duration
12
16
from hiero_sdk_python .executable import _Method
17
+ from hiero_sdk_python .file .file_id import FileId
13
18
from hiero_sdk_python .hapi .services .contract_create_pb2 import (
14
- ContractCreateTransactionBody
19
+ ContractCreateTransactionBody ,
15
20
)
16
- from hiero_sdk_python .Duration import Duration
21
+ from hiero_sdk_python .hbar import Hbar
22
+ from hiero_sdk_python .transaction .transaction import Transaction
17
23
18
24
DEFAULT_AUTO_RENEW_PERIOD = 90 * 24 * 60 * 60 # 90 days in seconds
19
25
@@ -43,9 +49,10 @@ class ContractCreateParams:
43
49
staked_node_id (optional): The node ID to stake to.
44
50
decline_reward (optional): Whether to decline staking rewards.
45
51
"""
52
+
46
53
bytecode_file_id : Optional [FileId ] = None
47
54
proxy_account_id : Optional [AccountId ] = None
48
- admin_key : Optional [PrivateKey ] = None
55
+ admin_key : Optional [PublicKey ] = None
49
56
gas : Optional [int ] = None
50
57
initial_balance : Optional [int ] = None
51
58
auto_renew_period : Duration = Duration (DEFAULT_AUTO_RENEW_PERIOD )
@@ -58,6 +65,7 @@ class ContractCreateParams:
58
65
staked_node_id : Optional [int ] = None
59
66
decline_reward : Optional [bool ] = None
60
67
68
+
61
69
class ContractCreateTransaction (Transaction ):
62
70
"""
63
71
A transaction that creates a new smart contract.
@@ -71,10 +79,7 @@ class ContractCreateTransaction(Transaction):
71
79
contract creation.
72
80
"""
73
81
74
- def __init__ (
75
- self ,
76
- contract_params : Optional [ContractCreateParams ] = None
77
- ):
82
+ def __init__ (self , contract_params : Optional [ContractCreateParams ] = None ):
78
83
"""
79
84
Initializes a new ContractCreateTransaction instance.
80
85
@@ -87,16 +92,14 @@ def __init__(
87
92
params = contract_params or ContractCreateParams ()
88
93
self .bytecode_file_id : Optional [FileId ] = params .bytecode_file_id
89
94
self .proxy_account_id : Optional [AccountId ] = params .proxy_account_id
90
- self .admin_key : Optional [PrivateKey ] = params .admin_key
95
+ self .admin_key : Optional [PublicKey ] = params .admin_key
91
96
self .gas : Optional [int ] = params .gas
92
97
self .initial_balance : Optional [int ] = params .initial_balance
93
98
self .auto_renew_period : Duration = params .auto_renew_period
94
99
self .parameters : Optional [bytes ] = params .parameters
95
100
self .contract_memo : Optional [str ] = params .contract_memo
96
101
self .bytecode : Optional [bytes ] = params .bytecode
97
- self .auto_renew_account_id : Optional [AccountId ] = (
98
- params .auto_renew_account_id
99
- )
102
+ self .auto_renew_account_id : Optional [AccountId ] = params .auto_renew_account_id
100
103
self .max_automatic_token_associations : Optional [int ] = (
101
104
params .max_automatic_token_associations
102
105
)
@@ -107,9 +110,8 @@ def __init__(
107
110
self ._default_transaction_fee = Hbar (20 ).to_tinybars ()
108
111
109
112
def set_bytecode_file_id (
110
- self ,
111
- bytecode_file_id : Optional [FileId ]
112
- ) -> 'ContractCreateTransaction' :
113
+ self , bytecode_file_id : Optional [FileId ]
114
+ ) -> "ContractCreateTransaction" :
113
115
"""
114
116
Sets the FileID of the file containing the contract bytecode.
115
117
@@ -124,10 +126,7 @@ def set_bytecode_file_id(
124
126
self .bytecode_file_id = bytecode_file_id
125
127
return self
126
128
127
- def set_bytecode (
128
- self ,
129
- code : Optional [bytes ]
130
- ) -> 'ContractCreateTransaction' :
129
+ def set_bytecode (self , code : Optional [bytes ]) -> "ContractCreateTransaction" :
131
130
"""
132
131
Sets the bytecode for the contract.
133
132
@@ -146,9 +145,8 @@ def set_bytecode(
146
145
return self
147
146
148
147
def set_proxy_account_id (
149
- self ,
150
- proxy_account_id : Optional [AccountId ]
151
- ) -> 'ContractCreateTransaction' :
148
+ self , proxy_account_id : Optional [AccountId ]
149
+ ) -> "ContractCreateTransaction" :
152
150
"""
153
151
Sets the proxy account ID for the contract.
154
152
@@ -163,14 +161,13 @@ def set_proxy_account_id(
163
161
return self
164
162
165
163
def set_admin_key (
166
- self ,
167
- admin_key : Optional [PrivateKey ]
168
- ) -> 'ContractCreateTransaction' :
164
+ self , admin_key : Optional [PublicKey ]
165
+ ) -> "ContractCreateTransaction" :
169
166
"""
170
167
Sets the admin key for the contract.
171
168
172
169
Args:
173
- admin_key (Optional[PrivateKey ]): The admin key.
170
+ admin_key (Optional[PublicKey ]): The admin key.
174
171
175
172
Returns:
176
173
ContractCreateTransaction: This transaction instance.
@@ -179,10 +176,7 @@ def set_admin_key(
179
176
self .admin_key = admin_key
180
177
return self
181
178
182
- def set_gas (
183
- self ,
184
- gas : Optional [int ]
185
- ) -> 'ContractCreateTransaction' :
179
+ def set_gas (self , gas : Optional [int ]) -> "ContractCreateTransaction" :
186
180
"""
187
181
Sets the gas limit for contract creation.
188
182
@@ -197,9 +191,8 @@ def set_gas(
197
191
return self
198
192
199
193
def set_initial_balance (
200
- self ,
201
- initial_balance : Optional [int ]
202
- ) -> 'ContractCreateTransaction' :
194
+ self , initial_balance : Optional [int ]
195
+ ) -> "ContractCreateTransaction" :
203
196
"""
204
197
Sets the initial balance for the contract in tinybars.
205
198
@@ -214,9 +207,8 @@ def set_initial_balance(
214
207
return self
215
208
216
209
def set_auto_renew_period (
217
- self ,
218
- auto_renew_period : Duration
219
- ) -> 'ContractCreateTransaction' :
210
+ self , auto_renew_period : Duration
211
+ ) -> "ContractCreateTransaction" :
220
212
"""
221
213
Sets the auto-renewal period for the contract.
222
214
@@ -231,9 +223,8 @@ def set_auto_renew_period(
231
223
return self
232
224
233
225
def set_constructor_parameters (
234
- self ,
235
- parameters : Optional [ContractFunctionParameters | bytes ]
236
- ) -> 'ContractCreateTransaction' :
226
+ self , parameters : Optional [ContractFunctionParameters | bytes ]
227
+ ) -> "ContractCreateTransaction" :
237
228
"""
238
229
Sets the constructor parameters for the contract.
239
230
@@ -252,9 +243,8 @@ def set_constructor_parameters(
252
243
return self
253
244
254
245
def set_contract_memo (
255
- self ,
256
- contract_memo : Optional [str ]
257
- ) -> 'ContractCreateTransaction' :
246
+ self , contract_memo : Optional [str ]
247
+ ) -> "ContractCreateTransaction" :
258
248
"""
259
249
Sets the contract_memo for the contract.
260
250
@@ -269,9 +259,8 @@ def set_contract_memo(
269
259
return self
270
260
271
261
def set_auto_renew_account_id (
272
- self ,
273
- auto_renew_account_id : Optional [AccountId ]
274
- ) -> 'ContractCreateTransaction' :
262
+ self , auto_renew_account_id : Optional [AccountId ]
263
+ ) -> "ContractCreateTransaction" :
275
264
"""
276
265
Sets the account ID that will pay for auto-renewal.
277
266
@@ -287,9 +276,8 @@ def set_auto_renew_account_id(
287
276
return self
288
277
289
278
def set_max_automatic_token_associations (
290
- self ,
291
- max_automatic_token_associations : Optional [int ]
292
- ) -> 'ContractCreateTransaction' :
279
+ self , max_automatic_token_associations : Optional [int ]
280
+ ) -> "ContractCreateTransaction" :
293
281
"""
294
282
Sets the maximum number of automatic token associations.
295
283
@@ -305,9 +293,8 @@ def set_max_automatic_token_associations(
305
293
return self
306
294
307
295
def set_staked_account_id (
308
- self ,
309
- staked_account_id : Optional [AccountId ]
310
- ) -> 'ContractCreateTransaction' :
296
+ self , staked_account_id : Optional [AccountId ]
297
+ ) -> "ContractCreateTransaction" :
311
298
"""
312
299
Sets the account ID to stake to.
313
300
@@ -322,9 +309,8 @@ def set_staked_account_id(
322
309
return self
323
310
324
311
def set_staked_node_id (
325
- self ,
326
- staked_node_id : Optional [int ]
327
- ) -> 'ContractCreateTransaction' :
312
+ self , staked_node_id : Optional [int ]
313
+ ) -> "ContractCreateTransaction" :
328
314
"""
329
315
Sets the node ID to stake to.
330
316
@@ -339,9 +325,8 @@ def set_staked_node_id(
339
325
return self
340
326
341
327
def set_decline_reward (
342
- self ,
343
- decline_reward : Optional [bool ]
344
- ) -> 'ContractCreateTransaction' :
328
+ self , decline_reward : Optional [bool ]
329
+ ) -> "ContractCreateTransaction" :
345
330
"""
346
331
Sets whether to decline staking rewards.
347
332
@@ -356,6 +341,16 @@ def set_decline_reward(
356
341
self .decline_reward = decline_reward
357
342
return self
358
343
344
+ def _validate_parameters (self ):
345
+ """
346
+ Validates the parameters for the contract creation transaction.
347
+ """
348
+ if self .bytecode_file_id is None and self .bytecode is None :
349
+ raise ValueError ("Either bytecode_file_id or bytecode must be provided" )
350
+
351
+ if self .gas is None :
352
+ raise ValueError ("Gas limit must be provided" )
353
+
359
354
def build_transaction_body (self ):
360
355
"""
361
356
Builds and returns the protobuf transaction body for contract creation.
@@ -367,25 +362,31 @@ def build_transaction_body(self):
367
362
Raises:
368
363
ValueError: If required fields are missing.
369
364
"""
370
- if self .bytecode_file_id is None and self .bytecode is None :
371
- raise ValueError ("Either bytecode_file_id or bytecode must be provided" )
372
-
373
- if self .gas is None :
374
- raise ValueError ("Gas limit must be provided" )
365
+ self ._validate_parameters ()
375
366
376
367
contract_create_body = ContractCreateTransactionBody (
377
368
gas = self .gas ,
378
369
initialBalance = self .initial_balance ,
379
370
constructorParameters = self .parameters ,
380
371
memo = self .contract_memo ,
381
372
max_automatic_token_associations = self .max_automatic_token_associations ,
382
- decline_reward = self .decline_reward if self .decline_reward is not None else False ,
383
- auto_renew_account_id = self .auto_renew_account_id ._to_proto () if self .auto_renew_account_id else None ,
384
- staked_account_id = self .staked_account_id ._to_proto () if self .staked_account_id else None ,
373
+ decline_reward = (
374
+ self .decline_reward if self .decline_reward is not None else False
375
+ ),
376
+ auto_renew_account_id = (
377
+ self .auto_renew_account_id ._to_proto ()
378
+ if self .auto_renew_account_id
379
+ else None
380
+ ),
381
+ staked_account_id = (
382
+ self .staked_account_id ._to_proto () if self .staked_account_id else None
383
+ ),
385
384
staked_node_id = self .staked_node_id ,
386
385
autoRenewPeriod = self .auto_renew_period ._to_proto (),
387
- proxyAccountID = self .proxy_account_id ._to_proto () if self .proxy_account_id else None ,
388
- adminKey = self .admin_key .public_key ()._to_proto () if self .admin_key else None ,
386
+ proxyAccountID = (
387
+ self .proxy_account_id ._to_proto () if self .proxy_account_id else None
388
+ ),
389
+ adminKey = (self .admin_key ._to_proto () if self .admin_key else None ),
389
390
fileID = self .bytecode_file_id ._to_proto () if self .bytecode_file_id else None ,
390
391
initcode = self .bytecode ,
391
392
)
@@ -407,6 +408,5 @@ def _get_method(self, channel: _Channel) -> _Method:
407
408
create contracts.
408
409
"""
409
410
return _Method (
410
- transaction_func = channel .smart_contract .createContract ,
411
- query_func = None
411
+ transaction_func = channel .smart_contract .createContract , query_func = None
412
412
)
0 commit comments