22
33from __future__ import annotations
44
5- import importlib
6- import sys
7-
85import pytest
96
10- from hiero_sdk_python .crypto .private_key import PrivateKey
117from tck .errors import JsonRpcError
128from tck .handlers import contract as contract_handlers
139from tck .param .contract import CreateContractParams
1612pytestmark = pytest .mark .unit
1713
1814
19- class TestCreateContractParams :
20- def test_parses_all_params (self ):
21- params = CreateContractParams .parse_json_params (
22- {
23- "sessionId" : "session-1" ,
24- "bytecodeFileId" : "0.0.123" ,
25- "initcode" : "0x60006000" ,
26- "adminKey" : PrivateKey .generate_ed25519 ().public_key ().to_string_der (),
27- "gas" : "1000000" ,
28- "initialBalance" : "1000" ,
29- "constructorParameters" : "0xabcd" ,
30- "autoRenewPeriod" : "7000000" ,
31- "autoRenewAccountId" : "0.0.5" ,
32- "memo" : "contract test" ,
33- "stakedAccountId" : "0.0.6" ,
34- "stakedNodeId" : "3" ,
35- "declineStakingReward" : True ,
36- "maxAutomaticTokenAssociations" : 10 ,
37- "commonTransactionParams" : {"memo" : "tx memo" },
38- }
39- )
40-
41- assert params .sessionId == "session-1"
42- assert params .bytecodeFileId == "0.0.123"
43- assert params .initcode == "0x60006000"
44- assert params .gas == "1000000"
45- assert params .initialBalance == "1000"
46- assert params .constructorParameters == "0xabcd"
47- assert params .autoRenewPeriod == "7000000"
48- assert params .autoRenewAccountId == "0.0.5"
49- assert params .memo == "contract test"
50- assert params .stakedAccountId == "0.0.6"
51- assert params .stakedNodeId == "3"
52- assert params .declineStakingReward is True
53- assert params .maxAutomaticTokenAssociations == 10
54- assert params .commonTransactionParams is not None
55- assert params .commonTransactionParams .memo == "tx memo"
56-
57- def test_requires_session_id (self ):
58- with pytest .raises (ValueError ):
59- CreateContractParams .parse_json_params ({"gas" : "1000000" })
60-
61- def test_absent_params_stay_none (self ):
62- params = CreateContractParams .parse_json_params ({"sessionId" : "session-1" })
63-
64- assert params .bytecodeFileId is None
65- assert params .initcode is None
66- assert params .adminKey is None
67- assert params .gas is None
68- assert params .declineStakingReward is None
69- assert params .maxAutomaticTokenAssociations is None
70- assert params .commonTransactionParams is None
71-
72-
7315class TestBuildCreateContractTransaction :
74- def test_maps_all_params (self ):
75- admin_key = PrivateKey .generate_ed25519 ().public_key ()
76- params = CreateContractParams (
77- sessionId = "session-1" ,
78- initcode = "0x60006000" ,
79- adminKey = admin_key .to_string_der (),
80- gas = "1000000" ,
81- initialBalance = "1000" ,
82- constructorParameters = "0xabcd" ,
83- autoRenewPeriod = "7000000" ,
84- autoRenewAccountId = "0.0.5" ,
85- memo = "contract test" ,
86- stakedAccountId = "0.0.6" ,
87- declineStakingReward = True ,
88- maxAutomaticTokenAssociations = 10 ,
89- )
90-
91- transaction = contract_handlers ._build_create_contract_transaction (params )
92-
93- assert transaction .bytecode == b"\x60 \x00 \x60 \x00 "
94- assert transaction .bytecode_file_id is None
95- assert transaction .gas == 1000000
96- assert transaction .initial_balance == 1000
97- assert transaction .parameters == b"\xab \xcd "
98- assert transaction .auto_renew_period .seconds == 7000000
99- assert str (transaction .auto_renew_account_id ) == "0.0.5"
100- assert transaction .contract_memo == "contract test"
101- assert str (transaction .staked_account_id ) == "0.0.6"
102- assert transaction .staked_node_id is None
103- assert transaction .decline_reward is True
104- assert transaction .max_automatic_token_associations == 10
105- assert transaction .admin_key is not None
106- assert transaction .admin_key .to_bytes () == admin_key .to_bytes ()
107-
10816 def test_bytecode_file_id_wins_when_both_sources_supplied (self ):
10917 params = CreateContractParams (
11018 sessionId = "session-1" ,
@@ -118,98 +26,15 @@ def test_bytecode_file_id_wins_when_both_sources_supplied(self):
11826 assert str (transaction .bytecode_file_id ) == "0.0.123"
11927 assert transaction .bytecode is None
12028
121- def test_neither_bytecode_source_left_unset_for_network (self ):
122- params = CreateContractParams (sessionId = "session-1" , gas = "1000000" )
123-
124- transaction = contract_handlers ._build_create_contract_transaction (params )
125-
126- assert transaction .bytecode is None
127- assert transaction .bytecode_file_id is None
128-
129- def test_default_auto_renew_period_preserved_when_absent (self ):
130- params = CreateContractParams (sessionId = "session-1" , gas = "1000000" )
131-
132- transaction = contract_handlers ._build_create_contract_transaction (params )
133-
134- assert transaction .auto_renew_period .seconds == 90 * 24 * 60 * 60
135-
13629 def test_invalid_gas_raises_invalid_params (self ):
13730 params = CreateContractParams (sessionId = "session-1" , gas = "not-a-number" )
13831
13932 with pytest .raises (JsonRpcError ):
14033 contract_handlers ._build_create_contract_transaction (params )
14134
142- def test_int64_boundaries_pass_through_to_the_network (self ):
143- params = CreateContractParams (
144- sessionId = "session-1" ,
145- gas = "9223372036854775807" ,
146- initialBalance = "-9223372036854775808" ,
147- )
148-
149- transaction = contract_handlers ._build_create_contract_transaction (params )
150-
151- assert transaction .gas == 9223372036854775807
152- assert transaction .initial_balance == - 9223372036854775808
153-
154- @pytest .mark .parametrize ("gas" , ["-1" , "-9223372036854775808" ])
155- def test_negative_gas_raises_sdk_error (self , gas ):
156- """The TCK driver expects an internal SDK error for negative gas, matching the JS SDK."""
157- params = CreateContractParams (sessionId = "session-1" , gas = gas )
158-
159- with pytest .raises (ValueError , match = "Gas cannot be negative" ):
160- contract_handlers ._build_create_contract_transaction (params )
161-
16235 @pytest .mark .parametrize ("gas" , ["9223372036854775808" , "-9223372036854775809" ])
16336 def test_gas_out_of_int64_range_raises_invalid_params (self , gas ):
16437 params = CreateContractParams (sessionId = "session-1" , gas = gas )
16538
16639 with pytest .raises (JsonRpcError ):
16740 contract_handlers ._build_create_contract_transaction (params )
168-
169- def test_maps_staked_node_id (self ):
170- params = CreateContractParams (sessionId = "session-1" , stakedNodeId = "3" , gas = "1000000" )
171-
172- transaction = contract_handlers ._build_create_contract_transaction (params )
173-
174- assert transaction .staked_node_id == 3
175- assert transaction .staked_account_id is None
176-
177- def test_last_staking_target_wins_when_both_supplied (self ):
178- """The SDK setters keep the staked_id oneof consistent; last applied (stakedNodeId) wins."""
179- params = CreateContractParams (
180- sessionId = "session-1" ,
181- stakedAccountId = "0.0.6" ,
182- stakedNodeId = "3" ,
183- gas = "1000000" ,
184- )
185-
186- transaction = contract_handlers ._build_create_contract_transaction (params )
187-
188- assert transaction .staked_account_id is None
189- assert transaction .staked_node_id == 3
190-
191- def test_invalid_initcode_hex_raises_value_error (self ):
192- params = CreateContractParams (sessionId = "session-1" , initcode = "0xZZ" , gas = "1000000" )
193-
194- with pytest .raises (ValueError ):
195- contract_handlers ._build_create_contract_transaction (params )
196-
197-
198- def test_create_contract_registered_via_package_import ():
199- """Importing the tck.handlers package alone must register createContract.
200-
201- Re-importing the package from scratch (which recreates the registry) makes
202- this test fail if tck/handlers/__init__.py stops importing the contract
203- module; module-level imports in this file cannot mask that.
204- """
205- saved = {
206- name : mod for name , mod in sys .modules .items () if name == "tck.handlers" or name .startswith ("tck.handlers." )
207- }
208- try :
209- for name in saved :
210- del sys .modules [name ]
211- fresh_handlers = importlib .import_module ("tck.handlers" )
212- assert fresh_handlers .get_handler ("createContract" ) is not None
213- finally :
214- sys .modules .update (saved )
215- sys .modules ["tck" ].handlers = saved ["tck.handlers" ]
0 commit comments