5

I am trying to Deploy a beginners contract onto the rinkeby test network through Infura node. It is throwing the following error.

(node:35183) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: The contract code couldn't be stored, please check your gas limit.
(node:35183) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Contract

    pragma solidity ^0.4.17;

    contract Inbox {
        string public message;

        function Inbox(

string initialMessage) public {
        message = initialMessage;
    }

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

I used some console logs to determine where the error is being thrown, I have marked it below with a //COMMENT.

Deploy script

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

const provider = new HDWalletProvider(
    'word frog gentle happy chicken book sneeze provide stick handle four fan',
      'https://rinkeby.infura.io/myStringOfRandomLetters'
);
const web3 = new Web3(provider);

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

    console.log('Attempting to Deploy from account', accounts[0]);

//ERROR OCCURS HERE
    const result = await new web3.eth.Contract(JSON.parse(interface))
        .deploy({ data: bytecode, arguments: ['Hi there!'] })
        .send({ gas: '1000000', from: accounts[0] });

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

Things I have tried to fix it:

Increasing the gas limit to 2,000,000, 4,000,000 and decreasing to around 500,000 and 21,000. I hit a "too small amount of gas" error, so I know gas amount is not the issue.

It may have been the npm module "truffle-hdwallet-provider" not installing properly, so I uninstalled, then sudo installed, then found the error fix on the internet (add --unsafe-perm to the commmand). like below

$ sudo npm install truffle-hdwallet-provider --unsafe-perm

still same error. I noticed another identical question Here however (as of now)it is also unanswered.

  • 1
    this Deploy script you're using - what is that? Is that another file? Cause all the tutorials I've seen say you're supposed to be doing all this using the truffle.js file. And then doing a Migrate in Terminal to get all this deployed. So what are you doing? – Sirab33 Dec 2 '18 at 22:05
  • @Sirab33 the code I'v posted abpove is a deployment script. the actually deployment happens in const result = await new web3.eth.Contract(JSON.parse(interface)) .deploy({ data: bytecode, arguments: ['Hi there!'] }) .send({ gas: '1000000', from: accounts[0] }); It uses HDWalletProvider to connect to an infura node, which web3 uses to deploy through. – Finn Rothacker Dec 4 '18 at 22:37
7

I added '0x' + in front of the bytecode in the .deploy to make it Work.

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

If there's no '0x' the bytecode will convert the whole string to hexadecimal, which will be double the size and throw the gas error.

I also re-installed truffle wallet provider using

$ sudo uninstall truffle-hdwallet-provider

and then

$ sudo install --save truffle-hdwallet-provider when inside my project folder.

I also updated git by following this tutorial

This answer is inspired from the answer HERE

1

Adding the '0x' to the bytecode worked for me, didn't need to reinstall the wallet provider.For some reason when I didn't add the '0x' to the bytecode, the I ended up using all the gas.

If you check https://rinkeby.etherscan.io/ and scan for the account you used to deploy, it will clearly show how much gas has been used. before the '0x' it shows that all gas was used up (3,000,000 gas), but after the '0x' is added it shows 281,572 gas has been used.

const Web3 = require('web3');
const Compiler = require('./Compiler');
const HDwalletProvider = require('truffle-hdwallet-provider');
const {interface,bytecode} = Compiler.parse('Inbox');
const walletProvider = new HDwalletProvider('Mnemonic','Rinkeby_link');
const web3 = new Web3(walletProvider);

async function deploy(){
  let accounts = await web3.eth.getAccounts();
  console.log('Deploying from Contract : ',accounts[0]);
  let details = await new web3.eth.Contract(JSON.parse(interface)).deploy({data:'0x'+bytecode,arguments:['Msg for rinkeby']}).send({from:accounts[0],gas:'3000000'});
  console.log(details.options.address);
}
deploy();
0

I believe that you need one await on deploy(...) and one await on send(...).

Something like this:

let x = new web3.eth.Contract(JSON.parse(interface));
let y = await x.deploy({ data: bytecode, arguments: ['Hi there!'] });
let z = await y.send({ gas: '1000000', from: accounts[0] });
console.log('Contract deployed to', z.options.address);

And it looks like you also need an await on the call to deploy(), since this is an async function.

  • I added all your code, no change. When I added an await to the deploy(); at the bottom, it threw an error of "unexpected identifier". So I deleted it. I'm still lost, unfortunately. – Finn Rothacker May 5 '18 at 10:05
0

I am pretty sure its due to latest version 0.0.5 of truffle-hdwallet-provider. If you uninstall it and reinstall version 0.0.3, this problem won't appear and you can go on the Udemy course.

  • I was wrong initally, there is version 0.0.3 avaliable. I have tried up update it and it cannot install. I will update my git and see if it works then. – Finn Rothacker May 6 '18 at 12:46
  • I can't install 0.0.3. Instead I just added a '0x' + inside the data tag of .deploy. As seen in this answer HERE – Finn Rothacker May 7 '18 at 3:08

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.