From 8478cfa39a2ea77e529a4e8b18a73d7aec441456 Mon Sep 17 00:00:00 2001 From: Tibor Reiss Date: Thu, 17 Apr 2025 08:13:22 +0200 Subject: [PATCH 1/3] Add abi attributes for ContractFunction --- newsfragments/3626.feature.rst | 1 + tests/core/contracts/test_contract_example.py | 20 +++++++++++++++++++ web3/contract/base_contract.py | 16 +++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 newsfragments/3626.feature.rst diff --git a/newsfragments/3626.feature.rst b/newsfragments/3626.feature.rst new file mode 100644 index 0000000000..82eec3bc5e --- /dev/null +++ b/newsfragments/3626.feature.rst @@ -0,0 +1 @@ +Add abi properties stateMutability, type, inputs, outputs to the ``ContractFunction`` object as ``state_mutability``, ``type``, ``inputs``, ``outputs``. diff --git a/tests/core/contracts/test_contract_example.py b/tests/core/contracts/test_contract_example.py index e47c9352d9..f8dcc1eaab 100644 --- a/tests/core/contracts/test_contract_example.py +++ b/tests/core/contracts/test_contract_example.py @@ -112,6 +112,26 @@ def test_updating_greeting_emits_event(w3, foo_contract): assert event.args._bar == "testing contracts is easy" +def test_functions_abi_state_mutability(w3, foo_contract): + assert foo_contract.functions.setBar.state_mutability == "nonpayable" + assert foo_contract.functions.bar.state_mutability == "view" + + +def test_functions_abi_type(w3, foo_contract): + assert foo_contract.functions.setBar.type == "function" + assert foo_contract.functions.bar.type == "function" + + +def test_functions_abi_inputs(w3, foo_contract): + assert foo_contract.functions.setBar.inputs == [{"name": "_bar", "type": "string"}] + assert foo_contract.functions.bar.inputs == [] + + +def test_functions_abi_outputs(w3, foo_contract): + assert foo_contract.functions.setBar.outputs == [] + assert foo_contract.functions.bar.outputs == [{"name": "", "type": "string"}] + + @pytest.fixture def async_eth_tester(): return AsyncEthereumTesterProvider().ethereum_tester diff --git a/web3/contract/base_contract.py b/web3/contract/base_contract.py index 6e9c3c9a04..81dce93683 100644 --- a/web3/contract/base_contract.py +++ b/web3/contract/base_contract.py @@ -596,6 +596,22 @@ def __init__(self, abi: Optional[ABIFunction] = None) -> None: self.argument_names = tuple([input.get("name", None) for input in event_inputs]) self.argument_types = tuple([input["type"] for input in event_inputs]) + @property + def state_mutability(self) -> str: + return self.abi["stateMutability"] + + @property + def type(self) -> str: + return "function" + + @property + def inputs(self) -> Optional[List]: + return self.abi.get("inputs") + + @property + def outputs(self) -> Optional[List]: + return self.abi.get("outputs") + @combomethod def _get_abi(cls) -> ABIFunction: if not cls.args and not cls.kwargs: From 23a6a8083bb6e297f7a97924ad6091725e84f328 Mon Sep 17 00:00:00 2001 From: Tibor Reiss Date: Fri, 18 Apr 2025 08:40:04 +0200 Subject: [PATCH 2/3] Add abi attributes for ContractEvent --- newsfragments/3626.feature.rst | 1 + tests/core/contracts/test_contract_example.py | 8 ++++++++ web3/contract/base_contract.py | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/newsfragments/3626.feature.rst b/newsfragments/3626.feature.rst index 82eec3bc5e..c3bbca51dd 100644 --- a/newsfragments/3626.feature.rst +++ b/newsfragments/3626.feature.rst @@ -1 +1,2 @@ Add abi properties stateMutability, type, inputs, outputs to the ``ContractFunction`` object as ``state_mutability``, ``type``, ``inputs``, ``outputs``. +Add abi properties type, anonymous, inputs to the ``ContractEvent`` object with the same name. diff --git a/tests/core/contracts/test_contract_example.py b/tests/core/contracts/test_contract_example.py index f8dcc1eaab..5326cde2fb 100644 --- a/tests/core/contracts/test_contract_example.py +++ b/tests/core/contracts/test_contract_example.py @@ -132,6 +132,14 @@ def test_functions_abi_outputs(w3, foo_contract): assert foo_contract.functions.bar.outputs == [{"name": "", "type": "string"}] +def test_events_abi(w3, foo_contract): + assert foo_contract.events.barred.type == "event" + assert foo_contract.events.barred.anonymous is False + assert foo_contract.events.barred.inputs == [ + {"indexed": False, "name": "_bar", "type": "string"} + ] + + @pytest.fixture def async_eth_tester(): return AsyncEthereumTesterProvider().ethereum_tester diff --git a/web3/contract/base_contract.py b/web3/contract/base_contract.py index 81dce93683..488317abf8 100644 --- a/web3/contract/base_contract.py +++ b/web3/contract/base_contract.py @@ -209,6 +209,18 @@ def topic(self) -> HexStr: self._topic = encode_hex(keccak(text=self.signature)) return self._topic + @property + def type(self) -> str: + return "event" + + @property + def anonymous(self) -> Optional[bool]: + return self.abi.get("anonymous") + + @property + def inputs(self) -> Optional[List]: + return self.abi.get("inputs") + @combomethod def _get_event_abi(cls) -> ABIEvent: if cls.abi: From c980bca37990affa3e8877c04ef49f2f36659399 Mon Sep 17 00:00:00 2001 From: Tibor Reiss Date: Fri, 18 Apr 2025 09:06:50 +0200 Subject: [PATCH 3/3] Fix types --- web3/contract/base_contract.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web3/contract/base_contract.py b/web3/contract/base_contract.py index 488317abf8..537b3c2b77 100644 --- a/web3/contract/base_contract.py +++ b/web3/contract/base_contract.py @@ -22,6 +22,8 @@ ) from eth_typing import ( ABI, + ABIComponent, + ABIComponentIndexed, ABIElement, ABIEvent, ABIFunction, @@ -218,7 +220,7 @@ def anonymous(self) -> Optional[bool]: return self.abi.get("anonymous") @property - def inputs(self) -> Optional[List]: + def inputs(self) -> Optional[Sequence["ABIComponentIndexed"]]: return self.abi.get("inputs") @combomethod @@ -617,11 +619,11 @@ def type(self) -> str: return "function" @property - def inputs(self) -> Optional[List]: + def inputs(self) -> Optional[Sequence["ABIComponent"]]: return self.abi.get("inputs") @property - def outputs(self) -> Optional[List]: + def outputs(self) -> Optional[Sequence["ABIComponent"]]: return self.abi.get("outputs") @combomethod