Skip to content

Commit 90cb12c

Browse files
authored
Function ABI Utilities (#3408)
* Parse and validate contract data using new ``ABI`` utilities. * Filter elements in a contract's ``ABI``. * Extract function and event ``ABI`` attributes from a contract. * Validate arguments passed to a function match the types in the ``ABI`` * Refactor logic and variable naming - Replace arguments named ``fn_name`` with ``abi_element_name`` - Rename `FunctionIdentifier` to `ABIElementIdentifier` - Remove redundant logic and `Optional` types - Integrate new ABI types in ``eth-typing``. * Newsfragments for #3408
1 parent 4a5d568 commit 90cb12c

39 files changed

+1761
-1213
lines changed

docs/migration.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ web3.py. This allows a user to distinguish between when a method is not availabl
195195
the current provider, ``MethodUnavailable``, and when a method is not supported by
196196
web3.py under certain conditions, ``MethodNotSupported``.
197197

198+
A ``MismatchedABI`` exception is now raised instead of a ``Web3ValidationError`` in
199+
cases where an ABI is not compatible with the data being passed to it. This change
200+
allows for more specific error handling when using certain ABI types.
201+
198202

199203
JSON-RPC Error Handling
200204
```````````````````````
@@ -252,6 +256,26 @@ The ``personal`` namespace was used for managing accounts and keys and was depre
252256
in geth in ``v1.11.0``. Geth has moved to using ``clef`` for account and key management.
253257

254258

259+
ABI Types Removed
260+
`````````````````
261+
262+
The type definitions for ABIs, deprecated in ``v6``, have been removed in ``v7``. New
263+
types have been introduced in the ``eth_typing`` ``v5`` package for ABIs. Improvements have
264+
been made to make required types more explicit and to offer better semantics.
265+
266+
The following types from ``web3.types`` have been removed:
267+
- ``ABIEventParams`` is no longer avaiable. Use ``ABIComponentIndexed`` from
268+
``eth_typing`` to represent event input components.
269+
- ``ABIEvent`` now resides in ``eth_typing``. ``ABIEvent.type`` and ``ABIEvent.name``
270+
are now required fields.
271+
- ``ABIFunctionComponents`` and ``ABIFunctionParams`` are no longer available. Use
272+
``ABIComponent`` from ``eth_typing`` to represent function input components.
273+
- ``ABIFunction`` now resides in ``eth_typing``. ``ABIFunction.type`` and
274+
``ABIFunction.name`` are now required fields.
275+
- ``ABIElement`` now resides in ``eth_typing`` and represents a ``Union`` of all valid
276+
ABI element types, ``ABICallable``, ``ABIEvent`` and ``ABIError``.
277+
278+
255279
Miscellaneous Changes
256280
~~~~~~~~~~~~~~~~~~~~~
257281

@@ -262,13 +286,18 @@ Miscellaneous Changes
262286
- ``User-Agent`` header was changed to a more readable format.
263287
- ``BaseContractFunctions`` iterator now returns instances of ``ContractFunction`` rather
264288
than the function names.
289+
- ``BaseContractFunction`` class attribute ``function_identifier`` has been removed in
290+
favor of the ``abi_element_identifier`` attribute.
291+
- ``web3.contract.utils.call_contract_function()`` no longers uses ``fn_abi`` as a
292+
parameter. Instead, the ``abi_callable`` parameter of type ``ABICallable`` is used.
265293
- Beacon API filename change: ``beacon/main.py`` -> ``beacon/beacon.py``.
266294
- The asynchronous version of ``w3.eth.wait_for_transaction_receipt()`` changes its
267295
signature to use ``Optional[float]`` instead of ``float`` since it may be ``None``.
268296
- ``get_default_ipc_path()`` and ``get_dev_ipc_path()`` now return the path value
269297
without checking if the ``geth.ipc`` file exists.
270298
- ``Web3.is_address()`` returns ``True`` for non-checksummed addresses.
271-
- ``Contract.encodeABI()`` has been renamed to ``Contract.encode_abi()``.
299+
- ``Contract.encodeABI()`` has been renamed to ``Contract.encode_abi()``. The ``fn_name``
300+
argument has been changed to ``abi_element_name``.
272301
- JSON-RPC responses are now more strictly validated against the JSON-RPC 2.0
273302
specification while providing more informative error messages for invalid responses.
274303

docs/web3.contract.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ Taking the following contract code as an example:
625625
>>> arrays_contract.functions.setBytes2Value([b'b']).transact()
626626
Traceback (most recent call last):
627627
...
628-
web3.exceptions.Web3ValidationError:
628+
web3.exceptions.MismatchedABI:
629629
Could not identify the intended function with name
630630
>>> # check value is still b'aa'
631631
>>> arrays_contract.functions.getBytes2Value().call()
@@ -650,7 +650,7 @@ Taking the following contract code as an example:
650650
>>> arrays_contract.functions.setBytes2Value([b'a']).transact()
651651
Traceback (most recent call last):
652652
...
653-
web3.exceptions.Web3ValidationError:
653+
web3.exceptions.MismatchedABI:
654654
Could not identify the intended function with name
655655

656656
.. _contract-functions:

docs/web3.utils.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ The ``utils`` module houses public utility functions and classes.
88
ABI
99
---
1010

11-
.. py:method:: utils.get_abi_input_names(abi)
12-
13-
Return the ``input`` names for an ABI function or event.
14-
15-
16-
.. py:method:: utils.get_abi_output_names(abi)
17-
18-
Return the ``output`` names an ABI function or event.
11+
.. automodule:: web3.utils.abi
12+
:members:
13+
:undoc-members:
14+
:show-inheritance:
1915

2016
Address
2117
-------

ens/base_ens.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
from eth_typing import (
1212
ChecksumAddress,
1313
)
14+
from eth_utils.abi import (
15+
get_abi_output_types,
16+
)
1417
from hexbytes import (
1518
HexBytes,
1619
)
1720

1821
from .utils import (
1922
address_to_reverse_domain,
20-
get_abi_output_types,
2123
is_valid_name,
2224
label_to_hash,
2325
normalize_name,

ens/utils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
TYPE_CHECKING,
77
Any,
88
Collection,
9-
Dict,
10-
List,
119
Optional,
1210
Sequence,
1311
Tuple,
@@ -28,9 +26,6 @@
2826
to_bytes,
2927
to_normalized_address,
3028
)
31-
from eth_utils.abi import (
32-
collapse_if_tuple,
33-
)
3429
from hexbytes import (
3530
HexBytes,
3631
)
@@ -68,9 +63,6 @@
6863
AsyncBaseProvider,
6964
BaseProvider,
7065
)
71-
from web3.types import ( # noqa: F401
72-
ABIFunction,
73-
)
7466

