Calling a non-view function within a contract in order to estimate gas for its execution #1918
Replies: 7 comments
-
Can you please provide more info on this? Like what problems are there with
I guess you mean a |
Beta Was this translation helpful? Give feedback.
-
@zemse thanks for your reply Basically, I do not get an accurate gasLimit from my provider when using Well, my thought was using Thanks in advance |
Beta Was this translation helpful? Give feedback.
-
I think Due to the complex txs, if the estimate is going to be different everytime, you can try taking multiple estimates and the max of these estimates could be a number that would probably be close to the longest execution path. More the tries, more it will be closer. You can make the final Edit: Also, you mentioned that you use geth. Is that correct or do you use the evm that comes with hard hat? Just asking because I am suspecting some issues with estimate gas in hardhat evm. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your tips @zemse ! I will go for it and get back to you with some feedback!
I am using my own geth, but yes, I am afraid something is wrong with hardhat gas estimate config... |
Beta Was this translation helpful? Give feedback.
-
@zemse suggestions are spot on. One additional thought I have is that Hardhat might use ganache under the hood, which may be part of the problem. Otherwise maybe your contract uses If you find a gas limit that works well, you can add it to the ABI. For a JSON ABI, use the property I personally usually just pick some large value like 500k gas. Once the execution is successful for a few different runs, I look at the actual gas used and pick a value higher than the worst case that looks nice. For example, if I see 55678, 67332 and 67312 gasUsed, I would probably pick 85000 as a hard-coded gas limit in the ABI. It does depend a bit on the code though. If there are arbitrary loops or external calls you may want to make something more parameterized... |
Beta Was this translation helpful? Give feedback.
-
Did that work for you? If so I'll close this issue. :) |
Beta Was this translation helpful? Give feedback.
-
Yes, sorry of the silence. As you both said, the interaction with complex contracts made difficult to estimate the tx gasLimit. So, my best option was to perform multiple tries to estimateGas, and I was thinking about the possibility of sending a trial tx to a mainnet-fork network in order to make sure the gasLimit is high enough. Nevertheless, IMO the easiest workaround here is to provided a large amount of gas (as much as your wallet can provide). |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am using
hardhat-ethers
plugin for hardhat and facing some problems with gas estimates while executing transactions within a contract (mainnet). I am facing some problems estimating the gasPrice for the transaction. Hardhat execute aneth_estimateGas
to my node in order to calculate the gasLimit of the tx but the gasLimit calc provided by my node is awful...So, I am searching for alternatives to calculate my tx gasLimit and I wonder if calling the non-view function with a simple
call
could be the way.I know this is the ethers.js repo, so my question is, how can I make a simple
call
to a non-view function within a contract?Using vanilla ethers it could be done creating a contract factory with no wallet (only provider), am I right?
Using hardhat-ethers and
getContractFactory
helpers, is it possible to do it? (as signers are included by default).Thanks in advance,
Beta Was this translation helpful? Give feedback.
All reactions