Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions newsfragments/1002.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve contract call errors when a contract address has code but returns
insufficient data for ABI decoding, raising ``ContractLogicError`` instead of a
generic decode failure.
13 changes: 9 additions & 4 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ABIReceiveNotFound,
BadFunctionCallOutput,
BlockNumberOutOfRange,
ContractLogicError,
InvalidAddress,
MismatchedABI,
NameNotFound,
Expand Down Expand Up @@ -498,8 +499,10 @@ def test_call_rejects_invalid_ens_name(address_reflector_contract, call):
def test_call_missing_function(mismatched_math_contract, call):
# note: contract being called needs to have a fallback function
# (StringContract in this case)
expected_missing_function_error_message = "Could not decode contract function call"
with pytest.raises(BadFunctionCallOutput) as exception_info:
expected_missing_function_error_message = (
"Contract call failed because execution reverted or returned no data."
)
with pytest.raises(ContractLogicError) as exception_info:
call(contract=mismatched_math_contract, contract_function="return13")
assert expected_missing_function_error_message in str(exception_info.value)

Expand Down Expand Up @@ -1788,8 +1791,10 @@ async def test_async_call_rejects_invalid_ens_name(
async def test_async_call_missing_function(async_mismatched_math_contract, async_call):
# note: contract being called needs to have a fallback function
# (StringContract in this case)
expected_missing_function_error_message = "Could not decode contract function call"
with pytest.raises(BadFunctionCallOutput) as exception_info:
expected_missing_function_error_message = (
"Contract call failed because execution reverted or returned no data."
)
with pytest.raises(ContractLogicError) as exception_info:
await async_call(
contract=async_mismatched_math_contract,
contract_function="return13",
Expand Down
52 changes: 36 additions & 16 deletions web3/contract/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
)
from web3.exceptions import (
BadFunctionCallOutput,
ContractLogicError,
Web3ValueError,
)
from web3.types import (
Expand All @@ -79,6 +80,19 @@
ACCEPTABLE_EMPTY_STRINGS = ["0x", b"0x", "", b""]


def _is_code_missing(code: bytes) -> bool:
return code in ACCEPTABLE_EMPTY_STRINGS


def _has_insufficient_output_data(
return_data: bytes,
output_types: Sequence[TypeStr],
) -> bool:
if return_data in ACCEPTABLE_EMPTY_STRINGS:
return True
return len(return_data) < 32 * len(output_types)


@curry
def format_contract_call_return_data_curried(
async_w3: Union["AsyncWeb3[Any]", "Web3"],
Expand Down Expand Up @@ -201,20 +215,23 @@ def call_contract_function(
except DecodingError as e:
# Provide a more helpful error message than the one provided by
# eth-abi-utils
is_missing_code_error = (
return_data in ACCEPTABLE_EMPTY_STRINGS
and w3.eth.get_code(address) in ACCEPTABLE_EMPTY_STRINGS
)
if is_missing_code_error:
code = w3.eth.get_code(address)
if _is_code_missing(code):
msg = (
"Could not transact with/call contract function, is contract "
"deployed correctly and chain synced?"
)
else:
raise BadFunctionCallOutput(msg) from e
if output_types and _has_insufficient_output_data(return_data, output_types):
msg = (
f"Could not decode contract function call to {abi_element_identifier} "
f"with return data: {str(return_data)}, output_types: {output_types}"
"Contract call failed because execution reverted or returned no "
"data."
)
raise ContractLogicError(msg, data=str(return_data)) from e
msg = (
f"Could not decode contract function call to {abi_element_identifier} "
f"with return data: {str(return_data)}, output_types: {output_types}"
)
raise BadFunctionCallOutput(msg) from e

_normalizers = itertools.chain(
Expand Down Expand Up @@ -499,20 +516,23 @@ async def async_call_contract_function(
except DecodingError as e:
# Provide a more helpful error message than the one provided by
# eth-abi-utils
is_missing_code_error = (
return_data in ACCEPTABLE_EMPTY_STRINGS
and await async_w3.eth.get_code(address) in ACCEPTABLE_EMPTY_STRINGS
)
if is_missing_code_error:
code = await async_w3.eth.get_code(address)
if _is_code_missing(code):
msg = (
"Could not transact with/call contract function, is contract "
"deployed correctly and chain synced?"
)
else:
raise BadFunctionCallOutput(msg) from e
if output_types and _has_insufficient_output_data(return_data, output_types):
msg = (
f"Could not decode contract function call to {abi_element_identifier} "
f"with return data: {str(return_data)}, output_types: {output_types}"
"Contract call failed because execution reverted or returned no "
"data."
)
raise ContractLogicError(msg, data=str(return_data)) from e
msg = (
f"Could not decode contract function call to {abi_element_identifier} "
f"with return data: {str(return_data)}, output_types: {output_types}"
)
raise BadFunctionCallOutput(msg) from e

_normalizers = itertools.chain(
Expand Down