compat
Documentation for eth_defi.compat Python module.
Web3.py compatibility module.
Provides helper functions and re-exports for web3.py v7.
Module Attributes
Deprecated. |
|
Default allowlist for RPC methods that are safe to retry on failure |
|
|
Kept for backwards compatibility |
Functions
|
Add middleware to a Web3 instance. |
|
Check if an RPC method is in the retry allowlist. |
|
Kept for backwards compatibility |
|
Clear all middleware. |
Wrapper for SignAndSendRawMiddlewareBuilder. |
|
|
Create an HTTPProvider instance. |
|
Encode ABI for a contract function call. |
|
Encode function arguments for a contract call. |
|
Exception retry middleware. |
|
Get function info from contract ABI. |
Install API call counter middleware. |
|
|
Install proof-of-authority middleware. |
|
Install retry middleware using provider-level ExceptionRetryConfiguration. |
|
Convert timestamp to naive UTC datetime object. |
Get current UTC time as a naive datetime object. |
Classes
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.
- class APICallCounterMiddleware
Bases:
web3.middleware.base.Web3MiddlewareMiddleware 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
- 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.
- 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
- Returns
Encoded ABI string
- Return type
- construct_sign_and_send_raw_middleware(private_key_or_account)
Wrapper for SignAndSendRawMiddlewareBuilder.
- check_if_retry_on_failure_compat(method)
Kept for backwards compatibility
- native_datetime_utc_now()
Get current UTC time as a naive datetime object.
Replacement for the deprecated datetime.datetime.utcnow().
- Return type
- 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
- 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