Skip to content

Commit

Permalink
Use the event interface to get the correct instance
Browse files Browse the repository at this point in the history
  • Loading branch information
reedsa committed Sep 5, 2024
1 parent 30434f0 commit bdc2282
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
46 changes: 16 additions & 30 deletions tests/core/contracts/test_contract_ambiguous_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ def test_get_abi_from_event(ambiguous_event_contract: "Contract") -> None:
"type": "event",
}

event_get_abi_result = ambiguous_event_contract.events.LogSingleArg._get_event_abi(
["uint256"]
)
event_get_abi_result = ambiguous_event_contract.events[
"LogSingleArg(uint256)"
]._get_event_abi()
assert expected_event_abi == event_get_abi_result


Expand Down Expand Up @@ -204,6 +204,19 @@ def test_contract_event_methods(
ContractEvent, ambiguous_event_contract.events["LogSingleArg(uint256)"]
)

assert log_arg_event._get_event_abi() == {
"anonymous": False,
"inputs": [
{
"indexed": False,
"internalType": "uint256",
"name": "arg0",
"type": "uint256",
}
],
"name": "LogSingleArg",
"type": "event",
}
assert log_arg_event.event_name == "LogSingleArg"
assert log_arg_event.get_logs() == []

Expand Down Expand Up @@ -245,30 +258,3 @@ def test_contract_event_methods(
"blockHash": "0x0",
"blockNumber": 0,
}

assert log_arg_event._get_event_abi(["uint256"]) == {
"anonymous": False,
"inputs": [
{
"indexed": False,
"internalType": "uint256",
"name": "arg0",
"type": "uint256",
}
],
"name": "LogSingleArg",
"type": "event",
}
assert log_arg_event._get_event_abi(["bytes32"]) == {
"anonymous": False,
"inputs": [
{
"indexed": False,
"internalType": "bytes32",
"name": "arg0",
"type": "bytes32",
}
],
"name": "LogSingleArg",
"type": "event",
}
34 changes: 16 additions & 18 deletions web3/contract/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
encode_hex,
filter_abi_by_type,
function_abi_to_4byte_selector,
get_abi_input_types,
get_normalized_abi_inputs,
is_list_like,
is_text,
Expand Down Expand Up @@ -122,6 +123,7 @@
)
from web3.utils.abi import (
check_if_arguments_can_be_encoded,
filter_abi_by_name,
get_abi_element,
get_abi_element_info,
get_event_abi,
Expand Down Expand Up @@ -169,23 +171,8 @@ def __init__(
@classmethod
def _get_event_abi(
cls,
argument_types: Optional[Sequence[str]] = None,
) -> ABIEvent:
abi_events = filter_abi_by_type("event", cls.contract_abi)

if argument_types is None:
return cast(
ABIEvent,
get_abi_element(abi_events, cls.event_name),
)

return cast(
ABIEvent,
get_abi_element(
abi_events,
get_abi_element_identifier(cls.event_name, argument_types),
),
)
return cls.abi

@combomethod
def process_receipt(
Expand Down Expand Up @@ -453,7 +440,6 @@ def __init__(
event_name=event["name"],
abi=event,
)
setattr(self, event["name"], event_factory)
setattr(self, abi_to_signature(event), event_factory)

def __getattr__(self, event_name: str) -> Type["BaseContractEvent"]:
Expand All @@ -462,12 +448,24 @@ def __getattr__(self, event_name: str) -> Type["BaseContractEvent"]:
"The abi for this contract contains no event definitions. ",
"Are you sure you provided the correct contract abi?",
)
elif event_name not in [event["name"] for event in self._events]:
elif event_name.split("(")[0] not in [
event["name"].split("(")[0] for event in self._events
]:
raise ABIEventNotFound(
f"The event '{event_name}' was not found in this contract's abi. ",
"Are you sure you provided the correct contract abi?",
)
else:
# See if there is a single event with the same name
event_abi = filter_abi_by_name(event_name.split("(")[0], self._events)
if len(event_abi) != 1:
raise MismatchedABI(
f"Could not identify the intended event with name `{event_name}`"
)

argument_types = get_abi_input_types(event_abi[0])
event_name = f"{event_name}({','.join(argument_types)})"

return super().__getattribute__(event_name)

def __getitem__(self, event_name: str) -> Type["BaseContractEvent"]:
Expand Down

0 comments on commit bdc2282

Please sign in to comment.