4
var EthereumTransaction = require("ethereumjs-tx")

var Web3 = require('web3')

var web3 = new Web3('http://127.0.0.1:7545')

//Setting Receiving and Sending Address

var sendingAddress = acc1

var receivingAddress = acc2

//Checking the balance of each account in ether

web3.eth.getBalance(sendingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

web3.eth.getBalance(receivingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

//Creating a transaction

var rawTransaction ={
    nounce:0,
    to:receivingAddress,
    gasPrice:20000000,
    gasLimit:30000,
    value:100,
    data:""
}

//Sign the Transaction

var privateKey = 'private key goes here'

var senderPrivateKeyHex = new Buffer(privateKey,'hex')

var transaction = new EthereumTransaction(rawTransaction)

transaction.sign(senderPrivateKeyHex)

//Send transaction to the network

var serializedTransaction = transaction.serialize()

web3.eth.sendSignedTransaction(serializedTransaction)

Error Occurred is.

TypeError: EthereumTransaction is not a constructor
2
  • Type npm ls ethereumjs-tx in a console and tell us what you're getting. Jun 18, 2020 at 11:02
  • npm WARN npm npm does not support Node.js v10.21.0 npm WARN npm You should probably upgrade to a newer version of node as we npm WARN npm can't make any promises that npm will work with this version. npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9. npm WARN npm You can find the latest version at nodejs.org simpleTransaction@1.0.0 /mnt/d/Blockchain/BlockchainTest/SimpleTransaction ├── ethereumjs-tx@2.1.2 └─┬ web3@1.2.9 └─┬ web3-eth@1.2.9 └─┬ web3-eth-accounts@1.2.9 └── ethereumjs-tx@2.1.2 deduped Jun 18, 2020 at 11:04

4 Answers 4

6

This is happening because ethereumjs-tx library has changed its syntax.

This is the working version of your code:

const EthereumTx = require('ethereumjs-tx').Transaction

var Web3 = require('web3')

var web3 = new Web3('http://127.0.0.1:7545')

//Setting Receiving and Sending Address

var sendingAddress = 'ADD SENDING ADDRESS HERE'

var receivingAddress = 'ADD RECEIVING ADDRESS HERE'

//Checking the balance of each account in ether

web3.eth.getBalance(sendingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

web3.eth.getBalance(receivingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

//Creating a transaction

var rawTransaction ={
    nonce: web3.utils.toHex(0),
    to: receivingAddress,
    gasPrice: web3.utils.toHex(20000000),
    gasLimit: web3.utils.toHex(30000),
    value: web3.utils.toHex(100),
    data: web3.utils.toHex("")
}

//Sign the Transaction

var privateKey = 'private key goes here'

var senderPrivateKeyHex = new Buffer.from(privateKey,'hex')

var transaction = new EthereumTx(rawTransaction)

transaction.sign(senderPrivateKeyHex)

//Send transaction to the network

var serializedTransaction = transaction.serialize()

web3.eth.sendSignedTransaction(serializedTransaction)
1
  • This works, perfect! :)
    – Pranay
    Jan 22 at 23:14
0

Try this

   var transaction = new TX(rawTransaction); 

instead of this

  var transaction = new EthereumTransaction(rawTransaction)
1
0

var TX = require('ethereumjs-tx').Transaction will remove this error.

0

You are using version 2.1.2 of the ethereumjs-tx library, but you want to use an earlier version.

If you install version 1.3.7 you should be alright. Try running this from the command line and see if your error goes away.

npm install ethereumjs-tx@1.3.7 --save

For version 2 and onwards the syntax to initialize the library has changed to require('ethereumjs-tx').Transaction. You can see an example here

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.