diff --git a/src/ape/managers/chain.py b/src/ape/managers/chain.py index c4cdc20103..1358f59640 100644 --- a/src/ape/managers/chain.py +++ b/src/ape/managers/chain.py @@ -1173,7 +1173,7 @@ def instance_at( # Always attempt to get an existing contract type to update caches contract_type = self.get(contract_address, default=contract_type) except Exception as err: - if contract_type: + if contract_type or abi: # If a default contract type was provided, don't error and use it. logger.error(str(err)) else: diff --git a/tests/functional/test_contract.py b/tests/functional/test_contract.py index c46f4cf457..aab93b4bb6 100644 --- a/tests/functional/test_contract.py +++ b/tests/functional/test_contract.py @@ -40,6 +40,28 @@ def test_Contract_from_json_str(contract_instance): assert contract.myNumber() == 0 +def test_Contract_from_json_str_retrieval_check_fails(mocker, chain, vyper_contract_instance): + """ + Tests a bug when providing an abi= but fetch-attempt raises that we don't + raise since the abi was already given. + """ + # Make `.get()` fail. + orig = chain.contracts.get + mock_get = mocker.MagicMock() + mock_get.side_effect = Exception + + abi_str = json.dumps([abi.model_dump() for abi in vyper_contract_instance.contract_type.abi]) + + chain.contracts.get = mock_get + try: + contract = Contract(vyper_contract_instance.address, abi=abi_str) + finally: + chain.contracts.get = orig + + # Mostly, we are asserting it did not fail. + assert isinstance(contract, ContractInstance) + + def test_Contract_from_file(contract_instance): """ need feedback about the json file specifications