0

I am trying to send ethers from one address to another address on the ropsten network but I get the message "undefined".

Here is my code:

web3.eth.getTransactionCount(account1, (error, txCount) => {
    //build a transaction object
    const txObject = {
        nonce: web3.utils.toHex(txCount),
        to: account2,
        value: web3.utils.toHex(web3.utils.toWei('0.3', 'ether')),
        gasLimit: web3.utils.toHex(21000), 
        gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))

    }

    //console.log(txObject)

    //sign transaction with private key of sender
    const tx = new Tx(txObject)
    tx.sign(privateKey1)

    //serialize the transaction
    const serializedTransaction = tx.serialize()
    const raw ='0x' + serializedTransaction.toString('hex')

    //broadcast transaction to the network
    web3.eth.sendSignedTransaction(raw, (error, txHash) => {
        console.log(txHash)
    })


})

Thank you for your help

Take care

Pierre

  • Print the error too please. – goodvibration Jul 20 at 20:10
  • thanks for replying. I get only the word "undefine" as an error – Pierre Jul 20 at 20:17
  • Try to follow this example from the official documentation. – goodvibration Jul 20 at 20:32
  • Thank you Goodvibration – Pierre Jul 21 at 18:02
0

If you're not on the mainnet, you have to specify the network :

const tx = new Tx(txObject, { chain: 'ropsten'}) 

That's because your signature will differ from one network to another (protection against replay attacks, cf EIP-155 : https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md).

| improve this answer | |
  • Thank you Clement – Pierre Jul 21 at 18:02

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.