Skip to content

Commit dbe467e

Browse files
authored
feat: jsonrpc batch requests (#653)
1 parent 660a732 commit dbe467e

File tree

11 files changed

+622
-112
lines changed

11 files changed

+622
-112
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,21 @@ Examples can be found in the [examples folder](./examples):
9292

9393
6. [Query the latest block number with JSON-RPC](./examples/jsonrpc.rs)
9494

95-
7. [Call a contract view function](./examples/erc20_balance.rs)
95+
7. [Batched JSON-RPC requests](./examples/batch.rs)
9696

97-
8. [Deploy an Argent X account to a pre-funded address](./examples/deploy_argent_account.rs)
97+
8. [Call a contract view function](./examples/erc20_balance.rs)
9898

99-
9. [Inspect public key with Ledger](./examples/ledger_public_key.rs)
99+
9. [Deploy an Argent X account to a pre-funded address](./examples/deploy_argent_account.rs)
100100

101-
10. [Deploy an OpenZeppelin account with Ledger](./examples/deploy_account_with_ledger.rs)
101+
10. [Inspect public key with Ledger](./examples/ledger_public_key.rs)
102102

103-
11. [Transfer ERC20 tokens with Ledger](./examples/transfer_with_ledger.rs)
103+
11. [Deploy an OpenZeppelin account with Ledger](./examples/deploy_account_with_ledger.rs)
104104

105-
12. [Parsing a JSON-RPC request on the server side](./examples/parse_jsonrpc_request.rs)
105+
12. [Transfer ERC20 tokens with Ledger](./examples/transfer_with_ledger.rs)
106106

107-
13. [Inspecting a erased provider-specific error type](./examples/downcast_provider_error.rs)
107+
13. [Parsing a JSON-RPC request on the server side](./examples/parse_jsonrpc_request.rs)
108+
109+
14. [Inspecting a erased provider-specific error type](./examples/downcast_provider_error.rs)
108110

109111
## License
110112

examples/batch.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use starknet::providers::{
2+
jsonrpc::{HttpTransport, JsonRpcClient},
3+
Provider, ProviderRequestData, ProviderResponseData, Url,
4+
};
5+
use starknet_core::types::{
6+
requests::{BlockNumberRequest, GetBlockTransactionCountRequest},
7+
BlockId,
8+
};
9+
10+
#[tokio::main]
11+
async fn main() {
12+
let provider = JsonRpcClient::new(HttpTransport::new(
13+
Url::parse("https://starknet-sepolia.public.blastapi.io/rpc/v0_7").unwrap(),
14+
));
15+
16+
let responses = provider
17+
.batch_requests([
18+
ProviderRequestData::BlockNumber(BlockNumberRequest),
19+
ProviderRequestData::GetBlockTransactionCount(GetBlockTransactionCountRequest {
20+
block_id: BlockId::Number(100),
21+
}),
22+
])
23+
.await
24+
.unwrap();
25+
26+
match (&responses[0], &responses[1]) {
27+
(
28+
ProviderResponseData::BlockNumber(block_number),
29+
ProviderResponseData::GetBlockTransactionCount(count),
30+
) => {
31+
println!("The latest block is #{}", block_number);
32+
println!("Block #100 has {} transactions", count);
33+
}
34+
_ => panic!("unexpected response type"),
35+
}
36+
}

examples/parse_jsonrpc_request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use starknet_providers::jsonrpc::{JsonRpcRequest, JsonRpcRequestData};
1+
use starknet_providers::{jsonrpc::JsonRpcRequest, ProviderRequestData};
22

33
fn main() {
44
// Let's pretend this is the raw request body coming from HTTP
@@ -17,7 +17,7 @@ fn main() {
1717
println!("Request received: {:#?}", parsed_request);
1818

1919
match parsed_request.data {
20-
JsonRpcRequestData::GetBlockTransactionCount(req) => {
20+
ProviderRequestData::GetBlockTransactionCount(req) => {
2121
println!(
2222
"starknet_getBlockTransactionCount request received for block: {:?}",
2323
req.block_id

starknet-providers/src/any.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use starknet_core::types::{
1212

1313
use crate::{
1414
jsonrpc::{HttpTransport, JsonRpcClient},
15-
Provider, ProviderError, SequencerGatewayProvider,
15+
Provider, ProviderError, ProviderRequestData, ProviderResponseData, SequencerGatewayProvider,
1616
};
1717

1818
/// A convenient Box-able type that implements the [Provider] trait. This can be useful when you
@@ -665,4 +665,21 @@ impl Provider for AnyProvider {
665665
}
666666
}
667667
}
668+
669+
async fn batch_requests<R>(
670+
&self,
671+
requests: R,
672+
) -> Result<Vec<ProviderResponseData>, ProviderError>
673+
where
674+
R: AsRef<[ProviderRequestData]> + Send + Sync,
675+
{
676+
match self {
677+
Self::JsonRpcHttp(inner) => {
678+
<JsonRpcClient<HttpTransport> as Provider>::batch_requests(inner, requests).await
679+
}
680+
Self::SequencerGateway(inner) => {
681+
<SequencerGatewayProvider as Provider>::batch_requests(inner, requests).await
682+
}
683+
}
684+
}
668685
}

0 commit comments

Comments
 (0)