starknet-io / starknet.js

JavaScript library for StarkNet
https://www.starknetjs.com
MIT License
1.21k stars 733 forks source link

[V6] Implement RPC Batch #758

Closed tabaktoni closed 3 weeks ago

tabaktoni commented 11 months ago

Similar to Viem, time-based batch with config options. Ref: https://github.com/starknet-io/starknet.js/issues/653 Viem docs: https://viem.sh/docs/clients/transports/http.html#batch-optional

PhilippeR26 commented 11 months ago

What will be the benefits for the user ?

ivpavici commented 11 months ago

@PhilippeR26 I'm copying this proposal from Francesco: Hello, how do you feel about an option in the RpcProvider to automatically batch rpc calls made in the same loop of the event loop (or within a time period) together? That's what viem does and it helps avoid 429 errors. Implementation (by copying viem code, so licensing/attribution requires consideration) is trivial to add. It's useful because not only it avoids 429s, but also it helps on page load to have all content appear on the screen at the same time RPC providers also limit how many requests you can make in a limited time window ... so they limit making 100 requests at the same time, but one batch with 100 method calls inside is fine

PhilippeR26 commented 11 months ago

Are Starknet nodes already able to handle such batched requests? The JSON-RPC spec https://www.jsonrpc.org/specification#batch() is telling :

The Server MAY process a batch rpc call as a set of concurrent tasks, processing them in any order and with any width of parallelism.

Couldn't this be messy for our use case? and to be useful only for a batch of Read commands (not for Write command) ; for example 50 commands of getBlock()? If the webpage has to wait the end of a batch of 10 invokes, it could be frozen for a very long time. Isn't it better to show progress after each transaction?

So, I see an interest for a large batch of Calls, mainly getBlock(). I am rather anxious for other usage...

PhilippeR26 commented 11 months ago

I have found this interesting doc of Ethereum node Geth : https://geth.ethereum.org/docs/interacting-with-geth/rpc/batch Similar spirit to mine.

ugur-eren commented 3 months ago

Hi @tabaktoni , if this issue is still available i can work on it. Since Starknet nodes supports batch requests, it can be implemented rather easily. I've tried a few things, and IMO it can be implemented in this way: A new BatchClient class can be created and initialized in the rpc_0_6 and rpc_0_7 RpcChannel classes. If the batch option is enabled, we can use the BatchClient to send the requests at the fetch method, if not enabled, current fetch method will be used.

ivpavici commented 3 months ago

@ugur-eren sure, you can try

tabaktoni commented 3 months ago

To be honest i don't see the point as startknet provide multi call but, instead of user creating call array, it cold be done behind the scene. This could be DX improvement in some way if implemented good. Try and will see.

ugur-eren commented 3 months ago

I agree, contract calls can be used with multicall, a simple function to batch requests can be implemented, for example unraggle.meme multiCallContract function. However adding batch would also enable batching for built-in methods, like getBlockNumber or getEvents.

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 6.12.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

ugur-eren commented 1 month ago

Hi @tabaktoni this issue is resolved with my PR #1125 can be closed. Also, sorry for asking but will there be a reward for this through onlydust?

ivpavici commented 3 weeks ago

@ugur-eren reward sent!

thanks!!