From 13ef078c2bda5e54512c7663f44a9afc6ae66f7c Mon Sep 17 00:00:00 2001 From: Toni Tabak Date: Fri, 26 Jul 2024 13:06:43 +0200 Subject: [PATCH] docs: batchClient --- www/docs/guides/connect_network.md | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/www/docs/guides/connect_network.md b/www/docs/guides/connect_network.md index 9abc87917..5eea392c5 100644 --- a/www/docs/guides/connect_network.md +++ b/www/docs/guides/connect_network.md @@ -160,3 +160,43 @@ const provider = new RpcProvider({ nodeUrl: 'http://127.0.0.1:5050/rpc' }); ``` > If you have customized host and port during starknet-devnet initialization, adapt in accordance your script. + +## Batch JSON-RPC + +The BatchClient class allows requests to be batched together in a single HTTP request, either by the interval amount or at the end of the callback queue if the batch is set to 0. By batching requests, we can reduce the overhead associated with handling individual requests. + +#### Example of usage with RpcProvider + +```typescript +const myProvider = new RpcProvider({ + batch: 0, +}); + +const [getBlockResponse, blockHashAndNumber, txCount] = await Promise.all([ + myBatchProvider.getBlock(), + myBatchProvider.getBlockLatestAccepted(), + myBatchProvider.getBlockTransactionCount('latest'), +]); + +// ... usage of getBlockResponse, blockHashAndNumber, txCount +``` + +#### Example of direct usage of underlying BatchClient class + +```typescript +const provider = new RpcProvider(); + +const batchClient = new BatchClient({ + nodeUrl: provider.channel.nodeUrl, + headers: provider.channel.headers, + interval: 0, +}); + +const [getBlockResponse, blockHashAndNumber, txCount] = await Promise.all([ + myBatchProvider.getBlock(), + myBatchProvider.getBlockLatestAccepted(), + myBatchProvider.getBlockTransactionCount('latest'), +]); + +// ... usage of getBlockResponse, blockHashAndNumber, txCount +```