Skip to content

Commit

Permalink
test: fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 13, 2023
1 parent 34983ec commit e7ff0bc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ape_etherscan/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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
Expand Down
42 changes: 27 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
24 changes: 19 additions & 5 deletions tests/test_etherscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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}]}

0 comments on commit e7ff0bc

Please sign in to comment.