Skip to content

Commit

Permalink
fix: contract retrieval from ABI fails from fetch-attempt (#2230)
Browse files Browse the repository at this point in the history
Co-authored-by: slush <[email protected]>
Co-authored-by: Juliya Smith <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2024
1 parent a2ed551 commit 228ab19
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/functional/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 228ab19

Please sign in to comment.