0

I am stuck with an issue as the contract works fine with remix ide and local ganache provider but when I am trying to use truffle with infura apis to deploy the contract over rinkeby testnet, I keep getting error.

I have tried with different gas limit values but nothing works and error is consistent.

Error :

(node:9286) UnhandledPromiseRejectionWarning: Error: The contract code couldn't be stored, please check your gas limit.
    at Object.callback (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-core-method/src/index.js:333:46)
    at sendTxCallback (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-core-method/src/index.js:486:29)
    at /home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-core-requestmanager/src/index.js:147:9
    at /home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/index.js:152:9
    at /home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/node_modules/async/internal/once.js:12:16
    at replenish (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/node_modules/async/internal/eachOfLimit.js:61:25)
    at /home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/node_modules/async/internal/eachOfLimit.js:71:9
    at eachLimit (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/node_modules/async/eachLimit.js:43:36)
    at /home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/node_modules/async/internal/doLimit.js:9:16
    at end (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/index.js:127:5)
    at /home/vineet/store/project/block-chain/ethereum/inbox/node_modules/web3-provider-engine/subproviders/provider.js:20:5
    at XMLHttpRequest.request.onreadystatechange (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/httpprovider.js:118:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/home/vineet/store/project/block-chain/ethereum/inbox/node_modules/xhr2/lib/xhr2.js:469:24)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

truffle.js file

module.exports = { networks: { ropsten: { host: "127.0.0.1", port: 8545, network_id: '*', gas: 4700000, gasPrice: 20000000000, }, }, };

deploy.js file contents:

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

const provider = new HDWalletProvider( 'account mnemonic',
'infura api link' );

const web3 = new Web3(provider);

const deploy = async () =>{ const 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!'] }) .send ({gas: '2000000', from: accounts[0] });

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

deploy();

6
  • What is your truffle.js file configuration? What is doing the failing deployment script? Did you try increasing the gas amount following the documentation?
    – Ismael
    May 22, 2018 at 5:49
  • I have edited the post to provide more details and tried many different values as gas amount but nothing worked. May 23, 2018 at 6:07
  • You've tried to deploy it on Kovan or Rinkeby? Just to make sure it's not a Ropsten error (because it has attacks lots of times).
    – CPereez19
    May 23, 2018 at 6:21
  • I tried to deploy it on Rinkeby. May 23, 2018 at 6:25
  • 1
    Does your bytecode data start with '0x'? If it does not you have to prepend it before passing to deploy, ie something like this .deploy({ data: '0x' + bytecode, .. }). Another possibility is you are setting the gas limit at 2M when you send the contract, it might be low if your contract is complex, try increasing it.
    – Ismael
    May 23, 2018 at 14:16

2 Answers 2

0

Finally it worked, after a lot of efforts i followed the advice given by @Ismail in the comment above.

Does your bytecode data start with '0x'? If it does not you have to prepend it before passing to deploy, ie something like this .deploy({ data: '0x' + bytecode, .. }). Another possibility is you are setting the gas limit at 2M when you send the contract, it might be low if your contract is complex, try increasing it.

The bytecode didn't start with '0x' so i had to prepend '0x'!!!

deploy.js file contents after changes :-

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

const provider = new HDWalletProvider( 'account mnemonic', 'infura api link' );

const web3 = new Web3(provider);

const deploy = async () =>{ const 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 : '0x' + bytecode,arguments: ['hello!'] }) .send ({gas: '5000000', from: accounts[0] });

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

deploy();

0

I've had a similar hassle with it and my solution was to migrate with ethers.js. It's much stabler than web3.js and I am not having to deal with the truffle config anymore. I've even abstracted my solution in a simple library. Here is a snipet for infura deployment I've used to deploy using Private Key:

const etherlime = require('etherlime');

const ICOTokenContract = require('./build/contracts/ICOToken.json');

const randomAddress = '0xda8a06f1c910cab18ad187be1faa2b8606c2ec86';

const defaultConfigs = {
    gasPrice: 20000000000,
    gasLimit: 4700000
}

const deployer = new etherlime.InfuraPrivateKeyDeployer('Your Privste KEY', 'ropsten', 'Your infura API key', defaultConfigs);

const runICODeployment = async () => {
    const contractWrapper = await deployer.deploy(ICOTokenContract);
    const transferTransaction = await contractWrapper.contract.transferOwnership(randomAddress);
    const result = await contractWrapper.verboseWaitForTransaction(transferTransaction.hash, 'Transfer Ownership');
}
runICODeployment()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.