From f670190988fc6b345f9ac468e43cf0fb9a9e8b3e Mon Sep 17 00:00:00 2001 From: thetheveloper Date: Thu, 20 Jun 2024 00:54:47 +0200 Subject: [PATCH] improve formatting & add comments for the example --- examples/jsonrpc_batch.rs | 38 +++++++++++++++---- starknet-providers/src/any.rs | 24 +++--------- starknet-providers/src/jsonrpc/mod.rs | 9 ++--- .../src/jsonrpc/transports/http.rs | 2 +- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/examples/jsonrpc_batch.rs b/examples/jsonrpc_batch.rs index c1b17ea4..4fbd2f68 100644 --- a/examples/jsonrpc_batch.rs +++ b/examples/jsonrpc_batch.rs @@ -10,26 +10,48 @@ use starknet_providers::jsonrpc::{JsonRpcMethod, JsonRpcRequestParams}; #[tokio::main] async fn main() { + // Create a new JSON RPC client using HTTP transport with the specified URL let provider = JsonRpcClient::new(HttpTransport::new( Url::parse("https://starknet-sepolia.public.blastapi.io/rpc/v0_7").unwrap(), )); - - let batch_mixed_results = provider.batch_requests(vec![(JsonRpcMethod::GetBlockWithTxHashes, JsonRpcRequestParams::GetBlockWithTxHashes(GetBlockWithTxHashesRequestRef { - block_id: BlockId::Tag(BlockTag::Latest).as_ref(), - })), (JsonRpcMethod::GetBlockWithTxs, JsonRpcRequestParams::GetBlockWithTxs(GetBlockWithTxsRequestRef { - block_id: BlockId::Tag(BlockTag::Latest).as_ref(), - }))]).await; + + // batch_requests allows to define a vector of requests for batch processing, ensuring each request specifies its corresponding JsonRpcMethod and JsonRpcRequestParams. + // This approach allows for a generic way to handle batch requests. + let batch_mixed_results = provider + .batch_requests(vec![ + // Request 1: Retrieve block data including transaction hashes. + ( + JsonRpcMethod::GetBlockWithTxHashes, + JsonRpcRequestParams::GetBlockWithTxHashes(GetBlockWithTxHashesRequestRef { + block_id: BlockId::Tag(BlockTag::Latest).as_ref(), + }), + ), + // Request 2: Retrieve block data including full transaction details. + ( + JsonRpcMethod::GetBlockWithTxs, + JsonRpcRequestParams::GetBlockWithTxs(GetBlockWithTxsRequestRef { + block_id: BlockId::Tag(BlockTag::Latest).as_ref(), + }), + ), + ]) + .await; match batch_mixed_results { Ok(v) => println!("{v:#?}"), Err(e) => println!("Error: {e:#?}"), } - let batched_blocks = provider.get_block_with_tx_hashes_batch(vec![BlockId::Tag(BlockTag::Latest), BlockId::Tag(BlockTag::Latest)]).await; + // The following example demonstrates the process of sending a batch request to retrieve multiple blocks, each including transaction hashes. + // get_block_with_tx_hashes_batch utilizes a vector of BlockId parameters to construct the batch request. + let batched_blocks = provider + .get_block_with_tx_hashes_batch(vec![ + BlockId::Tag(BlockTag::Latest), + BlockId::Tag(BlockTag::Latest), + ]) + .await; match batched_blocks { Ok(v) => println!("{v:#?}"), Err(e) => println!("Error: {e:#?}"), } - } diff --git a/starknet-providers/src/any.rs b/starknet-providers/src/any.rs index 7af0d2d5..0d04255c 100644 --- a/starknet-providers/src/any.rs +++ b/starknet-providers/src/any.rs @@ -674,14 +674,11 @@ impl Provider for AnyProvider { P: Serialize + Send + Sync, { match self { - Self::JsonRpcHttp(inner) => { - as Provider>::batch_requests(inner, requests).await - } - Self::SequencerGateway(inner) => { - ::batch_requests(inner, requests).await - } + Self::JsonRpcHttp(inner) => inner.batch_requests(requests).await, + Self::SequencerGateway(inner) => inner.batch_requests(requests).await, } } + async fn get_block_with_tx_hashes_batch( &self, block_ids: Vec, @@ -690,19 +687,8 @@ impl Provider for AnyProvider { B: AsRef + Send + Sync, { match self { - Self::JsonRpcHttp(inner) => { - as Provider>::get_block_with_tx_hashes_batch( - inner, block_ids, - ) - .await - } - Self::SequencerGateway(inner) => { - ::get_block_with_tx_hashes_batch( - inner, block_ids, - ) - .await - } + Self::JsonRpcHttp(inner) => inner.get_block_with_tx_hashes_batch(block_ids).await, + Self::SequencerGateway(inner) => inner.get_block_with_tx_hashes_batch(block_ids).await, } } - } diff --git a/starknet-providers/src/jsonrpc/mod.rs b/starknet-providers/src/jsonrpc/mod.rs index eeea4f18..ddb7e0d7 100644 --- a/starknet-providers/src/jsonrpc/mod.rs +++ b/starknet-providers/src/jsonrpc/mod.rs @@ -105,9 +105,7 @@ pub struct JsonRpcRequest { } #[derive(Debug, Clone)] -pub struct JsonRpcRequests { - pub requests: Vec, -} +pub struct JsonRpcRequests(pub Vec); #[derive(Debug, Clone, Serialize)] pub enum JsonRpcRequestData { @@ -216,6 +214,7 @@ where } } } + async fn send_requests(&self, requests: I) -> Result, ProviderError> where I: IntoIterator + Send + Sync, @@ -273,11 +272,11 @@ where where B: AsRef + Send + Sync, { - let requests = block_ids.iter().map(|b_id| { + let requests = block_ids.iter().map(|block_id| { ( JsonRpcMethod::GetBlockWithTxHashes, GetBlockWithTxHashesRequestRef { - block_id: b_id.as_ref(), + block_id: block_id.as_ref(), }, ) }); diff --git a/starknet-providers/src/jsonrpc/transports/http.rs b/starknet-providers/src/jsonrpc/transports/http.rs index 154ea4b6..450d6af8 100644 --- a/starknet-providers/src/jsonrpc/transports/http.rs +++ b/starknet-providers/src/jsonrpc/transports/http.rs @@ -130,7 +130,7 @@ impl JsonRpcTransport for HttpTransport { .body(serialized_batch) .header("Content-Type", "application/json"); - for (name, value) in self.headers.iter() { + for (name, value) in &self.headers { request = request.header(name, value); }