Web3js

Web3js 錯誤:不支持事務類型 - 如何知道支持的事務類型?

  • July 4, 2021

我正在嘗試發送測試交易,但收到“不支持交易類型”錯誤,我嘗試了@ethereumjs/tx 文件中支持的每種交易類型。

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

此外,是否有線上資源來確定每條鏈支持哪些交易類型?

此錯誤來自您的交易格式錯誤。實際上,在serialize()發送到節點之前,它必須以 rlp 格式(函式)進行編碼。此外,您應該更改 gas並在對像中gasLimit添加一個gasPrice屬性rawTx

這是一個工作程式碼,考慮到這些變化:

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

引用自:https://ethereum.stackexchange.com/questions/102961