0

I'm doing this with web3.js and infura.

Is there a way I can know the contract address?

const myContract = new web3.eth.Contract(myContractAbi);

let deploy = myContract.deploy({
  data: '0x' + myContractByteCode
  arguments: [ ]
});

// get the number of transactions sent so far so we can create a fresh nonce
web3.eth.getTransactionCount(fromAddress).then(txCount => {
  const rawTransaction = {
    nonce: web3.utils.toHex(txCount),
    gasLimit: web3.utils.toHex(500000),
    gasPrice: web3.utils.toHex(gasPrice * 1e9), // gasPrice Gwei
    from: fromAddress,
    value: '0x00',
    data: deploy.encodeABI()
  };

  const privateKeyHex = new Buffer(privateKey, 'hex')
  const tx = new Tx(rawTransaction);
  tx.sign(privateKeyHex);
  const serializedTx = tx.serialize().toString('hex');

  web3.eth.sendSignedTransaction('0x' + serializedTx, (err, result) => {
    if (!err) {
      console.log(result);
    }
  });
});

console.log(result); returns TXID

1

Problem solved:

web3.eth.sendSignedTransaction('0x' + serializedTx, (err, result) => {
  if (!err) {
    console.log(result);
  }
}).on('receipt', (receipt) => {
  console.log(receipt.contractAddress);
});

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.