Truffle: Error: The contract code couldn't be stored, please check your gas amount.

Created on 15 Apr 2018  路  9Comments  路  Source: trufflesuite/truffle

  • [ ] I've asked for help in the Truffle Gitter before filing this issue.

Issue

I followed tutorial to create a very simple token and crowdsale contract with Truffle and zeppelin-solidity. I managed to successfully migrate to Ganache locally, but when I tried to migrate to ropsten I encountered the following error:

Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.
    at Object.callback (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/contract.js:147:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/method.js:142:1
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:89:1
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-migrate/index.js:225:1
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1
    at Web3ProviderEngine._inspectResponseForNewBlock (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/web3-provider-engine/index.js:231:12)
    at /Users/wenwenchen/Documents/TestCrowdsale/node_modules/web3-provider-engine/index.js:131:14
    at /Users/wenwenchen/Documents/TestCrowdsale/node_modules/async/dist/async.js:473:16
    at replenish (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/async/dist/async.js:993:25)
    at iterateeCallback (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/async/dist/async.js:983:17)
...

I researched online and some answers said that this happened because of trying to deploy abstract contract to the testnet. But it seems this is not the case for me.

Steps to Reproduce

  1. Clone this repository https://github.com/chenwenwen11/TestCrowdsale.git
  2. terminal run: npm install -g truffle
  3. cd to the TestCrowdsale
  4. terminal run: npm install zeppelin-solidity
  5. terminal run: npm install truffle-hdwallet-provider
  6. terminal run: truffle compile
  7. terminal run: truffle migrate --network ropsten

Expected Behavior

Contracts are successfully migrated to Ropsten testnet

Actual Results

screen shot 2018-04-15 at 9 11 04 pm

Environment

  • Operating System: macOS High Sierra
  • Ethereum client: web3
  • Truffle version (truffle version): v4.1.3
  • node version (node --version): v9.8.0
  • npm version (npm --version): 5.6.0
  • solidity version: v0.4.19
  • zeppelin-solidity version: 1.7.0
  • Ganache version: 1.0.2

Most helpful comment

@chenwenwen11 It looks like you are using a Zeppelin contract called TimedCrowdsale which has a constructor that checks to make sure the crowdsale start time value is greater than the chain value now.

function TimedCrowdsale(uint256 _openingTime, uint256 _closingTime) public {
    require(_openingTime >= now);
    require(_closingTime >= _openingTime);

    openingTime = _openingTime;
    closingTime = _closingTime;

In your migrations you are setting startTime to the current system time. When the contract executes now is later than startTime causing the transaction to fail. Could you try adding some spare time to the line below in your migrations and see if that works?

const startTime = Math.round((new Date(Date.now()).getTime())/1000); // <--- Too soon! 
const endTime = startTime + (86400 * 2); // 30 days;

All 9 comments

@chenwenwen11 It looks like you are using a Zeppelin contract called TimedCrowdsale which has a constructor that checks to make sure the crowdsale start time value is greater than the chain value now.

function TimedCrowdsale(uint256 _openingTime, uint256 _closingTime) public {
    require(_openingTime >= now);
    require(_closingTime >= _openingTime);

    openingTime = _openingTime;
    closingTime = _closingTime;

In your migrations you are setting startTime to the current system time. When the contract executes now is later than startTime causing the transaction to fail. Could you try adding some spare time to the line below in your migrations and see if that works?

const startTime = Math.round((new Date(Date.now()).getTime())/1000); // <--- Too soon! 
const endTime = startTime + (86400 * 2); // 30 days;

Hi @cgewecke,really thank you for the answer. I managed to get over that error by adding spare time. However, I run into another error which I have very limited information about it.

/Users/wenwenchen/Documents/TestCrowdsale/node_modules/web3/lib/web3/errors.js:35
        return new Error(message);
               ^
Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/Users/wenwenchen/Documents/TestCrowdsale/node_modules/xhr2/lib/xhr2.js:414:24)
    at ClientRequest.emit (events.js:180:13)
    at TLSSocket.socketErrorListener (_http_client.js:394:9)
    at TLSSocket.emit (events.js:180:13)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at process._tickCallback (internal/process/next_tick.js:114:19)

It would be great if you could share some available documentation or sample contract with me as I might face other generic error during my development process.
Thanks!!

@chenwenwen11 Unfortunately lots of people are seeing this error at the very end of their migrations sequence when using Infura . . . there's an open issue for that at #852. In that case, the deployments are actually successful - e.g. you should be able to see that your contract deployed to Ropsten if you check the contract address displayed in the migrations.

Does #852 describe what you're seeing? If so we should close this as a duplicate and we'll try to get to the bottom of the problem in that issue.

@cgewecke sorry to ask so much. But I could not find relevant info anywhere else thats why I posted here. That error in #852 seems a little bit different from mine, but I am not sure if that is the same case. I tried to migrate the 2nd time after I remove /build folder and re-compiled the project. It successfully migrated to Ropsten testnet. However, I found the error happened again in ropsten when I transfer ETH to the address.
screen shot 2018-04-18 at 10 15 29 am
screen shot 2018-04-18 at 10 15 41 am

The token is not shown as ERC20 token in ropsten instead it is just a normal contract address. Not sure if that is the problem because I found other tokens are shown as ERC 20 token.
screen shot 2018-04-18 at 10 17 16 am
screen shot 2018-04-18 at 10 35 49 am

The following is the details of my migration, you can refer to the addresses in case you need to see the contracts in ropsten testnet https://ropsten.etherscan.io/

Wenwens-MacBook-Pro:TestCrowdsale wenwenchen$ truffle migrate --network ropsten
Using network 'ropsten'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xc6eb76b75c469ebd5b5c32c79e24e31a6d0df2714a56be0bba2c0d48659f28ac
  Migrations: 0xad14fe9f7750a681bcf7c18f9234e056100c28eb
Saving successful migration to network...
  ... 0x81150776a8e6fe0ca370e7fe54bf2d797640ce2c2d2a1aec84cf9f12cfa09430
Saving artifacts...
Running migration: 2_TestCrowdsale.js
  Deploying TestToken...
  ... 0x61627e39b3ff6fdad09a3d31c72fb4d31eab633e105712dc8e0ae5014790b420
  TestToken: 0xbf821ffcc82508e06462c32828593c59ab94313e
  Deploying TestCrowdsale...
  ... 0x5520b149bdbe2678b543f8a8ff33beeefe3bd81b9c1946619c1a21035e8dbab8
  TestCrowdsale: 0x223f507e1b95026d0833f59976b14bb81d003673
Saving successful migration to network...
  ... 0x9761d0f23f4c95df4f887edc56e67ed360b293b1168f58f2db1f7305e3ec5367
Saving artifacts...

Really appreciate your replies. If you think the issue should be handled somewhere else, please let me know where should I look for help. Thank you very much!

@chenwenwen11 Out of curiosity - the error you were seeing with the migrations just stopped happening after your removed the build folder and remigrated?

How are you sending Eth to the contract? One thing I recommend is using the unit tests in Truffle to experiment with writing code to make calls to the contracts using the ganache test client. If these succeed they will usually work on a public network as well.

@cgewecke yes, it stopped after I remove the build folder and re-compiled and re-migrated. I still do not get the part why the token is not an ERC20 token after migrated to ropsten.

I used one of the account in metamask to send to the crowdsale contract address. I have not yet completed the test cases, because I am still exploring. But I did follow tutorial and use truffle console to test on transaction. It works as what is described in the tutorial. Then I suspected that it might be my code logic problem, so I removed the extra code, just use the very basic one in openZeppelin examples, which is also the same as the one in the tutorial that I mentioned above. I can migrate successfully, but still the transactions failed. I tried varies tutorial, but all of them is very basic, cant really provide much info when facing generic errors. I am really wondering if there is any thorough and informative guide or docs out there for me to refer to, other than pestering you guys.

@chenwenwen11 I think you've done the right thing by minimizing the problem

Does the contract you're sending Ether to have a fallback function that is marked payable? I'd really encourage you to read through the Solidity language documentation. It's a lot of information but it's necessary to be familiar with that base of knowledge to debug things effectively.

@chenwenwen11 I'm going to close this for house-keeping because the original topic of the issue seems resolved. Thanks so much for your help diagnosing the Infura migrations problem.

@chenwenwen11 Etherscan doesn't automatically recognize an ERC20 compliant contract. You need to transfer some token to another address first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timothywangdev picture timothywangdev  路  3Comments

TOMOAKI12345 picture TOMOAKI12345  路  3Comments

EmanHerawy picture EmanHerawy  路  3Comments

arunmitteam picture arunmitteam  路  3Comments

ripper234 picture ripper234  路  4Comments