Skip to content

Commit

Permalink
feat: jsonrpc batch requests (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Sep 10, 2024
1 parent 660a732 commit dbe467e
Show file tree
Hide file tree
Showing 11 changed files with 622 additions and 112 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ Examples can be found in the [examples folder](./examples):

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

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

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

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

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

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

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

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

14. [Inspecting a erased provider-specific error type](./examples/downcast_provider_error.rs)

## License

Expand Down
36 changes: 36 additions & 0 deletions examples/batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use starknet::providers::{
jsonrpc::{HttpTransport, JsonRpcClient},
Provider, ProviderRequestData, ProviderResponseData, Url,
};
use starknet_core::types::{
requests::{BlockNumberRequest, GetBlockTransactionCountRequest},
BlockId,
};

#[tokio::main]
async fn main() {
let provider = JsonRpcClient::new(HttpTransport::new(
Url::parse("https://starknet-sepolia.public.blastapi.io/rpc/v0_7").unwrap(),
));

let responses = provider
.batch_requests([
ProviderRequestData::BlockNumber(BlockNumberRequest),
ProviderRequestData::GetBlockTransactionCount(GetBlockTransactionCountRequest {
block_id: BlockId::Number(100),
}),
])
.await
.unwrap();

match (&responses[0], &responses[1]) {
(
ProviderResponseData::BlockNumber(block_number),
ProviderResponseData::GetBlockTransactionCount(count),
) => {
println!("The latest block is #{}", block_number);
println!("Block #100 has {} transactions", count);
}
_ => panic!("unexpected response type"),
}
}
4 changes: 2 additions & 2 deletions examples/parse_jsonrpc_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use starknet_providers::jsonrpc::{JsonRpcRequest, JsonRpcRequestData};
use starknet_providers::{jsonrpc::JsonRpcRequest, ProviderRequestData};

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

match parsed_request.data {
JsonRpcRequestData::GetBlockTransactionCount(req) => {
ProviderRequestData::GetBlockTransactionCount(req) => {
println!(
"starknet_getBlockTransactionCount request received for block: {:?}",
req.block_id
Expand Down
19 changes: 18 additions & 1 deletion starknet-providers/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use starknet_core::types::{

use crate::{
jsonrpc::{HttpTransport, JsonRpcClient},
Provider, ProviderError, SequencerGatewayProvider,
Provider, ProviderError, ProviderRequestData, ProviderResponseData, SequencerGatewayProvider,
};

/// A convenient Box-able type that implements the [Provider] trait. This can be useful when you
Expand Down Expand Up @@ -665,4 +665,21 @@ impl Provider for AnyProvider {
}
}
}

async fn batch_requests<R>(
&self,
requests: R,
) -> Result<Vec<ProviderResponseData>, ProviderError>
where
R: AsRef<[ProviderRequestData]> + 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
}
}
}
}
Loading

0 comments on commit dbe467e

Please sign in to comment.