A Python aiohttp wrapper client to subscribe to the Ethereum JSON-RPC PubSub endpoints.
Project description
Sub3
A Python aiohttp wrapper client to subscribe to the Ethereum JSON-RPC PubSub endpoints.
Installation
Sub3 can be installed using pip as follows:
$ pip install sub3
Usage
Refer to the full documentation for the in-depth usage.
Prerequisite
The URL to a node that has a WebSocket port opened. This can be either from a provider like infura or through a self-hosted Geth/Nethermind node.
Create a basic subscription
The basic subscription will just print the data as received. It can be tested by doing the following:
>>> from sub3 import RPCMaker, Sub3
#RPCMaker is for easy formatting of JSON-RPC calls
>>> rpc = RPCMaker.new_heads()
>>> sub = Sub3("ws://localhost:8546", rpc)
>>> sub.start()
#Connection to the node
connected
#Successful answer from the JSONRPC request
{"jsonrpc":"2.0","id":"1","result":"0x1aaa6ce63bae0597ceadd723fd05e6db"}
#Starts receiving data
{
"jsonrpc":"2.0",
"method":"eth_subscription",
"params":{"subscription":"0x1aaa6ce63bae0597ceadd723fd05e6db",
"result":{"parentHash":"0x65906581" [...]}
}
Sub-classing
The data processing can easily be customized by sub-classing the Sub3 class like so:
from sub3 import Sub3
class NewClient(Sub3):
async def on_data(self, data):
# add your own data processing logic
async def on_closed(self, error):
# add your processing of `closed` message
async def on_error(self, error):
# add your processing of `error` message
rpc = RPCMaker.new_heads()
sub = NewClient("ws://localhost:8546", rpc)
sub.start()
Donate
I made this has a fun side project and it’s free for anyone to use. If you like it and wish to donate here’s a few of my crypto wallets.
Ethereum and L2s (0x29006…) |
Monero (85tBS7YSrM5…) |
Peercoin (PBzj1ZwMDW…) |
---|---|---|
Free software: MIT
Documentation: https://Sub3.readthedocs.io.
⊂(▀¯▀⊂)
History
0.0.3 (2022-07-09)
First release on PyPI.
1.0.0 (2022-07-09)
First automated release on Pypi
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Sub3-1.0.0.tar.gz
.
File metadata
- Download URL: Sub3-1.0.0.tar.gz
- Upload date:
- Size: 35.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81d3f01b5dab37e33547183433c676b6c52ebf41cfba85a4adab1bdb9f5e73b4 |
|
MD5 | a37716ba81477477da1770081398d376 |
|
BLAKE2b-256 | d7f0379700855a5c2d0f82c1ebf684cc1ced1813728d3cbc9360a151e6a2644f |
File details
Details for the file Sub3-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: Sub3-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1a1c70932dbc833bc9674e4bcc3297860766ba77d41edb6cdb5179ce4176d4e |
|
MD5 | c831b38801d05209ea1fc8eed6bf5d54 |
|
BLAKE2b-256 | b8d6f8d0bc344eb4273f7992441ced87a0a0d8bd55283a329b4cff042c19cea3 |