17

I am trying to deploy my simple solidity smart contract onto the Rinkeby Network but I keep getting the error:

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

My solidity code is simple

pragma solidity ^0.4.18; 

contract Greetings{ 
  string public message; 

  function Greetings(string initialMessage) public{ 
    message = initialMessage;
  }  

  function setMessage(string newMessage) public {
    message = newMessage;
  }  
}

and my deploy script is:

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

const provider = new HDWalletProvider(  
  'twelve word mnemonic...', 
  'https://rinkeby.infura.io/GLm6McXWuaih4gqq8nTY'    
);

const web3 = new Web3(provider);

const deploy = async () => {
    accounts = await web3.eth.getAccounts(); 

    console.log('attempting to deploy from account',accounts[0]);

    const result = await new web3.eth.Contract(JSON.parse(interface)) 
      .deploy({data:bytecode, arguments:['Hello World']})      
      .send({from: accounts[0], gas:'1000000'});                              

    console.log('Contract deployed to', result.options.address); 
};

deploy();

Funny thing is, I used to be able to deploy successfully, but when i created a new project and re did the same code, i get this error now. Please help!

Jason
  • 1,059
  • 1
  • 13
  • 32

4 Answers4

36

Had exactly same problem! Seems it is caused by the bug in the "truffle-hdwallet-provider" version 0.0.5. During the udemy course it was using "0.0.3" apparently.

If you do the following should be okay, it worked for me.

npm uninstall truffle-hdwallet-provider
npm install --save truffle-hdwallet-provider@0.0.3

Then I ran the same contract which has deployed successfully.

Good luck!

Kutay Ozdogru
  • 376
  • 3
  • 2
  • This along with the '0x' adding before the bytecode fixed same issue I had, I was about to give up on that udemy course. – mega_creamery May 30 '20 at 15:48
17

This issue can be solved by adding the '0x' as the prefix of the bytecode:

.deploy({ data: '0x' + bytecode, arguments: ['Hi there!'] })

More information is at https://ethereum.stackexchange.com/a/47654.

Huy Vo
  • 2,418
  • 6
  • 22
  • 43
3

I believe bytecode is being treated as a single number rather than a series of bytes. Instead of submitting data:bytecode, try:

data:'0x0' + bytecode

it will "preserve" bytecode value as a string

alernerdev
  • 2,014
  • 3
  • 19
  • 35
0

Also just remove the gas field let metamask decide the gas limit. this way works for me.