Openzeppelin-contracts: The error "Error: The contract code couldn't be stored, please check your gas amount." shows up when deploying Crowdsale contract on Ropsten/Rinkeby.

Created on 7 Jun 2018  路  20Comments  路  Source: OpenZeppelin/openzeppelin-contracts

image
I've tried increasing gas to maximum, and tried debugging the code looking for potential issues to no avail. The contract deploy on ganache/TestRPC without an issue.
Could anyone help?

Most helpful comment

@dhruv-karan . Try this in your deploy script.

change these two lines from this.
.deploy({data:bytecode, arguments: ['hi']})
.send({gas:'1000000 ', from: accounts[0]})

to this
.deploy({data: '0x' + bytecode, arguments:['hi']}) // add 0x before bytecode
.send({from:accounts[0]}) // take out the gas field

Your 1st Line
This is ok when you deploy your code in the ganache local test network.
When you deploy to another remote test network like kovan or rinkeby, you should put the '0x' as prefix in bytecode of ABI.

Your 2nd Line
When you test with ganache provider, the gas key should be specified but not in this case.
https://hanezu.github.io/posts/Gas-Limit-Error-when-deploy-Truffle-project-on-Private-Network.html

All 20 comments

Hello @sheraz104. Many things can cause this error, and it's generally misleading. Take a look at https://github.com/trufflesuite/truffle/issues/522
Check that your constructors are passing all the required arguments in the inheritance chain. Also check that your build directory is up to date.

I've received this error as well when an abstract function was not implemented IIRC.

@sheraz104 please provide more info (ideally also upload your full project to github so that we can see how the contracts are deployed).

This is almost definitely an issue with your constructor arguments coming from the migration/deploy script.

Hi, I have the same error message even when I changed the gas to 4700000 higher than default in truffle.js. The default "Migrations.sol" file is as follow:

pragma solidity ^0.4.23;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}

function Migrations() public {
owner = msg.sender;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}

}

Can someone share some more information how to resolve this compilation error?
I have also checked the constructors for the imported contracts "zeppelin-solidity/contracts/crowdsale/emission/MintedCrowdsale.sol" and
"zeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol". Thanks.

After couple of attempts, found the information in "https://github.com/OpenZeppelin/openzeppelin-solidity/issues/358". By changing as per suggestion by spalladino in the deployment script:
"const startTime = web3.eth.getBlock(web3.eth.blockNumber).timestamp + 120"; compilation and migration can be completed.

:laughing: awesome website @shrugs!!! I can see it in the short future as one of the most popular stack overflow answers.

I'm unable to resolve the issue.
I've uploaded the crowdsale code in this google sheet.
https://docs.google.com/document/d/1BcZWr8mh96y0OWN-3TCBUlSRbM1FlJEJCAlKAm1PvL8/edit?usp=sharing
Kindly take a look @shrugs

@Sheraz-dev please upload your code to a github repo so that it's easier for people to use :)

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const {interface,bytecode} = require('./compile');

const provider = new HDWalletProvider(
'XXXXXXX some account newmonix XXXXXXXXXXXXXXXXXXXXXXXXXX',
'XXXXXany infura node XXXXXXXXXXXXXXXXX'
)

const web3 = new Web3(provider)

const deploy = async()=>{
const accounts = await web3.eth.getAccounts()
console.log(accounts[0]);
const result = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data:bytecode,arguments:['hi']})
.send({gas:'1000000 ',from:accounts[0]})
console.log('contract deployed to',result.options.address);

}
deploy()

same problem

share your smart contract code

I got the same problem but when i remove function calling inside my constructor it works fine. Why?

@dhruv-karan . Try this in your deploy script.

change these two lines from this.
.deploy({data:bytecode, arguments: ['hi']})
.send({gas:'1000000 ', from: accounts[0]})

to this
.deploy({data: '0x' + bytecode, arguments:['hi']}) // add 0x before bytecode
.send({from:accounts[0]}) // take out the gas field

Your 1st Line
This is ok when you deploy your code in the ganache local test network.
When you deploy to another remote test network like kovan or rinkeby, you should put the '0x' as prefix in bytecode of ABI.

Your 2nd Line
When you test with ganache provider, the gas key should be specified but not in this case.
https://hanezu.github.io/posts/Gas-Limit-Error-when-deploy-Truffle-project-on-Private-Network.html

image
I've tried increasing gas to maximum, and tried debugging the code looking for potential issues to no avail. The contract deploy on ganache/TestRPC without an issue.
Could anyone help?

this happens to me also, but i got the solution.
open your package.json file
and change the versions as follows:
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"ganache-cli": "^6.0.3",
"mocha": "^5.0.1",
"solc": "^0.4.20",
"truffle-hdwallet-provider": "0.0.3",
"web3": "^1.0.0-beta.26"

and after this remove node-modules and run "npm install" on terminal

@ymekuria
Thanks!! the missing '0x' before bytecode was definitely the solution in my case.
It's a bit frustrating how misleading the errors are...

Hi, I am having the same problem and not getting what is causing it, can anyone help me if I share the code? Thanks!

@romisalve Sure. Please post on https://forum.openzeppelin.com.

Hi, I am having the same problem and not getting what is causing it, can anyone help me if I share the code? Thanks!

.deploy({data: '0x' + bytecode, arguments:['hi']}) // add 0x before bytecode
When you deploy to another remote test network like kovan or rinkeby, you should put the '0x' as prefix in bytecode of ABI.

Deleting the ./build folder and rebuilding solve it for me!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryana picture ryana  路  4Comments

spalladino picture spalladino  路  4Comments

bh2smith picture bh2smith  路  4Comments

mswezey23 picture mswezey23  路  4Comments

xiaoyao1991 picture xiaoyao1991  路  3Comments