My smart contract has a centralized web app as front end using Ethers.js to communicate (in a Flutter app). When I'm executing a function, I need to pass the gas limit. How can I know what is a good amount to set, so that it's not too much and it also won't run out of gas? On Rinkeby, I found the limit of 1000000 through trial&error, but I can't afford to do the same on Mainnet. I saw that when using Remix to call the function, Metamask does its own estimation, but if called through the custom frontend it just takes whatever it finds in the transaction object. So I guess I'd like to have the same estimation in my app that works through Remix. Thanks in advance for any input!

  • ethers.js estimates the gas automatically. Just leave the "gasLimit" field empty. Commented Oct 21, 2021 at 10:42

1 Answer 1

  1. Check getFeeData int etherjs documentation.
  2. Since the gas cost of solodity function is pretty much equal every time you can make average of 10, 20 calls on test net
  3. You can calculate the feePrice*gas*ethPrice to get some approximation what it would cost

EDIT: Updated for an EIP-1559 transaction

  • 1
    You probably want to use getFeeData instead, since the gas price is for legacy transactions.
    – Morten
    Commented Oct 21, 2021 at 14:57
  • @Morten Yep, you are correct! Commented Oct 22, 2021 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.