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

3
  • Print the error too please. Commented Jul 20, 2020 at 20:10
  • thanks for replying. I get only the word "undefine" as an error
    – Pierre
    Commented Jul 20, 2020 at 20:17
  • Try to follow this example from the official documentation. Commented Jul 20, 2020 at 20:32

1 Answer 1

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).

0

Your Answer

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

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