eth-abi
The `eth-abi` library, currently at version 5.2.0, provides low-level Python utilities for converting Python values to and from Solidity's binary Application Binary Interface (ABI) format. It is a core component within the Ethereum development ecosystem, offering functionalities for encoding Python values into ABI-compliant bytes and decoding ABI bytes back into Python values for smart contract interaction. The library maintains an active release cadence with frequent updates, including bug fixes, new features, and Python version support, with major versions typically released annually or biannually.
Warnings
- breaking Version 5.0.0 of `eth-abi` dropped support for Python 3.7. Users on older Python versions will need to upgrade their environment or stick to `eth-abi` versions < 5.0.0.
- breaking Upcoming version 6.0.0 (currently in beta) will drop support for Python 3.8 and 3.9. Plan for future Python environment upgrades if targeting this version.
- gotcha The `decode` function defaults to `strict=True`, which performs validations such as ensuring data is padded to a multiple of 32 bytes and that padding bytes are zero. However, Solidity's ABI decoder operates with a behavior equivalent to `strict=False`, which ignores some of these validations. This difference can lead to unexpected errors if not handled, particularly when decoding data from contracts that might not adhere strictly to padding rules.
- gotcha Understanding the Ethereum ABI specification is crucial for correctly using `eth-abi`. Complex types (like dynamic arrays, strings, and tuples) have specific encoding rules, including how their data is positioned (head/tail mechanism) and padded to 32-byte boundaries. Misunderstanding these rules can lead to incorrect encoding or decoding.
- gotcha Contract ABIs (Application Binary Interfaces) are not stored on the blockchain itself. To interact with a smart contract using `eth-abi` (or any web3 library), you need to obtain the contract's ABI from external sources. Common sources include block explorers (like Etherscan for verified contracts), the contract's official GitHub repository, or by compiling the contract's source code yourself.
Install
-
pip install eth-abi
Imports
- encode
from eth_abi import encode
- decode
from eth_abi import decode
- registry
from eth_abi import registry
Quickstart
from eth_abi import encode, decode
# Define ABI types and corresponding Python values
abi_types = ['uint256', 'address', 'bool', 'string']
python_values = [123, '0x5B38Da6a701c568545dCfcB03FcB875f56beddC4', True, 'Hello eth-abi']
# Encode Python values into ABI-compliant bytes
encoded_data = encode(abi_types, python_values)
print(f"Encoded data: {encoded_data.hex()}")
# Decode ABI-compliant bytes back into Python values
decoded_values = decode(abi_types, encoded_data)
print(f"Decoded values: {decoded_values}")
# Example with a simple uint256
encoded_uint = encode(['uint256'], [1])
decoded_uint = decode(['uint256'], b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')
print(f"Decoded single uint: {decoded_uint}")