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.