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.
Best Answer
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