compat

Documentation for eth_defi.compat Python module.

Web3.py compatibility module.

Provides helper functions and re-exports for web3.py v7.

Module Attributes

WEB3_PY_V7

Deprecated.

DEFAULT_RETRY_ALLOWLIST

Default allowlist for RPC methods that are safe to retry on failure

check_if_retry_on_failure_compat(method)

Kept for backwards compatibility

Functions

add_middleware(web3, middleware_func_or_name)

Add middleware to a Web3 instance.

check_if_retry_on_failure(method)

Check if an RPC method is in the retry allowlist.

check_if_retry_on_failure_compat(method)

Kept for backwards compatibility

clear_middleware(web3_or_provider)

Clear all middleware.

construct_sign_and_send_raw_middleware(...)

Wrapper for SignAndSendRawMiddlewareBuilder.

create_http_provider(*args, **kwargs)

Create an HTTPProvider instance.

encode_abi_compat(contract, fn_name, args)

Encode ABI for a contract function call.

encode_function_args(func, args)

Encode function arguments for a contract call.

exception_retry_middleware(make_request, ...)

Exception retry middleware.

get_function_info(*args, **kwargs)

Get function info from contract ABI.

install_api_call_counter_middleware_compat(web3)

Install API call counter middleware.

install_poa_middleware(web3[, layer])

Install proof-of-authority middleware.

install_retry_middleware_compat(web3[, layer])

Install retry middleware using provider-level ExceptionRetryConfiguration.

native_datetime_utc_fromtimestamp(timestamp)

Convert timestamp to naive UTC datetime object.

native_datetime_utc_now()

Get current UTC time as a naive datetime object.

Classes

APICallCounterMiddleware

Middleware that counts API calls per RPC method.

WEB3_PY_V7 = True

Deprecated. Always True. Kept for backwards compatibility with external consumers.

DEFAULT_RETRY_ALLOWLIST = ('eth_call', 'eth_getBalance', 'eth_getCode', 'eth_getTransactionCount', 'eth_getBlockByNumber', 'eth_getBlockByHash', 'eth_getLogs', 'eth_chainId', 'net_version', 'eth_blockNumber', 'eth_gasPrice', 'eth_estimateGas', 'eth_getTransactionByHash', 'eth_getTransactionReceipt')

Default allowlist for RPC methods that are safe to retry on failure

check_if_retry_on_failure(method)

Check if an RPC method is in the retry allowlist.

Parameters

method (str) –

Return type

bool

class APICallCounterMiddleware

Bases: web3.middleware.base.Web3Middleware

Middleware that counts API calls per RPC method.

__init__(w3, counter)
Parameters

counter (collections.Counter) –

install_api_call_counter_middleware_compat(web3)

Install API call counter middleware.

Returns

Counter instance tracking API calls

Parameters

web3 (web3.main.Web3) –

Return type

collections.Counter

add_middleware(web3, middleware_func_or_name, layer=0)

Add middleware to a Web3 instance.

Parameters
  • web3 (web3.main.Web3) – Web3 instance

  • middleware_func_or_name – Middleware class/instance or string name

  • layer (int) – Layer to inject at (default 0)

exception_retry_middleware(make_request, web3, retryable_exceptions, retryable_status_codes, retryable_rpc_error_codes, retries=10, sleep=5.0, backoff=1.6)

Exception retry middleware.

Consider using ExceptionRetryConfiguration on your provider instead.

Parameters
  • make_request (Callable[[web3.types.RPCEndpoint, Any], web3.types.RPCResponse]) –

  • web3 (web3.main.Web3) –

  • retries (int) –

  • sleep (float) –

  • backoff (float) –

Return type

Callable[[web3.types.RPCEndpoint, Any], web3.types.RPCResponse | None]

clear_middleware(web3_or_provider)

Clear all middleware.

Handles both Web3 instances and providers.

Parameters

web3_or_provider (web3.main.Web3 | web3.providers.rpc.rpc.HTTPProvider) –

Return type

None

install_poa_middleware(web3, layer=0)

Install proof-of-authority middleware.

Parameters
  • web3 (web3.main.Web3) –

  • layer (int) –

install_retry_middleware_compat(web3, layer=0)

Install retry middleware using provider-level ExceptionRetryConfiguration.

Parameters
  • web3 (web3.main.Web3) –

  • layer (int) –

encode_function_args(func, args)

Encode function arguments for a contract call.

get_function_info(*args, **kwargs)

Get function info from contract ABI.

Returns a tuple of (fn_abi, fn_selector, fn_args) compatible with the legacy v6 get_function_info interface.

encode_abi_compat(contract, fn_name, args)

Encode ABI for a contract function call.

Parameters
  • contract (web3.contract.contract.Contract) – Web3 contract instance

  • fn_name (str) – Function name to encode

  • args (list[Any]) – Arguments for the function

Returns

Encoded ABI string

Return type

eth_typing.encoding.HexStr

construct_sign_and_send_raw_middleware(private_key_or_account)

Wrapper for SignAndSendRawMiddlewareBuilder.

check_if_retry_on_failure_compat(method)

Kept for backwards compatibility

Parameters

method (str) –

Return type

bool

native_datetime_utc_now()

Get current UTC time as a naive datetime object.

Replacement for the deprecated datetime.datetime.utcnow().

Return type

datetime.datetime

native_datetime_utc_fromtimestamp(timestamp)

Convert timestamp to naive UTC datetime object.

Replacement for the deprecated datetime.datetime.utcfromtimestamp().

Parameters

timestamp (float) – Unix timestamp (seconds since epoch)

Return type

datetime.datetime

create_http_provider(*args, **kwargs)

Create an HTTPProvider instance.

Example:

@pytest.fixture()
def provider_1(anvil):
    provider = create_http_provider(anvil.json_rpc_url, exception_retry_configuration=None)
    clear_middleware(provider)
    return provider
Return type

web3.providers.rpc.rpc.HTTPProvider