11

Here is my code. I am trying to send ether from one account to another.

var Tx     = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/1ac73a4112ab456cb5bdd3ac33e4fe82')

const account1 = '<Account 1>'
const account2 = '<Account 2>'

const k1 = Buffer.from('<Private key 1>','hex')
const k2 = Buffer.from('<Private key 2>','hex')

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

  // Sign the transaction
  const tx = new Tx(txObject)
  tx.sign(k1)

  const serializedTx = tx.serialize()
  const raw = '0x' + serializedTx.toString('hex')

  // Broadcast the transaction
  web3.eth.sendSignedTransaction(raw, (err, txHash) => {
    console.log('txHash:', txHash)
  })
})

The error thrown is as follows:

UnhandledPromiseRejectionWarning: TypeError: Tx is not a constructor at GetTransactionCountMethod.web3.eth.getTransactionCount (C:\Users\Atul\Web3_code\send_transaction.js:22:14) at GetTransactionCountMethod._callee$ (C:\Users\Atul\node_modules\web3-core-method\dist\web3-core-method.cjs.js:126:22) at tryCatch (C:\Users\Atul\node_modules\regenerator-runtime\runtime.js:45:40) at Generator.invoke [as _invoke] (C:\Users\Atul\node_modules\regenerator-runtime\runtime.js:271:22) at Generator.prototype.(anonymous function) [as next] (C:\Users\Atul\node_modules\regenerator-runtime\runtime.js:97:21) at asyncGeneratorStep (C:\Users\Atul\node_modules@babel\runtime\helpers\asyncToGenerator.js:3:24) at _next (C:\Users\Atul\node_modules@babel\runtime\helpers\asyncToGenerator.js:25:9) at process._tickCallback (internal/process/next_tick.js:68:7) (node:10632) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:10632) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm extremely new to this. Pls help.

1 Answer 1

21

In the new version you should

change

const Tx = require('ethereumjs-tx')

to

const Tx = require('ethereumjs-tx').Transaction
4
  • 1
    Thanks that worked
    – Atul Anand
    Jul 19, 2019 at 8:01
  • 1
    You're welcome! Our pleasure! Please accept the answer then other people can know when they face the same issue :)
    – Ha ĐANG
    Jul 19, 2019 at 8:12
  • 1
    You're a hero @HaĐANG, but do you have where this change is documented? This wasted a couple hours of my time before finding this question. Google is now the worst for searching because of their quest for propaganda, I had to go 15 pages deep on Bing. This answer should be #1. Jul 23, 2019 at 15:53
  • Thank you for nice words. I don’t have the document. Just look into the latest code to understand:)
    – Ha ĐANG
    Jul 24, 2019 at 14:01

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