Skip to content

Commit 62d6399

Browse files
committed
chore: address PR feedback for contract create
Signed-off-by: dosi <dosi.kolev@limechain.tech>
1 parent f9c97e7 commit 62d6399

File tree

3 files changed

+259
-178
lines changed

3 files changed

+259
-178
lines changed

src/hiero_sdk_python/contract/contract_create_transaction.py

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
from typing import Optional
1+
# pylint: disable=too-many-instance-attributes
2+
"""
3+
ContractCreateTransaction class.
4+
"""
5+
26
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+
99
from hiero_sdk_python.account.account_id import AccountId
10-
from hiero_sdk_python.transaction.transaction import Transaction
1110
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
1216
from hiero_sdk_python.executable import _Method
17+
from hiero_sdk_python.file.file_id import FileId
1318
from hiero_sdk_python.hapi.services.contract_create_pb2 import (
14-
ContractCreateTransactionBody
19+
ContractCreateTransactionBody,
1520
)
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
1723

1824
DEFAULT_AUTO_RENEW_PERIOD = 90 * 24 * 60 * 60 # 90 days in seconds
1925

@@ -43,9 +49,10 @@ class ContractCreateParams:
4349
staked_node_id (optional): The node ID to stake to.
4450
decline_reward (optional): Whether to decline staking rewards.
4551
"""
52+
4653
bytecode_file_id: Optional[FileId] = None
4754
proxy_account_id: Optional[AccountId] = None
48-
admin_key: Optional[PrivateKey] = None
55+
admin_key: Optional[PublicKey] = None
4956
gas: Optional[int] = None
5057
initial_balance: Optional[int] = None
5158
auto_renew_period: Duration = Duration(DEFAULT_AUTO_RENEW_PERIOD)
@@ -58,6 +65,7 @@ class ContractCreateParams:
5865
staked_node_id: Optional[int] = None
5966
decline_reward: Optional[bool] = None
6067

68+
6169
class ContractCreateTransaction(Transaction):
6270
"""
6371
A transaction that creates a new smart contract.
@@ -71,10 +79,7 @@ class ContractCreateTransaction(Transaction):
7179
contract creation.
7280
"""
7381

74-
def __init__(
75-
self,
76-
contract_params: Optional[ContractCreateParams] = None
77-
):
82+
def __init__(self, contract_params: Optional[ContractCreateParams] = None):
7883
"""
7984
Initializes a new ContractCreateTransaction instance.
8085
@@ -87,16 +92,14 @@ def __init__(
8792
params = contract_params or ContractCreateParams()
8893
self.bytecode_file_id: Optional[FileId] = params.bytecode_file_id
8994
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
9196
self.gas: Optional[int] = params.gas
9297
self.initial_balance: Optional[int] = params.initial_balance
9398
self.auto_renew_period: Duration = params.auto_renew_period
9499
self.parameters: Optional[bytes] = params.parameters
95100
self.contract_memo: Optional[str] = params.contract_memo
96101
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
100103
self.max_automatic_token_associations: Optional[int] = (
101104
params.max_automatic_token_associations
102105
)
@@ -107,9 +110,8 @@ def __init__(
107110
self._default_transaction_fee = Hbar(20).to_tinybars()
108111

109112
def set_bytecode_file_id(
110-
self,
111-
bytecode_file_id: Optional[FileId]
112-
) -> 'ContractCreateTransaction':
113+
self, bytecode_file_id: Optional[FileId]
114+
) -> "ContractCreateTransaction":
113115
"""
114116
Sets the FileID of the file containing the contract bytecode.
115117
@@ -124,10 +126,7 @@ def set_bytecode_file_id(
124126
self.bytecode_file_id = bytecode_file_id
125127
return self
126128

127-
def set_bytecode(
128-
self,
129-
code: Optional[bytes]
130-
) -> 'ContractCreateTransaction':
129+
def set_bytecode(self, code: Optional[bytes]) -> "ContractCreateTransaction":
131130
"""
132131
Sets the bytecode for the contract.
133132
@@ -146,9 +145,8 @@ def set_bytecode(
146145
return self
147146

148147
def set_proxy_account_id(
149-
self,
150-
proxy_account_id: Optional[AccountId]
151-
) -> 'ContractCreateTransaction':
148+
self, proxy_account_id: Optional[AccountId]
149+
) -> "ContractCreateTransaction":
152150
"""
153151
Sets the proxy account ID for the contract.
154152
@@ -163,14 +161,13 @@ def set_proxy_account_id(
163161
return self
164162

165163
def set_admin_key(
166-
self,
167-
admin_key: Optional[PrivateKey]
168-
) -> 'ContractCreateTransaction':
164+
self, admin_key: Optional[PublicKey]
165+
) -> "ContractCreateTransaction":
169166
"""
170167
Sets the admin key for the contract.
171168
172169
Args:
173-
admin_key (Optional[PrivateKey]): The admin key.
170+
admin_key (Optional[PublicKey]): The admin key.
174171
175172
Returns:
176173
ContractCreateTransaction: This transaction instance.
@@ -179,10 +176,7 @@ def set_admin_key(
179176
self.admin_key = admin_key
180177
return self
181178

182-
def set_gas(
183-
self,
184-
gas: Optional[int]
185-
) -> 'ContractCreateTransaction':
179+
def set_gas(self, gas: Optional[int]) -> "ContractCreateTransaction":
186180
"""
187181
Sets the gas limit for contract creation.
188182
@@ -197,9 +191,8 @@ def set_gas(
197191
return self
198192

199193
def set_initial_balance(
200-
self,
201-
initial_balance: Optional[int]
202-
) -> 'ContractCreateTransaction':
194+
self, initial_balance: Optional[int]
195+
) -> "ContractCreateTransaction":
203196
"""
204197
Sets the initial balance for the contract in tinybars.
205198
@@ -214,9 +207,8 @@ def set_initial_balance(
214207
return self
215208

216209
def set_auto_renew_period(
217-
self,
218-
auto_renew_period: Duration
219-
) -> 'ContractCreateTransaction':
210+
self, auto_renew_period: Duration
211+
) -> "ContractCreateTransaction":
220212
"""
221213
Sets the auto-renewal period for the contract.
222214
@@ -231,9 +223,8 @@ def set_auto_renew_period(
231223
return self
232224

233225
def set_constructor_parameters(
234-
self,
235-
parameters: Optional[ContractFunctionParameters | bytes]
236-
) -> 'ContractCreateTransaction':
226+
self, parameters: Optional[ContractFunctionParameters | bytes]
227+
) -> "ContractCreateTransaction":
237228
"""
238229
Sets the constructor parameters for the contract.
239230
@@ -252,9 +243,8 @@ def set_constructor_parameters(
252243
return self
253244

254245
def set_contract_memo(
255-
self,
256-
contract_memo: Optional[str]
257-
) -> 'ContractCreateTransaction':
246+
self, contract_memo: Optional[str]
247+
) -> "ContractCreateTransaction":
258248
"""
259249
Sets the contract_memo for the contract.
260250
@@ -269,9 +259,8 @@ def set_contract_memo(
269259
return self
270260

271261
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":
275264
"""
276265
Sets the account ID that will pay for auto-renewal.
277266
@@ -287,9 +276,8 @@ def set_auto_renew_account_id(
287276
return self
288277

289278
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":
293281
"""
294282
Sets the maximum number of automatic token associations.
295283
@@ -305,9 +293,8 @@ def set_max_automatic_token_associations(
305293
return self
306294

307295
def set_staked_account_id(
308-
self,
309-
staked_account_id: Optional[AccountId]
310-
) -> 'ContractCreateTransaction':
296+
self, staked_account_id: Optional[AccountId]
297+
) -> "ContractCreateTransaction":
311298
"""
312299
Sets the account ID to stake to.
313300
@@ -322,9 +309,8 @@ def set_staked_account_id(
322309
return self
323310

324311
def set_staked_node_id(
325-
self,
326-
staked_node_id: Optional[int]
327-
) -> 'ContractCreateTransaction':
312+
self, staked_node_id: Optional[int]
313+
) -> "ContractCreateTransaction":
328314
"""
329315
Sets the node ID to stake to.
330316
@@ -339,9 +325,8 @@ def set_staked_node_id(
339325
return self
340326

341327
def set_decline_reward(
342-
self,
343-
decline_reward: Optional[bool]
344-
) -> 'ContractCreateTransaction':
328+
self, decline_reward: Optional[bool]
329+
) -> "ContractCreateTransaction":
345330
"""
346331
Sets whether to decline staking rewards.
347332
@@ -356,6 +341,16 @@ def set_decline_reward(
356341
self.decline_reward = decline_reward
357342
return self
358343

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+
359354
def build_transaction_body(self):
360355
"""
361356
Builds and returns the protobuf transaction body for contract creation.
@@ -367,25 +362,31 @@ def build_transaction_body(self):
367362
Raises:
368363
ValueError: If required fields are missing.
369364
"""
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()
375366

376367
contract_create_body = ContractCreateTransactionBody(
377368
gas=self.gas,
378369
initialBalance=self.initial_balance,
379370
constructorParameters=self.parameters,
380371
memo=self.contract_memo,
381372
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+
),
385384
staked_node_id=self.staked_node_id,
386385
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),
389390
fileID=self.bytecode_file_id._to_proto() if self.bytecode_file_id else None,
390391
initcode=self.bytecode,
391392
)
@@ -407,6 +408,5 @@ def _get_method(self, channel: _Channel) -> _Method:
407408
create contracts.
408409
"""
409410
return _Method(
410-
transaction_func=channel.smart_contract.createContract,
411-
query_func=None
411+
transaction_func=channel.smart_contract.createContract, query_func=None
412412
)

tests/integration/contract_create_transaction_e2e_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_integration_contract_create_transaction_can_execute(env):
4040

4141
receipt = (
4242
ContractCreateTransaction()
43-
.set_admin_key(env.operator_key)
43+
.set_admin_key(env.operator_key.public_key())
4444
.set_gas(CONTRACT_DEPLOY_GAS)
4545
.set_bytecode_file_id(file_id)
4646
.set_contract_memo("some test contract create transaction memo")
@@ -79,7 +79,7 @@ def test_integration_contract_create_transaction_with_constructor(env):
7979

8080
receipt = (
8181
ContractCreateTransaction()
82-
.set_admin_key(env.operator_key)
82+
.set_admin_key(env.operator_key.public_key())
8383
.set_gas(CONTRACT_DEPLOY_GAS)
8484
.set_bytecode_file_id(file_id)
8585
.set_constructor_parameters(params)
@@ -102,7 +102,7 @@ def test_integration_contract_create_transaction_set_bytecode(env):
102102

103103
receipt = (
104104
ContractCreateTransaction()
105-
.set_admin_key(env.operator_key)
105+
.set_admin_key(env.operator_key.public_key())
106106
.set_gas(CONTRACT_DEPLOY_GAS)
107107
.set_bytecode(bytecode)
108108
.execute(env.client)

0 commit comments

Comments
 (0)