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: jsonrpc batch requests #653

Merged
merged 1 commit into from
Sep 10, 2024
Merged

feat: jsonrpc batch requests #653

merged 1 commit into from
Sep 10, 2024

Conversation

xJonathanLEI
Copy link
Owner

Resolves #593. Supersedes #600.

Implements a type-safe version of the JSON-RPC batch request feature. Users can now do this:

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"),
}

and the underlying requests would be sent in the just one HTTP request.

@xJonathanLEI
Copy link
Owner Author

Note: technically we can also implement a method that accepts only up to 32 requests but is truly type-safe by leveraging generic params. Something similar to:

let (block_number, count) =
  provider.batch_requests(BlockNumberRequest, GetBlockTransactionCountRequest {...}).await.unwrap();

which would be much nicer for use cases where the request types are statically known at compile time.

Let's settle for this for now though.

@xJonathanLEI xJonathanLEI merged commit dbe467e into master Sep 10, 2024
26 checks passed
@xJonathanLEI xJonathanLEI deleted the dev/jsonrpc_batch branch September 10, 2024 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend JSON-RPC transport capabilities for batch requests
1 participant