1

I'm trying to send a test transaction but am getting a "transaction type not supported" error, and I tried every transaction type supported in the @ethereumjs/tx docs.

const Web3 = require('web3')
const Tx = require('@ethereumjs/tx');
const Common = require('@ethereumjs/common') 

const common = new Common.default({ chain: 'ropsten' })

const w3 = new Web3('https://ropsten.infura.io/v3/<apikey>')

 const key = '3c9...'
 const address = '0x3c6E2d83dFAd3858B55C978576b0Ba697C50a43c'

var privateKey = Buffer.from(key, 'hex')

const rawTx = {
    nonce: w3.utils.toHex(0),
    from: address,
    to: '0xe381c25de995d62b453af8b931aac84fccaa7a62',
    gas: w3.utils.toHex(21000),
    value: w3.utils.toHex(0),
    chainId: w3.utils.toHex(3),
};

let tx = Tx.Transaction.fromTxData(rawTx, {common})
tx.sign(privateKey);

let tx_hash = w3.eth.sendSignedTransaction(w3.utils.toHex(tx))
  .on('receipt', console.log)
     .catch(e => console.log('e: ', e));

Additionally, is there an online resource to determine what transaction types are supported for each chain?

1

This error comes from a bad formatting of your transaction. In fact, it must be encoded in rlp format (serialize() function) before being sent to the node. In addition, you should change gas to gasLimit and add a gasPrice property in the rawTx object.

Here is a working piece of code, taking these changes into account :

const Web3 = require('web3')
const Tx = require('@ethereumjs/tx');
const Common = require('@ethereumjs/common') 

const common = new Common.default({ chain: 'ropsten' })

const w3 = new Web3('https://ropsten.infura.io/v3/<apikey>')

 const key = '3c9...'
 const address = '0x3c6E2d83dFAd3858B55C978576b0Ba697C50a43c'

var privateKey = Buffer.from(key, 'hex')

const rawTx = {
    nonce: w3.utils.toHex(0), //to be incremented for each new transaction with this account
    from: address,
    to: '0xe381c25de995d62b453af8b931aac84fccaa7a62',
    gasLimit: w3.utils.toHex(21000),
    gasPrice: w3.utils.toHex(10e9), //10 Gwei
    value: w3.utils.toHex(0),
    //chainId: w3.utils.toHex(3), optional parameter
};

let tx = Tx.Transaction.fromTxData(rawTx, {common})
const signedTx = tx.sign(privateKey);
//encode the transaction in rlp format
const serializedTx = signedTx.serialize()

let tx_hash = w3.eth.sendSignedTransaction(w3.utils.toHex(serializedTx))
  .on('receipt', console.log)
     .catch(e => console.log('e: ', e));

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.