Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: JSON-RPC batch request #161

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ This repository contains the following examples:
- [x] [Subscribe and listen to pending transactions in the public mempool](./examples/subscriptions/examples/subscribe_pending_transactions.rs)
- [x] [Event multiplexer](./examples/subscriptions/examples/event_multiplexer.rs)
- [x] Providers
- [x] [JSON-RPC Batch Request](./examples/providers/examples/batch_rpc.rs)
- [x] [Builder](./examples/providers/examples/builder.rs)
- [x] [Builtin](./examples/providers/examples/builtin.rs)
- [x] [HTTP with authentication](./examples/providers/examples/http_with_auth.rs)
Expand Down
49 changes: 49 additions & 0 deletions examples/providers/examples/batch_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Example depicting how to make a Batch RPC request using the HTTP provider.

use alloy::{
node_bindings::Anvil,
primitives::{address, U128, U64},
rpc::client::ClientBuilder,
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
let anvil = Anvil::new().spawn();

// Swap this out with a RPC_URL provider that supports JSON-RPC batch requests. e.g. https://eth.merkle.io
let rpc_url = anvil.endpoint_url();

// Create a HTTP transport.
let client = ClientBuilder::default().http(rpc_url);

// Instantiate a batch.
let mut batch = client.new_batch();

// Add calls to the batch.
let blobk_number_fut =
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved
batch.add_call("eth_blockNumber", &())?.map_resp(|resp: U64| resp.to::<u64>());

let gas_price_fut =
batch.add_call("eth_gasPrice", &())?.map_resp(|resp: U128| resp.to::<u128>());

let vitalik = address!("d8da6bf26964af9d7eed9e03e53415d37aa96045");

let vitalik_nonce_fut = batch
.add_call("eth_getTransactionCount", &(vitalik, "latest"))? // Vitalik's nonce at BlockId::Latest
.map_resp(|resp: U128| resp.to::<u128>());

// Send the batch request.
batch.send().await?;

// Get the results.
let latest_block = blobk_number_fut.await?;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved
let gas_price = gas_price_fut.await?;
let vitalik_nonce = vitalik_nonce_fut.await?;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved

println!("Latest block number: {latest_block}");
println!("Gas price: {gas_price}");
println!("Vitalik's nonce: {vitalik_nonce}");

Ok(())
}
Loading