Ganache VM Error – Processing Transaction Fails Due to Out of Gas Exception

ganachejavascriptweb3js

I am deploying my contract using web3 by taking values (just copy paste) from Etherscan (i.e. abi, contract creation code, etc.) and then deploy my contract on Ganache (gui). When i run the my code through node deploy.js following error displayed (However, on Ganache GUI, that command shows that my contract has been deployed and also shows its address).

Here is my code:

 async function deployWithCreationCode(){

     var contract = new web3.eth.Contract(abi);
    var dc = await contract.deploy({data: crCode})
          .send({
              from: account1,
              gas: 6721975, // i cannot assign more gas above gas limit of ganache... is it possible to increase this limit.... ?
              gasPrice: '30000000'
          });
return dc._address;

}

Here is my error:

UnhandledPromiseRejectionWarning: Error: Returned error: VM Exception while processing transaction: out of gas

Note: If size of my contract creation code is less, then no error at all.

Best Answer

I cannot assign more gas above gas limit of ganache... is it possible to increase this limit.... ?

Yes, start Ganache with --gasLimit=someHigherValue.

That being said, note that 6721975 is already by itself a pretty high value for a single transaction, so your deployment most likely fails for a different reason ("out of gas" is unfortunately quite a misleading error-message, but I think that Web3.js dev team are aware of that and are working towards fixing it in a future release).

In order to check whether or not your gas value is the problem here, replace this:

send({from: account1, gas: 6721975, gasPrice: '30000000');

With this:

estimateGas({from: account1);

And then make sure that it completes successfully.

If it does, then your gas value is indeed the problem, and you should replace it with whatever the call to estimateGas has returned.

Otherwise, your transaction reverts for a different reason, most likely related to the contract function itself (i.e., your contract's constructor).


One more thing to notice (which I recently encountered myself and took a lot of effort in figuring out):

Ethereum has a contract code-size limit of 24KB.

If you contract's code-size exceeds this limit, then no matter what you do - whether it's in your contract code or in your deployment code - the contract deployment will fail.

How to check your contract's code-size:

  1. If you're compiling via solc executable, then make sure that the size of the output bin file does not exceed 48KB
  2. If you're compiling via solc.js (for example, via Truffle), then make sure that the size of the bytecode field in the output json file does not exceed 48KB

Why 48KB?

2 hexdecimal characters = 2 bytes in the text file = 1 byte in the actual bytecode.