Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilities for Contract ABIs #271

Open
wants to merge 57 commits into
base: main
Choose a base branch
from
Open

Conversation

reedsa
Copy link
Contributor

@reedsa reedsa commented Apr 10, 2024

What was wrong?

Contract ABI utilities are useful for encoding and decoding contract transactions. Many utilities are implemented in web3.py as private methods.

Related to ethereum/web3.py#3036

These changes will be utilized in web3.py function/event utils:
ethereum/web3.py#3408
ethereum/web3.py#3401

How was it fixed?

Implements a public API for ABI utilities.

Todo:

  • Clean up commit history

  • Add or update documentation related to these changes

  • Add entry to the release notes

Cute Animal Picture

Screen Shot 2024-04-12 at 3 16 35 PM

eth_utils/abi.py Outdated Show resolved Hide resolved
eth_utils/abi.py Outdated Show resolved Hide resolved
Copy link
Contributor

@kclowes kclowes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new test functions, very nice! I didn't go through with a fine tooth comb, but looks good overall!

The only outstanding things I saw are:

  • the comment around which exceptions to raise, and where they live
  • I left a comment about raising vs returning an empty list. let me know what you think!

eth_utils/abi.py Outdated Show resolved Hide resolved
eth_utils/abi.py Outdated Show resolved Hide resolved
reedsa added a commit to reedsa/eth-utils that referenced this pull request Apr 12, 2024
eth_utils/abi.py Outdated
if function_identifier is None or not is_text(function_identifier):
raise TypeError("Unsupported function identifier")

if abi_codec is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where and how to document this feature. Maybe the docstring should point to the codec docs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me.

reedsa added a commit to reedsa/eth-utils that referenced this pull request Apr 15, 2024
reedsa added a commit to reedsa/eth-utils that referenced this pull request Apr 17, 2024
reedsa added a commit to reedsa/eth-utils that referenced this pull request Apr 18, 2024
reedsa added a commit to reedsa/eth-utils that referenced this pull request Apr 22, 2024
@reedsa reedsa marked this pull request as ready for review April 26, 2024 20:49
@reedsa reedsa requested review from pacrob and kclowes April 26, 2024 21:12
reedsa added a commit to reedsa/eth-utils that referenced this pull request Apr 29, 2024
@reedsa reedsa force-pushed the abi-element-utils branch 2 times, most recently from adfc679 to 854a6a5 Compare April 29, 2024 18:00
pyproject.toml Show resolved Hide resolved
eth_utils/abi.py Outdated Show resolved Hide resolved
eth_utils/abi.py Outdated Show resolved Hide resolved
@pacrob
Copy link
Contributor

pacrob commented May 1, 2024

Usage question - I pulled an ABI from web3 (extended_resolver.py), use get_all_function_abis to get function info, then take a name from that, say supportsInterface. From the description, I should be able to plug the abi and the function name into get_function_abi and get the same info back about that function, but I get:

MismatchedABI: 
Could not identify the intended function with name `supportsInterface`, positional arguments with type(s) `` and keyword arguments with type(s) `{}`.
Found 1 function(s) with the name `supportsInterface`: ['supportsInterface(bytes4)']
Function invocation failed due to improper number of arguments.

Same for get_function_info.

@reedsa
Copy link
Contributor Author

reedsa commented May 22, 2024

This is still a WIP. Keeping the ABI methods but codec utils will move into web3.utils.abi.

* Utility moved to `web3.utils.abi` module
* Expose `get_normalized_abi_inputs` and `get_aligned_abi_inputs`
* Docs improvements for `get_aligned_abi_inputs`
Doctests added to the following:
`collapse_if_tuple`
`function_signature_to_4byte_selector`
`function_abi_to_4byte_selector`
`event_signature_to_log_topic`
`event_abi_to_log_topic`
`get_normalized_abi_arg_type`
Copy link
Contributor

@kclowes kclowes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple small comments, but nothing major!

eth_utils/abi.py Outdated Show resolved Hide resolved
eth_utils/abi.py Outdated Show resolved Hide resolved
"""
warnings.warn(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a warning in the doctests about line 94 where you use collapse_if_tuple

setup.py Outdated Show resolved Hide resolved
Remove unused `_filter_by_encodability` method
Remove `_abi_to_signature` and related methods
Move `abi_to_signature` with public methods.
Doctest for `abi_to_signature`
Fix doctest in `collapse_if_tuple`
@reedsa
Copy link
Contributor Author

reedsa commented May 30, 2024

Cleaning up a few more items on this and I will request reviews when it's good to go!

@reedsa reedsa removed the request for review from fselmo May 30, 2024 17:39
@reedsa reedsa requested review from fselmo, kclowes and pacrob May 30, 2024 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants