TransWikia.com

problem with sending ethers on ropsten network

Ethereum Asked on August 26, 2021

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

One Answer

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

Answered by clement on August 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP