From e7ff0bc873c7e8d19b8b4728b79c5df727bad925 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 12 Dec 2023 18:35:16 -0600 Subject: [PATCH] test: fix broken tests --- ape_etherscan/verify.py | 4 ++-- tests/conftest.py | 42 ++++++++++++++++++++++++++--------------- tests/test_etherscan.py | 24 ++++++++++++++++++----- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/ape_etherscan/verify.py b/ape_etherscan/verify.py index 93caa63..4c7412c 100644 --- a/ape_etherscan/verify.py +++ b/ape_etherscan/verify.py @@ -198,7 +198,8 @@ def constructor_arguments(self) -> str: if code := self._contract_type.runtime_bytecode: runtime_code = code.bytecode or "" deployment_code = deploy_receipt["input"] - return extract_constructor_arguments(deployment_code, runtime_code) + ctor_args = extract_constructor_arguments(deployment_code, runtime_code) + return ctor_args else: raise ContractVerificationError("Failed to find runtime bytecode.") @@ -414,7 +415,6 @@ def extract_constructor_arguments(deployment_bytecode: str, runtime_bytecode: st runtime_bytecode = ( runtime_bytecode[2:] if runtime_bytecode.startswith("0x") else runtime_bytecode ) - if deployment_bytecode.endswith(runtime_bytecode): # If the runtime bytecode is at the end of the deployment bytecode, # there are no constructor arguments diff --git a/tests/conftest.py b/tests/conftest.py index e5cfa08..eeed689 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -407,12 +407,10 @@ def setup_mock_account_transactions_response( response_data_file, file_name=file_name, response_overrides=overrides ) - self.add_handler("GET", "account", params, return_value=response) - self.set_network("ethereum", "mainnet") - return response + return self._setup_account_response(params, response) def setup_mock_account_transactions_with_ctor_args_response( - self, address: Optional[AddressType] = None + self, address: Optional[AddressType] = None, **overrides ): file_name = "get_account_transactions_with_ctor_args.json" test_data_path = MOCK_RESPONSES_PATH / file_name @@ -424,10 +422,16 @@ def setup_mock_account_transactions_with_ctor_args_response( params = EXPECTED_ACCOUNT_TXNS_PARAMS with open(test_data_path) as response_data_file: - response = self.get_mock_response(response_data_file, file_name=file_name) - self.add_handler("GET", "account", params, return_value=response) - self.set_network("ethereum", "mainnet") - return response + response = self.get_mock_response( + response_data_file, file_name=file_name, response_overrides=overrides + ) + + return self._setup_account_response(params, response) + + def _setup_account_response(self, params, response): + self.add_handler("GET", "account", params, return_value=response) + self.set_network("ethereum", "mainnet") + return response def get_mock_response( self, response_data: Optional[Union[IO, Dict, str, MagicMock]] = None, **kwargs @@ -476,19 +480,22 @@ def verification_params(address_to_verify, standard_input_json): @pytest.fixture(scope="session") -def verification_params_with_ctor_args( - address_to_verify_with_ctor_args, library, standard_input_json -): +def constructor_arguments(): # abi-encoded representation of uint256 value 42 - ctor_args = "000000000000000000000000000000000000000000000000000000000000002a" # noqa: E501 + return "000000000000000000000000000000000000000000000000000000000000002a" # noqa: E501 + +@pytest.fixture(scope="session") +def verification_params_with_ctor_args( + address_to_verify_with_ctor_args, library, standard_input_json, constructor_arguments +): json_data = standard_input_json.copy() json_data["libraryaddress1"] = "0xF2Df0b975c0C9eFa2f8CA0491C2d1685104d2488" return { "action": "verifysourcecode", "codeformat": "solidity-standard-json-input", - "constructorArguements": ctor_args, + "constructorArguements": constructor_arguments, "contractaddress": address_to_verify_with_ctor_args, "contractname": "foo.sol:fooWithConstructor", "evmversion": None, @@ -530,7 +537,7 @@ def address_to_verify(contract_to_verify): @pytest.fixture(scope="session") -def address_to_verify_with_ctor_args(fake_connection, project, account): +def contract_to_verify_with_ctor_args(fake_connection, project, account): # Deploy the library first. library = account.deploy(project.MyLib) ape.chain.contracts._local_contract_types[library.address] = library.contract_type @@ -541,7 +548,12 @@ def address_to_verify_with_ctor_args(fake_connection, project, account): foo = project.fooWithConstructor.deploy(42, sender=account) ape.chain.contracts._local_contract_types[address] = foo.contract_type - return foo.address + return foo + + +@pytest.fixture(scope="session") +def address_to_verify_with_ctor_args(contract_to_verify_with_ctor_args): + return contract_to_verify_with_ctor_args.address @pytest.fixture(scope="session") diff --git a/tests/test_etherscan.py b/tests/test_etherscan.py index db97ebb..015efb8 100644 --- a/tests/test_etherscan.py +++ b/tests/test_etherscan.py @@ -90,9 +90,7 @@ def setup_verification_test( mock_backend, verification_params, verification_tester_cls, contract_to_verify ): def setup(found_handler: Callable, threshold: int = 2): - overrides = { - "result": [{"input": contract_to_verify.contract_type.deployment_bytecode.bytecode}] - } + overrides = _acct_tx_overrides(contract_to_verify) mock_backend.setup_mock_account_transactions_response( address=contract_to_verify.address, **overrides ) @@ -114,11 +112,15 @@ def setup_verification_test_with_ctor_args( mock_backend, verification_params_with_ctor_args, verification_tester_cls, - address_to_verify_with_ctor_args, + contract_to_verify_with_ctor_args, + constructor_arguments, ): def setup(found_handler: Callable, threshold: int = 2): + overrides = _acct_tx_overrides( + contract_to_verify_with_ctor_args, args=constructor_arguments + ) mock_backend.setup_mock_account_transactions_with_ctor_args_response( - address=address_to_verify_with_ctor_args + address=contract_to_verify_with_ctor_args.address, **overrides ) mock_backend.add_handler( "POST", "contract", verification_params_with_ctor_args, return_value=PUBLISH_GUID @@ -258,3 +260,15 @@ def test_publish_contract_with_ctor_args( setup_verification_test_with_ctor_args(lambda: "Pass - You made it!") explorer.publish_contract(address_to_verify_with_ctor_args) assert caplog.records[-1].message == expected_verification_log_with_ctor_args + + +def _acct_tx_overrides(contract, args=None): + suffix = args or "" + if suffix.startswith("0x"): + suffix = suffix[2:] + + # Include construcor aguments! + ct = contract.contract_type + prefix = ct.deployment_bytecode.bytecode + code = f"{prefix}{suffix}" + return {"result": [{"input": code}]}