Skip to main content

Lido Web3 Multi Provider

Code style: black License: MIT

Provider that accepts multiple endpoints.

Install

$ pip install web3-multi-provider

or

$ poetry add web3-multi-provider

or with metrics:

$ poetry add web3-multi-provider[metrics]

Usage

from web3 import Web3
from web3_multi_provider import MultiProvider
from web3_multi_provider import FallbackProvider

w3 = Web3(MultiProvider([  # RPC endpoints list
    'http://127.0.0.1:8000/',
    'https://mainnet.infura.io/v3/...',
]))

# or

w3 = Web3(FallbackProvider([  # RPC endpoints list
    'http://127.0.0.1:8000/',
    'https://mainnet.infura.io/v3/...',
]))

last_block = w3.eth.get_block('latest')

MultiProvider

This provider keeps track of the current endpoint and switches to the next one if an error occurs. It fails if no endpoints are available.

FallbackProvider

This provider sends requests to the all endpoints in the sequence until response received or endpoints list exhausted.

A failing endpoint is slow to fail over from: web3 retries it before the next endpoint is tried. Pass cooldown_seconds to try an endpoint that failed after the other endpoints until its cooldown ends. It is still tried if the other endpoints fail. When the cooldown ends, one request tries the endpoint first again; concurrent requests keep it last until that request finishes. If it fails, a new cooldown starts. An endpoint that answers, including one tried last, is first again at once. The default 0 disables the cooldown.

Any exception from an endpoint starts its cooldown, including errors that the request itself causes, such as a timeout or HTTP 413 on a heavy eth_getLogs. A JSON-RPC error in an HTTP 200 response (for example, a rate limit reported as error -32005) is a valid answer and does not start a cooldown.

w3 = Web3(FallbackProvider([
    'http://127.0.0.1:8000/',
    'https://mainnet.infura.io/v3/...',
], cooldown_seconds=60))

AsyncMultiProvider and AsyncFallbackProvider

These providers are async versions of MultiProvider and FallbackProvider respectively. They may be used with instances of AsyncWeb3.

from web3 import AsyncWeb3
from web3_multi_provider import AsyncMultiProvider

w3 = AsyncWeb3(AsyncMultiProvider([  # RPC endpoints list
    'http://127.0.0.1:8000/',
    'https://mainnet.infura.io/v3/...',
]))

Metrics

The library has built-in Prometheus metrics. They are disabled by default — install the extra and call init_metrics() before creating any provider.

pip install web3-multi-provider[metrics]
from web3 import Web3
from web3_multi_provider import MultiProvider, init_metrics

init_metrics()  # must be called before creating a provider

w3 = Web3(MultiProvider([
    'http://127.0.0.1:8000/',
    'https://mainnet.infura.io/v3/...',
]))

init_metrics() registers the following metrics with the default Prometheus registry:

Metric Type Description
http_rpc_requests Counter Total HTTP requests by network, provider, response code, result
http_rpc_batch_size Histogram Distribution of batch sizes
http_rpc_response_seconds Histogram RPC response time
http_rpc_request_payload_bytes Histogram Request payload size
http_rpc_response_payload_bytes Histogram Response payload size
rpc_request Counter RPC calls by method and result

Custom configuration

Use MetricsConfig to set a namespace or override the chain ID → network name mapping:

from web3_multi_provider.metrics import MetricsConfig

init_metrics(MetricsConfig(
    namespace="myapp",
    chain_id_to_name={
        1: "ethereum",
        137: "polygon",
        # add any custom chains here
    },
))

Custom registry

Pass a custom Prometheus registry if you don't want to use the global one:

from prometheus_client import CollectorRegistry
from web3_multi_provider import init_metrics

registry = CollectorRegistry()
init_metrics(registry=registry)

For developers

This project requires a specific Poetry version, pinned in .poetry-version. To install it:

POETRY_VERSION=$(cat .poetry-version) curl -sSL https://install.python-poetry.org/ | python -

Requirements

Setup

devcontainer up --workspace-folder .
devcontainer exec --workspace-folder . bash

This installs all dependencies and pre-commit hooks automatically via postCreateCommand.

Alternatively, without Docker:

  1. poetry install - to install deps
  2. pre-commit install - to install pre-commit hooks

Tests

poetry run pytest tests

Metadata

Release files for web3-multi-provider 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for web3-multi-provider 2.3.0
File Size Uploaded Provenance Actions
web3_multi_provider-2.3.0.tar.gz 19.3 kB Without provenance Details for web3_multi_provider-2.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for web3-multi-provider 2.3.0
File Interpreter ABI Platform Actions
web3_multi_provider-2.3.0-py3-none-any.whl Python 3 none any Details for web3_multi_provider-2.3.0-py3-none-any.whl

Total release size: 43.2 kB

Release files / web3_multi_provider-2.3.0.tar.gz

Download URL web3_multi_provider-2.3.0.tar.gz
Size 19.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0ad363ecb0ff427a9557979de41b4555a5e1447ba30bdace59e27fc78199f83a
BLAKE2b-256 checksum
How to use checksums
ed4c5f37da8376a22ed03161edd1439313516fd9ebd34ed5c8ff4eb045128752
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / web3_multi_provider-2.3.0-py3-none-any.whl

Download URL web3_multi_provider-2.3.0-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7722a5b315d9f48f08048e74e179222573f7d56dd7ad206c8bba44a8e67edaf7
BLAKE2b-256 checksum
How to use checksums
32850ba92078a74af6a29f6b0718e29c9d806e8c26db961cb66c0878c875944e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.16

Security for web3-multi-provider 2.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page