Skip to content

Commit f11674c

Browse files
committed
Add abi attributes for ContractFunction
1 parent cdd2afa commit f11674c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Diff for: newsfragments/3626.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add abi properties stateMutability, type, inputs, outputs to the ``ContractFunction`` object as ``state_mutability``, ``type``, ``inputs``, ``outputs``.

Diff for: tests/core/contracts/test_contract_example.py

+20
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ def test_updating_greeting_emits_event(w3, foo_contract):
112112
assert event.args._bar == "testing contracts is easy"
113113

114114

115+
def test_functions_abi_state_mutability(w3, foo_contract):
116+
assert foo_contract.functions.setBar.state_mutability == "nonpayable"
117+
assert foo_contract.functions.bar.state_mutability == "view"
118+
119+
120+
def test_functions_abi_type(w3, foo_contract):
121+
assert foo_contract.functions.setBar.type == "function"
122+
assert foo_contract.functions.bar.type == "function"
123+
124+
125+
def test_functions_abi_inputs(w3, foo_contract):
126+
assert foo_contract.functions.setBar.inputs == [{"name": "_bar", "type": "string"}]
127+
assert foo_contract.functions.bar.inputs == []
128+
129+
130+
def test_functions_abi_outputs(w3, foo_contract):
131+
assert foo_contract.functions.setBar.outputs == []
132+
assert foo_contract.functions.bar.outputs == [{"name": "", "type": "string"}]
133+
134+
115135
@pytest.fixture
116136
def async_eth_tester():
117137
return AsyncEthereumTesterProvider().ethereum_tester

Diff for: web3/contract/base_contract.py

+16
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,22 @@ def __init__(self, abi: Optional[ABIFunction] = None) -> None:
596596
self.argument_names = tuple([input.get("name", None) for input in event_inputs])
597597
self.argument_types = tuple([input["type"] for input in event_inputs])
598598

599+
@property
600+
def state_mutability(self) -> str:
601+
return self.abi["stateMutability"]
602+
603+
@property
604+
def type(self) -> str:
605+
return "function"
606+
607+
@property
608+
def inputs(self) -> Optional[List]:
609+
return self.abi.get("inputs")
610+
611+
@property
612+
def outputs(self) -> Optional[List]:
613+
return self.abi.get("outputs")
614+
599615
@combomethod
600616
def _get_abi(cls) -> ABIFunction:
601617
if not cls.args and not cls.kwargs:

0 commit comments

Comments
 (0)