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!
1 Answer
- Check getFeeData int
etherjs
documentation. - Since the
gas
cost of solodity function is pretty much equal every time you can make average of 10, 20 calls on test net - You can calculate the
feePrice*gas*ethPrice
to get some approximation what it would cost
EDIT: Updated for an EIP-1559 transaction
-
1You probably want to use
getFeeData
instead, since the gas price is for legacy transactions.– MortenOct 21, 2021 at 14:57 -