Common utility functions for python code that interacts with Ethereum
npx @tessl/cli install tessl/pypi-eth-utils@5.3.0A comprehensive utility library providing essential functions for Python applications that interact with Ethereum blockchain and related technologies. It offers tools for address validation, ABI processing, data conversions, cryptographic operations, currency handling, and more.
pip install eth-utilsimport eth_utilsCommon imports for specific functionality:
from eth_utils import (
# Address handling
is_address, to_checksum_address, is_checksum_address,
# Data conversions
to_bytes, to_hex, to_int, to_text,
# ABI processing
function_signature_to_4byte_selector, event_signature_to_log_topic,
# Currency conversions
from_wei, to_wei, denoms,
# Hexadecimal utilities
encode_hex, decode_hex, is_hex,
# Type checking
is_integer, is_string, is_bytes
)from eth_utils import (
to_checksum_address, to_wei, from_wei,
keccak, to_bytes, encode_hex
)
# Address validation and formatting
address = "0xd3cda913deb6f67967b99d67acdfa1712c293601"
checksum_addr = to_checksum_address(address)
print(checksum_addr) # 0xd3CdA913deB6f67967B99D67aCDFa1712C293601
# Currency conversions
wei_amount = to_wei(1.5, 'ether') # Convert 1.5 ETH to wei
eth_amount = from_wei(wei_amount, 'ether') # Convert back to ETH
# Data conversions and hashing
data = to_bytes(text="Hello, Ethereum!")
hash_result = keccak(data)
hex_hash = encode_hex(hash_result)
# ABI function selector generation
from eth_utils import function_signature_to_4byte_selector
selector = function_signature_to_4byte_selector("transfer(address,uint256)")
print(encode_hex(selector)) # 0xa9059cbbeth-utils is organized into focused modules that address specific aspects of Ethereum development:
The library follows a zero-dependency core philosophy while providing optional extensions, ensuring minimal overhead and maximum compatibility across Python 3.8+ environments.
Comprehensive Ethereum address handling including validation, format conversion, and EIP-55 checksum processing. Supports all address formats: binary (20 bytes), hex, normalized, and checksummed.
def is_address(value) -> bool: ...
def to_checksum_address(value) -> str: ...
def is_checksum_address(value) -> bool: ...
def to_canonical_address(address) -> bytes: ...
def is_same_address(left, right) -> bool: ...Smart contract ABI manipulation including signature generation, function selectors, event topics, and parameter extraction. Essential for contract interaction and transaction processing.
def function_signature_to_4byte_selector(function_signature: str) -> bytes: ...
def event_signature_to_log_topic(event_signature: str) -> bytes: ...
def abi_to_signature(abi_element) -> str: ...
def filter_abi_by_type(abi_type: str, contract_abi) -> list: ...Flexible type conversion utilities supporting multiple input formats (primitive values, hex strings, text) with consistent output types. Core functionality for data transformation pipelines.
def to_bytes(primitive=None, hexstr=None, text=None) -> bytes: ...
def to_hex(primitive=None, hexstr=None, text=None) -> str: ...
def to_int(primitive=None, hexstr=None, text=None) -> int: ...
def to_text(primitive=None, hexstr=None, text=None) -> str: ...Ethereum currency conversions between wei and various denominations (ether, gwei, finney, etc.) with precise decimal handling and comprehensive denomination constants.
def to_wei(number, unit: str) -> int: ...
def from_wei(number: int, unit: str) -> Union[int, Decimal]: ...
class denoms:
wei: int
gwei: int
ether: int
...Hex string processing including encoding, decoding, prefix handling, and validation. Essential for working with Ethereum's hex-encoded data formats.
def encode_hex(value) -> str: ...
def decode_hex(value: str) -> bytes: ...
def is_hex(value) -> bool: ...
def add_0x_prefix(value: str) -> str: ...
def remove_0x_prefix(value: str) -> str: ...Comprehensive type validation utilities for common Python and Ethereum-specific data types. Provides reliable type checking for data validation pipelines.
def is_integer(value) -> bool: ...
def is_string(value) -> bool: ...
def is_bytes(value) -> bool: ...
def is_boolean(value) -> bool: ...
def is_list_like(obj) -> bool: ...Cryptographic utilities centered around Keccak-256 hashing with flexible input handling. Essential for Ethereum hash computations.
def keccak(primitive=None, hexstr=None, text=None) -> bytes: ...Advanced data transformation utilities including formatters, applicators, and functional programming tools for complex data processing workflows.
def apply_formatters_to_dict(formatters, value) -> dict: ...
def apply_formatter_if(condition, formatter, value): ...
def apply_key_map(key_mappings, value) -> dict: ...Enhanced logging capabilities with DEBUG2 level support and environment introspection tools for development and troubleshooting.
def get_logger(name: str) -> Logger: ...
def get_extended_debug_logger(name: str) -> ExtendedDebugLogger: ...
def get_environment_summary() -> str: ...Ethereum network data including chain IDs, network names, and metadata for all major Ethereum networks and testnets.
def network_from_chain_id(chain_id: int) -> Network: ...
def name_from_chain_id(chain_id: int) -> str: ...
class Network:
chain_id: int
name: str
shortName: str
symbol: strFunctional programming utilities including curried functions, return value transformers, and functional composition tools for advanced data processing patterns.
def apply_to_return_value(callback): ...
def to_tuple(func): ...
def flatten_return(func): ...
from eth_utils.curried import filter_abi_by_type, apply_formatter_ifeth-utils defines custom exceptions for validation failures:
class ValidationError(Exception):
"""Raised when validation of input data fails."""Most functions will raise ValidationError for invalid inputs, TypeError for incorrect types, and ValueError for values outside expected ranges.