0

I wrote this java script code to send test ether to my other account:

var Tx = require('ethereumjs-tx').Transaction
var Web3 = require('web3')
web3 = new Web3('https://ropsten.infura.io/hqRzEqFKv6IsjRxfVUMH')
const account1 = '0x0539B9c9C886e93778542B553C64cb5EAfB902b1'
const account2 = '0xca7F442c44b079dd07324C9c7eDfe348b92f46c9'
const privatekey1 = Buffer.from('cxxxxxxxxxxxxx5xd0804xxxxxxx238ca1aed1xxx' , 'hex')
web3.eth.getTransactionCount(account1 , (err, transCount) => {
const txObject = {
nonce: web3.utils.toHex(transCount) ,
to: account2 ,
value: web3.utils.toHex(web3.utils.toWei('1' , 'ether')) ,
gasLimit: web3.utils.toHex(30000) ,
gasPrice: web3.utils.toHex(web3.utils.toWei('10' , 'gwei'))
}
var tx = new Tx(txObject)
tx.sign(privatekey1)
const serializedTransaction = tx.serialize()
const raw = '0x' + serializedTransaction.toString('hex')
web3.eth.sendSignedTransaction(raw , (err, txHash) => 
  {console.log('txHash: ' , txHash)})
})

Now I run this code in the command prompt:

C:\Users\HP\path>
node testEther

This outputs:

txHash: undefined

txHash: undefined

Why does this happen? I have tried to connect to different ropsten network, but still the problem persists. I have checked my test ether balance and it shows that I have enough ether.The transaction does not happen at all. Is the way I am connecting to ropsten wrong?

8
  • What version of web3.js are you using? Nov 22 '19 at 19:54
  • version: 12.13.0
    – Crypto
    Nov 22 '19 at 19:56
  • There's no such version of web3.js. The major version is either 0 or 1. There is an alpha for 2 as well, but I doubt you're using it. Please run npm ls web3 from a command line and come back with the result. Alternatively, you can console.log(web3.version) from your JS code. Nov 22 '19 at 20:10
  • In any case, since you print the second parameter and get undefined, try to print the first parameter (called err in your code). I have a guts feeling that you'll be surprised of what you find in there (which ain't gonna be an error object as you might expect). Nov 22 '19 at 20:16
  • Sorry, I entered node.js version. Web3 version is 1.2.4
    – Crypto
    Nov 22 '19 at 20:24
0

Where you have

var tx = new Tx(txObject)

you need

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

h/t

https://community.infura.io/t/sendsignedtransaction-invalid-sender-error/832

Full Code

This code is working locally. Replace your infura url and private key.

  • web3 1.2.4
  • ethereumjs-tx 2.1.1 (installed with separate npm install ethereumjs-tx)
var Tx = require('ethereumjs-tx').Transaction
var Web3 = require('web3');
var web3 = new Web3('https://ropsten.infura.io/v3/INFURA_PROJECT');
const account1 = '0x926f8E72CE94491Cafba874b4affD40C3b05a5e3'
const account2 = '0xca7F442c44b079dd07324C9c7eDfe348b92f46c9'
const privatekey1 = Buffer.from('PRIVATE_KEY' , 'hex')
web3.eth.getTransactionCount(account1 , (err, transCount) => {
  const txObject = {
    nonce: web3.utils.toHex(transCount) ,
    from: account1,
    to: account2 ,
    value: web3.utils.toHex(web3.utils.toWei('.01' , 'ether')) ,
    gasLimit: web3.utils.toHex(30000) ,
    gasPrice: web3.utils.toHex(web3.utils.toWei('10' , 'gwei'))
  }
  const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})
  tx.sign(privatekey1)
  const serializedTransaction = tx.serialize()
  const raw = '0x' + serializedTransaction.toString('hex')
  web3.eth.sendSignedTransaction(raw , (err, txHash) => {
    console.log('txHash: ' , txHash)
  });
});

A resulting transaction:

https://ropsten.etherscan.io/tx/0x2b2f2a09d7d9d7cf8ec7243c6975b14d1db04ebaa2cff530700cf686d88095e9

3
  • Tried it but still got the same result.
    – Crypto
    Nov 23 '19 at 5:49
  • Try also adding a from in the transaction (from: account 1) before the to
    – Steven V
    Nov 23 '19 at 12:36
  • Also maybe try a more simple action like getBalance instead of sending a transaction to confirm your web3/provider is connected w/o and error
    – Steven V
    Nov 23 '19 at 12:41

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.