High-performance JSON-RPC client — HTTP, WebSocket, provider pool with circuit breaker
Project description
ChainRPC
Production-grade, multi-provider RPC transport layer for EVM blockchains.
Built-in retry, circuit breaker, rate limiting, tiered caching, request deduplication, auto-batching, multi-chain routing, MEV protection, and Prometheus metrics — all composable via a single Arc<dyn RpcTransport> trait.
DedupTransport(CacheTransport(BackpressureTransport(ProviderPool(HttpRpcClient))))
Every layer wraps Arc<dyn RpcTransport> and itself implements RpcTransport. Stack what you need, skip what you don't.
Quick Start
Rust
# Cargo.toml
[dependencies]
chainrpc-core = "0.1"
chainrpc-http = "0.1"
use chainrpc_http::HttpRpcClient;
use chainrpc_core::transport::RpcTransport;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = HttpRpcClient::default_for("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY");
// Typed call — auto-deserializes the response
let block: String = client.call(1, "eth_blockNumber", vec![]).await?;
println!("Latest block: {block}");
Ok(())
}
Multi-Provider Failover
use chainrpc_http::pool_from_urls;
let pool = pool_from_urls(&[
"https://eth-mainnet.g.alchemy.com/v2/KEY1",
"https://mainnet.infura.io/v3/KEY2",
"https://rpc.ankr.com/eth",
])?;
// Round-robin with automatic failover when a provider's circuit breaker opens
let block = pool.send(JsonRpcRequest::auto("eth_blockNumber", vec![])).await?;
println!("Healthy: {}/{}", pool.healthy_count(), pool.len());
Tiered Caching + Dedup
use chainrpc_core::cache::{CacheTransport, CacheConfig, CacheTierResolver};
use chainrpc_core::dedup::DedupTransport;
// Cache with smart tier resolution:
// eth_getTransactionReceipt → Immutable (1h TTL)
// eth_blockNumber → Volatile (2s TTL)
// eth_sendRawTransaction → NeverCache
let cached = CacheTransport::new(Arc::new(client), CacheConfig {
tier_resolver: Some(CacheTierResolver::new()),
max_entries: 4096,
..Default::default()
});
// Deduplicate concurrent identical requests
let dedup = DedupTransport::new(Arc::new(cached));
Features
Policy Engine
- Rate Limiter — Token-bucket with CU-aware mode (knows
eth_getLogs= 75 CU,eth_blockNumber= 10 CU) - Circuit Breaker — Three-state (Closed/Open/HalfOpen), configurable failure threshold and reset timeout
- Retry Policy — Exponential backoff with jitter, respects method safety (never retries
eth_sendTransaction) - Method Safety — Classifies 40+ EVM methods as Safe / Idempotent / Unsafe
Provider Pool
- 5 selection strategies: RoundRobin, Priority, WeightedRoundRobin, LatencyBased, Sticky
- Per-provider circuit breaker + automatic failover
- Atomic metrics (success/failure counts, latency, rate-limit hits)
- Background health checking
Caching & Dedup
- 4-tier cache: Immutable (1h), SemiStable (5m), Volatile (2s), NeverCache
- Smart tier resolution based on method + parameters
- Reorg-aware invalidation (
invalidate_for_reorg(block)) - Request deduplication (N concurrent callers = 1 HTTP call)
Advanced
- Auto-Batching — Collects individual calls into JSON-RPC batch requests within a time window
- Multi-Chain Router — Route by chain ID, parallel cross-chain queries
- Request Hedging — Race primary + backup, return first response
- Backpressure — Concurrency limiting, fail-fast when overloaded
- Archive Routing — Route historical queries to archive nodes
- MEV Protection — Detect 12 MEV-susceptible selectors, route to private relays
- Gas Estimation — EIP-1559 recommendations (Slow/Standard/Fast/Urgent)
- Tx Lifecycle — Send, track, poll receipt, detect stuck, nonce management, gas bumping
- CU Budget Tracking — Monitor compute unit consumption against monthly budgets
- Solana Support — Commitment levels (Processed/Confirmed/Finalized), 50+ method safety classification, CU costs
- Geo-Aware Routing — Route to geographically closest provider with automatic proximity-based fallback
- Gas Bumping — Speed up or cancel stuck transactions with EIP-1559 compliant replacement (Percentage/Double/SpeedTier/Cancel)
- Reorg Detection — Sliding block hash window, configurable safe depth, reorg callbacks for cache invalidation
Lifecycle & Observability
- Graceful shutdown with signal handling and drain timeout
- Cooperative cancellation tokens (parent/child hierarchy)
- Per-provider Prometheus metrics export
- Structured tracing integration
Crate Structure
chainrpc/
crates/
chainrpc-core/ # 31 modules — trait, types, all middleware
chainrpc-http/ # HTTP transport (reqwest) with retry loop
chainrpc-ws/ # WebSocket transport (tokio-tungstenite)
chainrpc-providers/ # Pre-configured Alchemy, Infura, QuickNode, Chainstack, public profiles
bindings/
node/ # TypeScript (napi-rs)
python/ # Python (PyO3 + maturin)
go/ # Go (cgo)
java/ # Java (JNI)
cli/ # CLI tool (call, pool, bench)
examples/ # 26 runnable examples
docs/ # Architecture, modules, API reference
Examples
26 examples covering every module. See examples/README.md for the full index.
| Category | Examples |
|---|---|
| Basics | Simple Client |
| Caching | Tiered Cache |
| Cost Control | Rate Limiting |
| Routing | Multi-Chain |
| EVM | Gas |
| Lifecycle | Shutdown |
| New Modules | Solana RPC |
Documentation
| Document | Description |
|---|---|
| Architecture | System diagram, crate structure, request flow, design principles |
| Modules | Detailed reference for all 31 modules with public API |
| API Reference | Quick-lookup tables for every type, trait, and function |
| Use Cases | 20 real-world patterns with complete Rust code |
| Implementation | Design decisions, concurrency patterns, internals |
Language Bindings
| Language | Package | Install |
|---|---|---|
| TypeScript | @chainfoundry/chainrpc |
npm install @chainfoundry/chainrpc |
| Python | chainrpc |
pip install chainrpc |
| Go | chainrpc |
go get github.com/DarshanKumar89/chainfoundry/chainrpc |
| Java | chainrpc |
Maven / Gradle |
Test Coverage
262 tests, 0 failures
chainrpc-core: 250 tests (all 31 modules)
chainrpc-providers: 6 tests (URL construction)
chainrpc-ws: 3 tests (subscription management)
Doc-tests: 3 tests
cd chainrpc && cargo test --workspace
License
MIT
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 Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chainfoundry_chainrpc-0.2.2-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: chainfoundry_chainrpc-0.2.2-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25ae0c349657fd250008720d5bf61264ab00fd708c818e5474873b3237da63bb
|
|
| MD5 |
659d61cad81560c7bcae3e57b1184da7
|
|
| BLAKE2b-256 |
11129486fe96980aa6855e0629cae7a495bc2c28b8874157176b0e8a3c4c7e58
|
Provenance
The following attestation bundles were made for chainfoundry_chainrpc-0.2.2-cp311-cp311-win_amd64.whl:
Publisher:
publish-chainrpc.yml on DarshanKumar89/chainfoundry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainfoundry_chainrpc-0.2.2-cp311-cp311-win_amd64.whl -
Subject digest:
25ae0c349657fd250008720d5bf61264ab00fd708c818e5474873b3237da63bb - Sigstore transparency entry: 1339366973
- Sigstore integration time:
-
Permalink:
DarshanKumar89/chainfoundry@be107debc6d424486094bcb27de7bbadee439f67 -
Branch / Tag:
refs/tags/chainrpc-v0.2.2 - Owner: https://github.com/DarshanKumar89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-chainrpc.yml@be107debc6d424486094bcb27de7bbadee439f67 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file chainfoundry_chainrpc-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: chainfoundry_chainrpc-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.11, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a9b9d7c9218857b45aba1f0d8e799629ea5d30ab4f3c9a44ca0b9fa74e51550
|
|
| MD5 |
de0d3f44101571b8c7fad2d0c85596c4
|
|
| BLAKE2b-256 |
b748bc7893cb24790d7d4fdf5349d96c614a3d3f2e9c1ec04110d97f5aa7fbdc
|
Provenance
The following attestation bundles were made for chainfoundry_chainrpc-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl:
Publisher:
publish-chainrpc.yml on DarshanKumar89/chainfoundry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainfoundry_chainrpc-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl -
Subject digest:
2a9b9d7c9218857b45aba1f0d8e799629ea5d30ab4f3c9a44ca0b9fa74e51550 - Sigstore transparency entry: 1339367018
- Sigstore integration time:
-
Permalink:
DarshanKumar89/chainfoundry@be107debc6d424486094bcb27de7bbadee439f67 -
Branch / Tag:
refs/tags/chainrpc-v0.2.2 - Owner: https://github.com/DarshanKumar89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-chainrpc.yml@be107debc6d424486094bcb27de7bbadee439f67 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d7905bd0fdf617a84ef9cb6517d098b833a22a8e792805f75614c343c787561
|
|
| MD5 |
8a865efbe1be6e608ae0a4ec4ad1cf6b
|
|
| BLAKE2b-256 |
4162a45cdd1ba3759f40d53b804f43872c59a0ddd7a073d792559a1d4994ecb4
|
Provenance
The following attestation bundles were made for chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_11_0_arm64.whl:
Publisher:
publish-chainrpc.yml on DarshanKumar89/chainfoundry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_11_0_arm64.whl -
Subject digest:
8d7905bd0fdf617a84ef9cb6517d098b833a22a8e792805f75614c343c787561 - Sigstore transparency entry: 1339366945
- Sigstore integration time:
-
Permalink:
DarshanKumar89/chainfoundry@be107debc6d424486094bcb27de7bbadee439f67 -
Branch / Tag:
refs/tags/chainrpc-v0.2.2 - Owner: https://github.com/DarshanKumar89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-chainrpc.yml@be107debc6d424486094bcb27de7bbadee439f67 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl.
File metadata
- Download URL: chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.11, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f898c7852913c6eab3cf285a261c3a1c0cfa3ab3bc6c5ed74ad49a97f477793
|
|
| MD5 |
2c6aac486cee2686b48b3079fb6deaf4
|
|
| BLAKE2b-256 |
dbfce081a0e331494e044d0b410735148ec1d01c9e4c3de4e9fcc29187d3614c
|
Provenance
The following attestation bundles were made for chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl:
Publisher:
publish-chainrpc.yml on DarshanKumar89/chainfoundry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainfoundry_chainrpc-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl -
Subject digest:
9f898c7852913c6eab3cf285a261c3a1c0cfa3ab3bc6c5ed74ad49a97f477793 - Sigstore transparency entry: 1339367068
- Sigstore integration time:
-
Permalink:
DarshanKumar89/chainfoundry@be107debc6d424486094bcb27de7bbadee439f67 -
Branch / Tag:
refs/tags/chainrpc-v0.2.2 - Owner: https://github.com/DarshanKumar89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-chainrpc.yml@be107debc6d424486094bcb27de7bbadee439f67 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc93ecccece23489beb733f20800ee77b86af9ae6e0f324ab5044fd72ad52c7c
|
|
| MD5 |
61f8cf712d1aa7e5ced2af93a71a9d7a
|
|
| BLAKE2b-256 |
d6f0c4bb4f59a5d960855aea93fc1c191631e33c28ab8f23c8e2ab3bb4d0d397
|
Provenance
The following attestation bundles were made for chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
publish-chainrpc.yml on DarshanKumar89/chainfoundry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
bc93ecccece23489beb733f20800ee77b86af9ae6e0f324ab5044fd72ad52c7c - Sigstore transparency entry: 1339367044
- Sigstore integration time:
-
Permalink:
DarshanKumar89/chainfoundry@be107debc6d424486094bcb27de7bbadee439f67 -
Branch / Tag:
refs/tags/chainrpc-v0.2.2 - Owner: https://github.com/DarshanKumar89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-chainrpc.yml@be107debc6d424486094bcb27de7bbadee439f67 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d23c95236c1499b958274d96528419b71ec0b25713e75c9d96c77739d550a98e
|
|
| MD5 |
71e8555bdad45f7fffcec3bbc7b81230
|
|
| BLAKE2b-256 |
7c343a49b19b6b9a66268f6b907f99ddb1eeb9ac9c662fefcf645426e553670a
|
Provenance
The following attestation bundles were made for chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:
Publisher:
publish-chainrpc.yml on DarshanKumar89/chainfoundry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainfoundry_chainrpc-0.2.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -
Subject digest:
d23c95236c1499b958274d96528419b71ec0b25713e75c9d96c77739d550a98e - Sigstore transparency entry: 1339366994
- Sigstore integration time:
-
Permalink:
DarshanKumar89/chainfoundry@be107debc6d424486094bcb27de7bbadee439f67 -
Branch / Tag:
refs/tags/chainrpc-v0.2.2 - Owner: https://github.com/DarshanKumar89
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-chainrpc.yml@be107debc6d424486094bcb27de7bbadee439f67 -
Trigger Event:
workflow_dispatch
-
Statement type: