I am trying to deploy contract using web3js in nodejs on ganache cli test server, but it fails with error as follows :

                                throw new Error('The contract code couldn\'t be stored, please check your gas amount.');
Error: The contract code couldn't be stored, please check your gas amount.
at Object.callback (D:\Thrive\PoC\NodeWebAPI\node_modules\web3\lib\web3\contract.js:149:43)
at D:\Thrive\PoC\NodeWebAPI\node_modules\web3\lib\web3\method.js:142:25
at D:\Thrive\PoC\NodeWebAPI\node_modules\web3\lib\web3\requestmanager.js:89:9
at XMLHttpRequest.request.onreadystatechange (D:\Thrive\PoC\NodeWebAPI\node_modules\web3\lib\web3\httpprovider.js:118:13)

The code snippet is as follows :

    var web3 = connection.getWeb3Object(config.BlockChainServer());

    var contractAbi = contractDetails.getContractAbi(contractName);

    var contractByteCode = contractAbi.bin;

    var gasEstimate = web3.eth.estimateGas({data: contractByteCode});


    var contractObject = web3.eth.contract(contractAbi);

    var contract = contractObject.new({from: account, gas:1000000,data: "0x" + contractByteCode});

    var receipt = web3.eth.getTransactionReceipt(contract.transactionHash);

    if (receipt && receipt.contractAddress) {
        console.log("Your contract has been deployed at " + config.BlockChainServer + " | Contract address -" + receipt.contractAddress);

I have tried sending varying amounts of gas, but I am encountering the same issue. What could be the problem ?

  • have you tried setting a different gas limit? You can't actually send gas hence the question. Jul 3 '18 at 15:26
  • Yes, I did try setting different gas limits. I have also tried creating the contract without passing the gas. The issue persists Jul 4 '18 at 4:27

Remember gas value is always on Wei, try setting it to this value. 4712388 (which is truffle's default gas value). Also you could try deploying another small contract to check if the issue is with your blockchain or specific to this contract.

  • I tried deploying a very small contract but I get the same error. I am running this on ganache-cli test server Jul 4 '18 at 5:23
  • what is the command you use to execute ganache-cli? If you try to obtain a list of all accounts, from your .js file does it work?
    – Roberto C.
    Jul 4 '18 at 16:40
  • Yes, it does work. I am able to fetch the list of accounts ! Jul 12 '18 at 7:14

After trying out some more ways, I figured out the issue was due to not passing the proper byte-code information.The following piece of code was not correct.

var contractByteCode = contractAbi.bin;

I changed it to : var contractByteCode = contractAbi.Abi.bytecode;

Also, as @RobertC suggested in the earlier answer, deploying a smaller contract helped !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.