7567

7668
def Web3() -> Type["_Web3"]:
@@ -293,15 +285,6 @@ def is_valid_ens_name(ens_name: str) -> bool:
293285
return True
294286

295287

296-
# borrowed from similar method at `web3._utils.abi` due to circular dependency
297-
def get_abi_output_types(abi: "ABIFunction") -> List[str]:
298-
return (
299-
[]
300-
if abi["type"] == "fallback"
301-
else [collapse_if_tuple(cast(Dict[str, Any], arg)) for arg in abi["outputs"]]
302-
)
303-
304-
305288
# -- async -- #
306289

307290

newsfragments/3408.breaking.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Upgrades to use latest ``ABI`` utilities and typings from ``eth-utils`` and ``eth-typing``.
2+
* Typings for ``ABI`` components are now available in the ``eth-typing`` package. ``ABI`` types previously in ``web3.types`` have been removed.
3+
* New versions of existing ABI functions were added to ``eth-utils`` and are now exposed in `web3.py` via ``web3.utils.abi``.
4+
* ABI exceptions have been renamed in ``web3.exceptions``. The ``ABIEventFunctionNotFound`` and ``FallbackNotFound`` exceptions have been removed. Use ``ABIEventNotFound`` and ``ABIFallbackNotFound`` instead.
5+
* ``MismatchedABI`` exceptions are raised instead of a ``Web3ValidationError`` for ABI related errors.
6+
* ``encode_abi`` arguments have been updated to use ``abi_element_name`` instead of ``fn_name``.

newsfragments/3408.docs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use autodoc and update ABI functions with docstrings and doctests.

newsfragments/3408.feature.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Utilities to extract function and event ``ABI`` attributes from a contract. Utilities in the ``web3.utils.abi`` module parse ABI elements and check encodability of provided arguments. ABI functions in ``eth-utils`` are exposed by the ``web3.utils.abi`` module.
2+
* ``get_abi_element_info`` returns an ``ABIElementInfo`` TypedDict with the ``abi``, ``selector``, and ``arguments``.
3+
* ``get_abi_element`` returns the ``ABI`` of a function, event, or error given the name and arguments.
4+
* ``check_if_arguments_can_be_encoded`` returns true if the arguments can be encoded with the given ABI.
5+
* ``get_event_abi`` returns the ``ABI`` of an event given the name.
6+
* ``get_event_log_topics`` returns the log topics of an event given the name.
7+
* ``log_topics_to_bytes`` returns the log topics as bytes.

newsfragments/3408.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move ``get_abi_input_names`` and ``get_abi_output_names`` to ``eth-utils`` and re-export in ``web3.utils.abi``.

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
"eth-abi>=5.0.1",
6565
"eth-account>=0.12.2",
6666
"eth-hash[pycryptodome]>=0.5.1",
67-
"eth-typing>=4.0.0",
68-
"eth-utils>=4.0.0",
67+
"eth-typing>=5.0.0b1",
68+
"eth-utils>=5.0.0b1",
6969
"hexbytes>=1.2.0",
7070
"pydantic>=2.4.0",
7171
"pywin32>=223;platform_system=='Windows'",

0 commit comments

Comments
 (0)