ethrpc-rs
A lightweight async Rust library for making JSON-RPC calls to Ethereum-compatible
nodes. A port of the Go ethrpc library,
built on the async rsurl HTTP client and Tokio.
Install
All network methods are async and run inside a Tokio runtime.
Quick start
use ;
async
Features
Positional and named arguments
use json;
// Positional arguments
let balance = rpc.call.await?.to_big_int?;
// Named arguments
let mut params = new;
params.insert;
params.insert;
let result = rpc.call_named.await?;
Decode helpers
The [ValueExt] trait adds decoding methods to the serde_json::Value a call
returns, so results decode in place with ? — no hex-string juggling:
use ValueExt;
let block = rpc.call.await?.to_u64?;
let balance = rpc.call.await?.to_big_int?;
let hash = rpc.call.await?.to_str?.to_owned;
// Decode into any type implementing serde::Deserialize
let block: MyBlockType =
rpc.call_as.await?;
Deserialize into a target type
let peers: = rpc.call_as.await?;
Basic authentication
let mut rpc = new;
rpc.set_basic_auth;
Method overrides
Intercept RPC methods locally without hitting the remote node:
rpc.set_override;
Server evaluation
Select the fastest endpoints by racing eth_blockNumber calls:
let handler = evaluate.await?;
// handler implements ethrpc_rs::Handler with the best responding servers
let block = handler.call.await?.to_u64?;
Contract calls (ABI)
The abi module (on by default) turns a function signature and typed arguments
into calldata, performs the eth_call, and decodes the result — no manual hex
juggling. It covers the common ABI types (address, uint<M>, int<M>, bool,
bytes<N>, dynamic bytes/string, and arrays of those), which is enough for
ERC-20/721 reads and most view calls:
use ;
// balanceOf(address) -> uint256
let out = eth_call_abi.await?;
let balance = out.as_uint.unwrap;
Selectors use Keccak-256 from purecrypto.
Disable the whole thing (and that dependency) with default-features = false for
a lean raw-JSON-RPC build. Lower-level encode, decode, encode_call, and
function_selector helpers are exposed too.
HTTP response forwarding
Build a JSON-RPC response (running overrides locally or proxying to the node, stripping hop-by-hop headers) ready to write to any HTTP framework:
use ;
use Duration;
let resp = rpc.forward.await;
// resp.status, resp.headers, resp.body
Chain metadata
The chains module provides static metadata for known EVM-compatible chains:
let eth = get.unwrap; // Ethereum Mainnet
println!; // "Ethereum Mainnet"
println!; // "ETH"
println!; // true
println!; // Some("https://etherscan.io/tx/0xabc...")
println!; // Some("https://etherscan.io")
Differences from the Go library
- All RPC methods are
async; cancellation is via dropping the future ortokio::time::timeoutrather than acontext.Context. Forwardreturns a framework-agnosticForwardResponse { status, headers, body }instead of writing to anhttp.ResponseWriter.- Method overrides are closures
Fn(&[Value]) -> Result<Value>rather than reflection-based arbitrary Go functions. - The
abicontract-call helper (eth_call_abi) has no Go counterpart — the Go library only exposed raw JSON-RPC.
License
MIT