or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

abi-processing.mdaddress-operations.mdcrypto-functions.mdcurrency-units.mddata-conversions.mddata-formatting.mdfunctional-programming.mdhexadecimal-utilities.mdindex.mdlogging-debugging.mdnetwork-information.mdtype-checking.md
tile.json

tessl/pypi-eth-utils

Common utility functions for python code that interacts with Ethereum

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/eth-utils@5.3.x

To install, run

npx @tessl/cli install tessl/pypi-eth-utils@5.3.0

index.mddocs/

eth-utils

A 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.

Package Information

  • Package Name: eth-utils
  • Language: Python
  • Installation: pip install eth-utils
  • Version: 5.3.1
  • License: MIT

Core Imports

import eth_utils

Common 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
)

Basic Usage

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))  # 0xa9059cbb

Architecture

eth-utils is organized into focused modules that address specific aspects of Ethereum development:

  • Core Data Types: Address, hex string, and byte handling with format validation
  • ABI Processing: Smart contract interface manipulation and signature generation
  • Type System: Comprehensive type checking and conversion utilities
  • Functional Patterns: Curried functions and applicators for data transformation
  • Debugging Tools: Environment introspection and development utilities

The library follows a zero-dependency core philosophy while providing optional extensions, ensuring minimal overhead and maximum compatibility across Python 3.8+ environments.

Capabilities

Address Operations

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: ...

Address Operations

ABI Processing

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: ...

ABI Processing

Data Conversions

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: ...

Data Conversions

Currency and Units

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
    ...

Currency and Units

Hexadecimal Utilities

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: ...

Hexadecimal Utilities

Type Checking

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: ...

Type Checking

Cryptographic Functions

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: ...

Cryptographic Functions

Data Formatting

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: ...

Data Formatting

Logging and Debugging

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: ...

Logging and Debugging

Network Information

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: str

Network Information

Functional Programming

Functional 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_if

Functional Programming

Error Handling

eth-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.