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

4

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!

Fruiterer answered 6/5, 2018 at 15:21 Comment(0)
C
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 [email protected]

Then I ran the same contract which has deployed successfully.

Good luck!

Cuomo answered 7/5, 2018 at 14:58 Comment(1)
This along with the '0x' adding before the bytecode fixed same issue I had, I was about to give up on that udemy course.Stephanistephania
D
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.

Dewhurst answered 6/5, 2018 at 15:21 Comment(2)
This solution makes way more sense! Should be the accepted answer. Thanks for this, I was stuck on it for awhile!Sharecropper
This alone did not solve my problem, I had to downgrade Truffle to 0.0.3 to get this to work.Chacma
U
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

Unteach answered 11/5, 2018 at 14:11 Comment(0)
M
0

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

Mystic answered 14/10, 2021 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.