Skip to main content

JSON-RPC Python Client

Library for interacting with JSON-RPC servers. Implements the JSON-RPC 2.0 spec, as well as first-class support for the subscription streaming endpoints exposed by most blockchain RPC interfaces (e.g. Ethereum, Solana) for JSON-RPC websockets connections.

Features

  • Websockets connection wrapper with full support for matching request IDs to responses and subscription updates to subscription IDs
  • Classes for (de)serializing JSON-RPC spec compliant message types

Installation

$ pip install bx-jsonrpc-py

Websockets Streaming

Many blockchains use a subscription RPC endpoint with JSON-RPC notifications to provide real-time event streams of updates in the network. They usually look something like this:

// create subscription
>> {"id": 1, "method": "subscribe", "params": ["feedName"]}
<< {"jsonrpc":"2.0","id":1,"result":"subscriptionID"}

// incoming notifications
<< {"jsonrpc":"2.0","method":"subscribe","params":{"subscription":"subscriptionID","result":"data"}}}
<< {"jsonrpc":"2.0","method":"subscribe","params":{"subscription":"subscriptionID","result":"data}}}

// cancel subscription
>> {"id": 1, "method": "unsubscribe", "params": ["subscriptionID"]}
<< {"jsonrpc":"2.0","id":1,"result":true}

Some examples: go-ethereum, solana

The included jsonrpc.WsRpcConnection class fully implements this feature.

Usage

import jsonrpc

async with jsonrpc.WsRpcConnection("ws://...") as ws:
    print(await ws.call("method", {"param": "value"}))
    
    subscription_id = await ws.subscribe(
        "feedname", {"option": "value"}
    )
    async for notification in ws.notifications_for_id(subscription_id):
        print(notification)

See examples/client.py for a working sample.

Anticipated Future Work

  • Reconnection support for websockets to reconnect and resubscribe to former feeds

Comments / Questions

Feel free to open up a Github issue to discuss enhancements or problems.

MIT License

Copyright (c) 2022 bloXroute Labs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for bx-jsonrpc-py 0.2.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 bx-jsonrpc-py 0.2.0
File Size Uploaded
bx-jsonrpc-py-0.2.0.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bx-jsonrpc-py 0.2.0
File Interpreter ABI Platform
bx_jsonrpc_py-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.5 kB

Release files / bx-jsonrpc-py-0.2.0.tar.gz

Download URL bx-jsonrpc-py-0.2.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7d5b4888ac2aee1d65cc85b787f1cfb6cd61c07d28b5699c25764337b18ba37f
BLAKE2b-256 checksum
How to use checksums
cd699963fd07838e9ed35df5222a6978e52c98c58c3fc8aad0e589d68397e007
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.5

Release files / bx_jsonrpc_py-0.2.0-py3-none-any.whl

Download URL bx_jsonrpc_py-0.2.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9db51d433ecb4248c681c137a58050b820c2360f3b47d84c262991a29873c6f5
BLAKE2b-256 checksum
How to use checksums
555c6069accc43bdac9319f8dc2383b44904f4cec420c7f4703f9c6943af2601
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

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