A Python library for interacting with Ethereum blockchain
Overall
score
88%
Evaluation — 88%
↑ 1.01xAgent success when using this tile
The main client classes for connecting to Ethereum nodes, providing comprehensive blockchain interaction capabilities and module management.
Main synchronous client for Ethereum interaction with provider management, middleware support, and module attachment.
class Web3:
def __init__(
self,
provider: BaseProvider = None,
modules: Dict[str, Union[Type[Module], Sequence[Any]]] = None,
middleware: Optional[List[Middleware]] = None
):
"""
Initialize Web3 instance.
Parameters:
- provider: Connection provider (HTTPProvider, WebSocketProvider, etc.)
- modules: Custom modules to attach
- middleware: Middleware stack for request/response processing
"""
def is_connected(self) -> bool:
"""Check if connected to Ethereum node."""
@property
def eth(self) -> Eth:
"""Access Ethereum operations module."""
@property
def net(self) -> Net:
"""Access network operations module."""
@property
def geth(self) -> Geth:
"""Access Geth-specific operations module."""
@property
def manager(self) -> RequestManager:
"""Access request manager for low-level operations."""
@property
def middleware_onion(self) -> MiddlewareOnion:
"""Access middleware management."""
@property
def provider(self) -> BaseProvider:
"""Access current provider."""
def enable_unstable_package_management_api(self) -> None:
"""Enable package management functionality."""
def to_wei(self, number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei:
"""Convert number to Wei."""
def from_wei(self, number: Wei, unit: str) -> decimal.Decimal:
"""Convert Wei to specified unit."""
def to_checksum_address(self, value: AnyAddress) -> ChecksumAddress:
"""Convert address to checksum format."""
def is_address(self, value: Any) -> bool:
"""Check if value is valid Ethereum address."""
def is_checksum_address(self, value: Any) -> bool:
"""Check if address is in checksum format."""
def keccak(
self,
primitive: Primitives = None,
text: str = None,
hexstr: HexStr = None
) -> HexBytes:
"""Compute Keccak-256 hash."""
def solidity_keccak(self, abi_types: List[str], values: List[Any]) -> HexBytes:
"""Compute Solidity-compatible Keccak hash."""
def encode_abi(self, types: List[str], args: List[Any]) -> HexBytes:
"""Encode data according to ABI specification."""
def decode_abi(self, types: List[str], data: HexBytes) -> List[Any]:
"""Decode ABI-encoded data."""
def to_bytes(
self,
primitive: Primitives = None,
hexstr: HexStr = None,
text: str = None
) -> bytes:
"""Convert primitive to bytes."""
def to_int(
self,
primitive: Primitives = None,
hexstr: HexStr = None,
text: str = None
) -> int:
"""Convert primitive to integer."""
def to_hex(
self,
primitive: Primitives = None,
hexstr: HexStr = None,
text: str = None
) -> HexStr:
"""Convert primitive to hex string."""
def to_text(
self,
primitive: Primitives = None,
hexstr: HexStr = None,
text: str = None
) -> str:
"""Convert primitive to text string."""
def to_json(self, obj: Dict[Any, Any]) -> str:
"""Convert object to JSON string."""
def normalize_values(
self,
abi_types: List[str],
values: List[Any]
) -> List[Any]:
"""Normalize values according to ABI types."""
def is_encodable(self, _type: str, value: Any) -> bool:
"""Check if value is encodable for given type."""
def attach_modules(
self,
modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]]
) -> None:
"""Attach modules to Web3 instance."""
def batch_requests(self) -> RequestBatcher:
"""Create batch request context manager."""Asynchronous client providing the same interface as Web3 with async/await support.
class AsyncWeb3:
def __init__(
self,
provider: AsyncBaseProvider = None,
modules: Dict[str, Union[Type[Module], Sequence[Any]]] = None,
middleware: Optional[List[Middleware]] = None
):
"""
Initialize AsyncWeb3 instance.
Parameters:
- provider: Async connection provider
- modules: Custom modules to attach
- middleware: Middleware stack for request/response processing
"""
async def is_connected(self) -> bool:
"""Check if connected to Ethereum node."""
@property
def eth(self) -> AsyncEth:
"""Access async Ethereum operations module."""
@property
def net(self) -> AsyncNet:
"""Access async network operations module."""
@property
def geth(self) -> AsyncGeth:
"""Access async Geth-specific operations module."""
@property
def manager(self) -> RequestManager:
"""Access request manager for low-level operations."""
@property
def middleware_onion(self) -> MiddlewareOnion:
"""Access middleware management."""
@property
def provider(self) -> AsyncBaseProvider:
"""Access current async provider."""
def to_wei(self, number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei:
"""Convert number to Wei."""
def from_wei(self, number: Wei, unit: str) -> decimal.Decimal:
"""Convert Wei to specified unit."""
def to_checksum_address(self, value: AnyAddress) -> ChecksumAddress:
"""Convert address to checksum format."""
def is_address(self, value: Any) -> bool:
"""Check if value is valid Ethereum address."""
def is_checksum_address(self, value: Any) -> bool:
"""Check if address is in checksum format."""
def keccak(
self,
primitive: Primitives = None,
text: str = None,
hexstr: HexStr = None
) -> HexBytes:
"""Compute Keccak-256 hash."""
def solidity_keccak(self, abi_types: List[str], values: List[Any]) -> HexBytes:
"""Compute Solidity-compatible Keccak hash."""
def encode_abi(self, types: List[str], args: List[Any]) -> HexBytes:
"""Encode data according to ABI specification."""
def decode_abi(self, types: List[str], data: HexBytes) -> List[Any]:
"""Decode ABI-encoded data."""def get_default_modules() -> Dict[str, Union[Type[Module], Sequence[Any]]]:
"""Get default modules attached to Web3 instance."""
def get_async_default_modules() -> Dict[str, Union[Type[Module], Sequence[Any]]]:
"""Get default modules attached to AsyncWeb3 instance."""from web3 import Web3
# HTTP connection
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR-PROJECT-ID'))
# Check connection
if w3.is_connected():
print("Connected to Ethereum mainnet")
print(f"Latest block: {w3.eth.block_number}")from web3 import Web3
from web3.middleware import geth_poa_middleware
w3 = Web3(Web3.HTTPProvider('http://localhost:8545'))
# Add Proof of Authority middleware for private networks
w3.middleware_onion.inject(geth_poa_middleware, layer=0)import asyncio
from web3 import AsyncWeb3
async def main():
w3 = AsyncWeb3(AsyncWeb3.AsyncHTTPProvider('https://mainnet.infura.io/v3/YOUR-PROJECT-ID'))
if await w3.is_connected():
block_number = await w3.eth.block_number
print(f"Latest block: {block_number}")
asyncio.run(main())Install with Tessl CLI
npx tessl i tessl/pypi-web3docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10