ETHHelper Websocket Connectors

Note

All interfaces in this repo rely on the asynchronous environment, so the asyncio environment is required when using the REPL for interactive coding. This can be done by running python -m asyncio directly on the command line or shell (instead of python).

Websocket Low-level API

GethSubscriber

class ethhelper.connectors.ws.GethSubscriber(url: str, logger: Logger)

Abstract base class for implementing a subscriber to a Geth node using websockets with the Geth JSON-RPC API.

This class defines the basic structure for implementing a subscriber to a Geth node using websockets. It provides a method for binding to the node, sending requests, handling responses, and closing the connection.

The url is used to indicate the path of the WS service of the Geth node, usually in the form of http://host:port/. The use of third-party nodes may be out of the ordinary.

The logger is used to assign a logger of the Python logging module to this class. Explicitly assigning a logger can be used to control the output location of the logger, which is convenient for debugging.

async bind() Task[None]

Bind the subscriber to the Geth node and start listening for messages.

Returns:

A task that will run the subscriber until it is closed.

async run() None

The main loop that listens for messages from the Geth node.

async send(method: str, params: list[Any]) int

Send a request to the Geth node over the websocket connection.

Parameters:
  • method – The JSON-RPC method to call.

  • params – The parameters to send with the request.

Returns:

The ID of the request, which can be used to match responses with requests.

async subscribe(param: str) int

Subscribe to a specific event or method on the Geth node.

Parameters:

param – The name of the event or method to subscribe to.

Returns:

The ID of the subscription, which can be used to unsubscribe later.

abstract async after_connection() None

A method that is called after the connection to the Geth node has been established.

This method can be overridden to perform any necessary setup or initialization after the connection to the Geth node has been established.

abstract async handle(data: GethWSResponse | GethSuccessResponse) None

A method that is called when a message is received from the Geth node.

This method must be overridden to handle the messages received from the Geth node.

Parameters:

data – The message received from the Geth node.

async close() None

Close the connection to the Geth node.