event_reader.web3factory

Documentation for eth_defi.event_reader.web3factory Python module.

Web3 connection factory.

Methods for creating Web3 connections over multiple threads and processes.

Classes

SimpleWeb3Factory

Single reusable Web3 connection.

TunedWeb3Factory

Create a Web3 connections.

Web3Factory

Create a new Web3 connection.

class Web3Factory

Bases: Protocol

Create a new Web3 connection.

  • Web3 connection cannot be passed across thread/process boundaries

  • Help to setup TCP/IP connections and Web3 instance over it in threads and processes

  • When each worker is initialised, the factory is called to get JSON-RPC connection

See Python documentation regarding typing.Protocol.

__init__(*args, **kwargs)
class TunedWeb3Factory

Bases: eth_defi.event_reader.web3factory.Web3Factory

Create a Web3 connections.

A factory that allows us to pass web3 connection creation method across thread and process bounderies.

  • Disable AttributedDict middleware and other middleware that slows us down

  • Enable graceful retries in the case of network errors and API throttling

  • Use faster ujson instead of stdlib json to decode the responses

Set up a factory.

Parameters
  • rpc_config_line

    JSON-RPC config line.

    See eth_defi.provider.multi_provider.

  • http_adapter

    Connection pooling for HTTPS.

    Parameters for requests library. Default to pool size 10.

  • thread_local_cache

    Construct the web3 connection only once per thread.

    If you are using thread pooling, recycles the connection across different factory calls.

  • api_counter – Enable API counters

__init__(rpc_config_line, http_adapter=None, thread_local_cache=False, api_counter=False)

Set up a factory.

Parameters
  • rpc_config_line (str) –

    JSON-RPC config line.

    See eth_defi.provider.multi_provider.

  • http_adapter (Optional[requests.adapters.HTTPAdapter]) –

    Connection pooling for HTTPS.

    Parameters for requests library. Default to pool size 10.

  • thread_local_cache

    Construct the web3 connection only once per thread.

    If you are using thread pooling, recycles the connection across different factory calls.

  • api_counter – Enable API counters

get_total_api_call_counts()

Sum API call counts across all threads

Return type

collections.Counter

class SimpleWeb3Factory

Bases: object

Single reusable Web3 connection.

  • Does not work for multithreaded use cases, because Web3 object with TCP/IP connection is not passable across thread or process boundaries

  • Useful for testing

__init__(web3)
Parameters

web3 (web3.main.Web3) –