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

estimateGas using gasleft() #39

Closed
sebastiantf opened this issue Sep 2, 2022 · 9 comments
Closed

estimateGas using gasleft() #39

sebastiantf opened this issue Sep 2, 2022 · 9 comments

Comments

@sebastiantf
Copy link

sebastiantf commented Sep 2, 2022

Would it be possible to add another utility function equivalent to do multiple eth_estimateGas with some gasleft() magic ✨

Guess its too hard to get it right

@mds1
Copy link
Owner

mds1 commented Sep 2, 2022

Sorry what exactly do you mean here? eth_estimateGas is an off-chain RPC call so I don't quite understand the question 😅

@sebastiantf
Copy link
Author

Apologies for the confusion. I meant a function that can be used to estimate gas for multiple transactions in a single call, that returns those estimates separately for each call.

Similar to aggregate(), but only the return values would be gas usage, instead of actual call results

@mds1
Copy link
Owner

mds1 commented Sep 2, 2022

In theory you could, but it won't be very accurate or useful—the sum of the individual call gas estimates won't equal the aggregated call gas limit / gas usage for various reasons. Gas costs vary by a lot of factors cold/warm addresses, cold/warm storage slots, different refund amounts based on total gas usage, etc.

What is your use case for needing estimates of individual calls if they are going to be submitted together via something like aggregate?

@sebastiantf
Copy link
Author

sebastiantf commented Sep 2, 2022

Yeah, it wont be accurate. The use case is to just be able to estimate gas usage of multiple transactions in a single RPC call. And the individual calls need not exactly be submitted together. Yes, that does cause the gas costs to vary very much. But just wondering if its still possible to get close to it and since Multicall is already a public good with very useful utility functions, something like this would also belong here.

@mds1
Copy link
Owner

mds1 commented Sep 2, 2022

The JSON-RPC spec supports batching, so you can batch multiple individual eth_estimateGas calls into a single request, which it sounds like is exactly what you want

@sebastiantf
Copy link
Author

sebastiantf commented Sep 2, 2022

Yes, but I had seen Alchemy documentation recommending against it here:

And I believe it also adds to the CUs and pushes the rate limits -- prevention of which is another advantage of batching them

@sebastiantf
Copy link
Author

sebastiantf commented Sep 2, 2022

But I guess JSON RPC batching is the best bet atm among the options

@mds1
Copy link
Owner

mds1 commented Sep 2, 2022

Yea either batch or individual txs is probably your best bet. Gas estimation can get tricky and I don't think solving it at the contract level is too useful / the right solution. It will also simply not work for a lot cases, such as when a call needs to come from a certain sender.

The nodes use a binary search to find the gas limit so the tx gets executed many times, so does make sense if it's pricey in terms of CUs unfortunately 😕

@mds1 mds1 closed this as completed Sep 2, 2022
@sebastiantf
Copy link
Author

JSON RPC batching is very inefficient if aiming for performance/speed: ethers-io/ethers.js#62 (comment)

Apparently JSON RPC batching executes the requests are performed serially.
I tried it out, and multiple concurrent requests are way faster than batching

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

No branches or pull requests

2 participants