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
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:With this:
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 toestimateGas
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:
bin
file does not exceed 48KBbytecode
field in the outputjson
file does not exceed 48KBWhy 48KB?