Skip to content

Commit

Permalink
improve formatting & add comments for the example
Browse files Browse the repository at this point in the history
  • Loading branch information
thetheveloper committed Jul 16, 2024
1 parent 681d7a4 commit f670190
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
38 changes: 30 additions & 8 deletions examples/jsonrpc_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#?}"),
}

}
24 changes: 5 additions & 19 deletions starknet-providers/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,14 +674,11 @@ impl Provider for AnyProvider {
P: Serialize + Send + Sync,
{
match self {
Self::JsonRpcHttp(inner) => {
<JsonRpcClient<HttpTransport> as Provider>::batch_requests(inner, requests).await
}
Self::SequencerGateway(inner) => {
<SequencerGatewayProvider as Provider>::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<B>(
&self,
block_ids: Vec<B>,
Expand All @@ -690,19 +687,8 @@ impl Provider for AnyProvider {
B: AsRef<BlockId> + Send + Sync,
{
match self {
Self::JsonRpcHttp(inner) => {
<JsonRpcClient<HttpTransport> as Provider>::get_block_with_tx_hashes_batch(
inner, block_ids,
)
.await
}
Self::SequencerGateway(inner) => {
<SequencerGatewayProvider as Provider>::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,
}
}

}
9 changes: 4 additions & 5 deletions starknet-providers/src/jsonrpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ pub struct JsonRpcRequest {
}

#[derive(Debug, Clone)]
pub struct JsonRpcRequests {
pub requests: Vec<JsonRpcRequest>,
}
pub struct JsonRpcRequests(pub Vec<JsonRpcRequest>);

#[derive(Debug, Clone, Serialize)]
pub enum JsonRpcRequestData {
Expand Down Expand Up @@ -216,6 +214,7 @@ where
}
}
}

async fn send_requests<I, P, R>(&self, requests: I) -> Result<Vec<R>, ProviderError>
where
I: IntoIterator<Item = (JsonRpcMethod, P)> + Send + Sync,
Expand Down Expand Up @@ -273,11 +272,11 @@ where
where
B: AsRef<BlockId> + 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(),
},
)
});
Expand Down
2 changes: 1 addition & 1 deletion starknet-providers/src/jsonrpc/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit f670190

Please sign in to comment.