Gas Estimation Error – Root Cause of ‘Gas Estimation Failed’ in Solidity Contracts

contract-developmentevmremixsolidity

Rest of the error:

The transaction execution will likely fail. Do you want to force
sending? Internal JSON-RPC error.

I got that message on several cases, usually on different mistakes.

What's the cause for that error and is there a way which can get me towards understanding the problem faster.

In most cases I'm trying to rewrite everything chunk by chunk and check when I don't get that error.

Best Answer

Among other things, every Ethereum transaction has attribute named gasLimit or simply gas. This is the maximum amount of gas transaction is allowed to consume. When sending transaction via Web3 API, you may specify value for this attribute explicitly:

web3.eth.sendTransaction ({
  from: 0x0123456789012345678901234567890123456789,
  to: 0x9876543210987654321098765432109876543210,
  gas: 90000 // <-- Here is gasLimit
});

This parameter is optional, and if it is omitted, Web3 API implementation will try to estimate proper value by first executing transaction locally before actually broadcasting it to the network. This local execution is similar to how miners execute transaction before including them into blocks.

In case local execution succeeds, Web3 API implementation counts how much gas transaction actually consumed and uses this value to populate gasLimit parameter. Though, in case local execution failed, e.g. smart contract reverted it, gas estimation failed error is reported. You may still broadcast such transaction by explicitly specifying gasLimit for it.

Related Topic