How meta estimating gas cost before broadcasting.

How can I calculate gas cost for myfunc() in web3js?

function myfunc(){
   require(msg.sender == owner)

await contractInstance.myfunc.sendTransaction(); //it sends the transaction and wastes the gas if `myfunc` will fail.

Is there any best way to know gas cost before call solidity myfunc(), its saves gas for me ? (Similar like metamask)


2 Answers 2


web3 has an estimateGas method you can use to estimate the gas required for the transaction. You can use it for contract methods as well.


Yes there is way but not that much accurate. Just add 10 or 20% extra gas.


Please have a look below link

how to estimate gas cost?

  • How do you add 10 - 20%?
    – Shamoon
    Jun 28, 2018 at 15:31

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