Search code examples
ethereumganache

ethereum ganache is failing with "Exceeds block gas limit" even though I have enough balance


I am testing an ethereum smart contract with ganache and web3.

const Web3 = require('web3');
const web3 = new Web3(provider);
const contract = new web3.eth.Contract(abi, contractAddress);

web3.eth.getBalance(contractOwner).then(console.log); // this returns 99953972490000000000

const sendTxOptions = {from: contractOwner, gas: 1000*1000*10}
contract.methods.my_method().send(sendTxOptions).then(console.log);

(node:80755) UnhandledPromiseRejectionWarning: Error: Returned error: Exceeds block gas limit

1000*1000*10 is less than 99953972490000000000. Why is this failing?

Note: I've already searched for other similar questions, such as this one, but they do not answer my question. https://ethereum.stackexchange.com/questions/26577/error-vm-exception-while-processing-transaction-out-of-gas


Solution

  • It's ETH balance (not gas):

    web3.eth.getBalance(contractOwner).then(console.log); // this returns 99953972490000000000
    

    Use estimateGas instead: https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#methods-mymethod-estimategas

    my_method() consumes more gas than block gas limit. You can increase the gas limit for the block for Ganache, but it is better to optimize the function, otherwise in real network there may still be problems.