EatTheBlocks Forum

WEB3.JS problem

Hello,

I am trying to use the library “ethereumjs-tx” but it does not work.
I get this error message: “TypeError: Tx is not a constructor” on the code “const tx = new Tx(txObject)”.

Here is my code:

const Tx = require(‘ethereumjs-tx’)
const tx = new Tx(txObject)

Thank you for your help

Take care

Pierre

Don’t use this library, with web3 or etherjs you have everything you need.

Thank you for your answer.
I am now using web3 but now I have this message:
tx.sign is not a function

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

}



//sign transaction with private key of sender

const tx = new Web3(txObject)

tx.sign(privateKey1)

Thank you again for your help

I recommend to follow a tutorial for how to sign transactions with web3, like this one (I am the author).

Thank you for your help