3

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)

Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
Slim Shady
  • 313
  • 2
  • 16

2 Answers2

0

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.

Howard
  • 421
  • 2
  • 6
0

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

contractInstance.method.estimateGas(ARGS...);

Please have a look below link

how to estimate gas cost?

Jitendra Kumar. Balla
  • 2,154
  • 1
  • 8
  • 15