0

I am having trouble deploying a smart contract to the Ropsten testnet. I have dug through a lot of tutorials and questions and I cannot seem to find an suitable answer. Thanks so much for you time!

Currently, the code runs and appears to be successfully deploying. However, it will always use all the gas I provide and never actually deploy. Here is the account I am trying to deploy from: etherscan. You can see that whenever I deploy a contract, it errors out with not enough gas. As I understand it, there should be plenty of gas provided.

Here is my code in question:

const endpoint = 'https://ropsten.infura.io/<mykey>';
const solc = require('solc');
const Web3 = require('web3');
const EthTx = require('ethereumjs-tx');
const web3 = new Web3(new Web3.providers.HttpProvider(endpoint));

const addr1 = "0xa8f157e9c445faad451bd07e6e8780db76f066b3";
const pKey1 = 'd6be926bd949...';               //private key
const pKey1x = new Buffer(pKey1, 'hex');

const source = `contract HellowWorld {         //source in plaintext for readability
  function displayMessage() constant returns (string) {
    return "Hello";
  }
}
`
const compiled = solc.compile(source);

const estGasPrice = new Promise((resolve) => {
  resolve(web3.eth.getGasPrice())
});

const getNonce = new Promise((resolve) => {
  resolve(web3.eth.getTransactionCount(addr1))
});

Promise.all([estGasPrice, getNonce])
  .then(([gasAmt, nonce]) => {
    let tx = new EthTx({
      from:     addr1,
      nonce:    Web3.utils.toHex(nonce),
      value:    Web3.utils.toHex(10000),
      gasLimit: Web3.utils.toHex(10000000),
      gasPrice: Web3.utils.toHex(2000000000),
      data:     Web3.utils.toHex(compiled.contracts[":HellowWorld"].bytecode)
  })
  tx.sign(pKey1x)
  let serializedTx = `0x${tx.serialize().toString('hex')}`
  web3.eth.sendSignedTransaction(serializedTx, (err, data) => {
    return !err
      ? console.log(`Success! Data: ${data}`)
      : console.log(`Error: ${err}`)
    })
  })
  .catch((err) => {
    console.log('Error' + err)
  })

And for reference, the error I get on the terminal when I run this code is:

Error: Transaction ran out of gas. Please provide more gas:

My intuition is that I haven't formatted the raw transaction correctly. Or it may be that I don't understand how much gas this thing needs. Whatever the case, I appreciate any insight you can provide!

EDIT: My code is refactored, should make more sense

bumped to the homepage by Community 2 days ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • Did you try deploying your contract to Ropsten via Remix? I recently also had a problem with deploying my contract to Rinkeby with my own script, but it was deployed correctly with Remix. I know that this is not the answer, but maybe you need solution more than the answer ;) – Mike A. Clearance May 9 '18 at 5:53
  • That's funny you say that, I did deploy via remix and injected web3 and it worked. I'll try some other test nets today and let you know how it goes. – le_sanglier May 9 '18 at 14:31
0

Your problem is you are converting your bytecode to hexadecimal

data:     Web3.utils.toHex(compiled.contracts[":HellowWorld"].bytecode)

And the bytecode appears to already be in hexadecimal. From Etherscan inputData field

0x3630383036303430353233343830313536313030313035373
630303038306664356235303631303133663830363130303230

You need to only prepend the 0x in front of the bytecode

data:     '0x' + compiled.contracts[":HellowWorld"].bytecode
  • If you are using web3 v1, prepending 0x is no longer required as it is handled by the library if missing. – GViz Jun 18 '18 at 18:05

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.