ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.95k stars 1.85k forks source link

Multicall support #788

Closed Destiner closed 2 months ago

Destiner commented 4 years ago

As projects usually make a lot of queries per page (and Infura call limits are quite modest), I think it might be a good idea to support multicall.

There is multicall.js, but its pretty low-level (and in general have a different approach to ethereum querying).

There is also ethcall (which is kind of multicall and ether.js baby), but given that it's already tightly coupled with ethers, I think it might be a good idea to have this functionality without an additional dependency.

ricmoo commented 4 years ago

I’ve thought about adding a similar batching for token and ether balances. It’s an interesting idea, and I think the easiest and most useful way would be to add a new provider, which can be backed by any other provider, and have a short delay (maybe 10ms; configurable) queueing up requests and then sending them all at once (or in blocks) to the contract.

Then it just works, no code changes needed and handles the common case where a bunch of calls are made in the same event loop, and also doesn’t hold up the dependency graph.

I’ll look into it over the weekend and probably add it to the experimental package to try out before adding it to the provider package.

Thanks for the suggestion. :)

jparklev commented 4 years ago

Ooh hah, so I think @michaelelliot was working on basically exactly this just the other week as an experiment. I don't have too much to add to the idea, other than to say that it sounds great, but I thought it was exciting/ a positive sign/ serendipitous that there was independent alignment

PfanP commented 3 years ago

Hey, why batch call is still not added to ethers?

ricmoo commented 3 years ago

@self-coding-crab batch calls are unrelated to multicalls.

It turns out multicall has an issue with certain opcodes that need work around for, which I’m hoping to address later in v6 with a new type of provider.

Batch calls are not available yet, because they add very little value, are less performant and not all backends support them. It is important to note that batch calls are part of the JSON-RPC specification, nothing specific to Ethereum or Geth.

There are plenty of issues you can search though for more details (e.g. #62), and I plan to add a cookbook entry on how to create an implicit batching provider, but there has not been many strong arguments made in favour of batching; I’m more than welcome to hear thoughts though. Perhaps start a new discussion?

But this issue is specifically for multicall (not batching).

Does that all make sense? :)

PfanP commented 3 years ago

Thanks for your reply @ricmoo . I've already checked all GitHub issues explaining batch calls. https://github.com/ethers-io/ethers.js/issues/62#issuecomment-343002204 Is it what web3 BatchRequest does? I thought batch call chunk txs into one block to optimize gas

ricmoo commented 2 months ago

Cleaning up some older issues.

Ethers v6 fully supports batching internally and the MulticallProvider package has full multicall support.

Let me know if some aspect of this issue is still not addressed.

Thanks! :)