-1

I use sendSignedTransaction to send the contract and return contractAddress: null

      let rpcUrl = "https://ropsten.infura.io/v3/6756e6f8f0e34482a262a7cf1d15f2c2";
  var web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
  var input = {
    language: 'Solidity',
    sources: {
      'HelloWorld.sol': {
        content: 'pragma solidity ^0.5.10; contract HelloWorld { function f() public { } }'
      }
    },
    settings: {
      outputSelection: {
        '*': {
          '*': ['*']
        }
      }
    }
  }

  var compiled = solc.compile(JSON.stringify(input))
  var output = JSON.parse(compiled);
  var bytecode = output.contracts["HelloWorld.sol"]['HelloWorld'].evm.bytecode.object
  const privateKey = Buffer.from(
    'my privatekey',
    'hex'
  )

  web3.eth.getTransactionCount("0x146DD8c053d94354C75783Ff8846012C8907Df17").then(function (transactionCount) {
    console.log(web3.utils.toHex(transactionCount));
    var txParams = {
      nonce: web3.utils.toHex(transactionCount),
      gasPrice: web3.utils.toHex(1000),
      gasLimit: web3.utils.toHex(1000000),
      to: '0x0000000000000000000000000000000000000000',
      value: '0x00',
      data: "0x" + bytecode,
    }
    const tx = new Tx(txParams, { 'chain': 'ropsten' })
    tx.sign(privateKey)
    const serializedTx = tx.serialize()

    var receipt=null;

    web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
      console.log(err);
      console.log(hash);
      while (receipt == null) {

        receipt = web3.eth.getTransactionReceipt(hash);

      }

      console.log('Contract address: ' + receipt.contractAddress);





  });
New contributor
qi li is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0
{ blockHash:

'0x6a77b44ca49bd3a822d7a033251cc425941c6a184b555c3242831f4180ff7ebf', blockNumber: 6183868, contractAddress: null, cumulativeGasUsed: 5492997, from: '0x146dd8c053d94354c75783ff8846012c8907df17', gasUsed: 29740, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: true, to: '0x0000000000000000000000000000000000000000', transactionHash: '0x44adb75d7ee2d0fa0ba939784354f9b0fe512887d988711296995973c20be4a1', transactionIndex: 12 }

New contributor
qi li is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Please edit your own question if you want to add more details. – Ismael Aug 14 at 14:52
0

You send the transaction to the "zero" address "0x0000000000000000000000000000000000000000", which itself is a valid address. For ethereum it looks like a transfer of ether.

To create a contract you have to send it to the "null" address, removing "to" in the txParams should be enough.

var txParams = {
  nonce: web3.utils.toHex(transactionCount),
  gasPrice: web3.utils.toHex(1000),
  gasLimit: web3.utils.toHex(1000000),
  // to: '0x0000000000000000000000000000000000000000', <--- removed
  value: '0x00',
  data: "0x" + bytecode,
}

Your Answer

qi li is a new contributor. Be nice, and check out our Code of Conduct.

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.