Links

Raw TX Reconstruction

The newTxs and pendingTxs streams provide transaction events. Traders who would like to reconstruct raw transactions bytes may use the following code as a reference.
Python
Node.js
# Type 0 legacy transaction:
import blxr_rlp
from bxcommon.messages.eth.serializers.transaction import Transaction
from bxcommon.utils import convert
tx_contents = {
'nonce': '0x21',
'gasPrice': '0x46c7cfe5b3',
'gas': '0xc62d',
'to': '0xdac17f958d2ee523a2206206994597c13d831ec7',
'value': '0x0',
'input': '0xa9059cbb000000000000000000000000a1ff1a11cac7c4f183c6fcb9554a7249fca271d5000000000000000000000000000000000000000000000000000000033e87f780',
'v': '0x25',
'r': '0xc79b76b082ab0a24c98119097e87a4dad6e1dceb6b0c2c65d9267beb6da1cbeb',
's': '0x433e77847d7e4f53805dedbc7f9954464f7b44a09fca9cbf75209b1bc14192bf',
}
transaction = Transaction.from_json_with_validation(tx_contents)
raw_transaction = convert.bytes_to_hex(blxr_rlp.encode(transaction))
print(raw_transaction)
# f8a921...4192bf
"""
# Type 2 dynamic fee transaction:
import blxr_rlp
from bxcommon.messages.eth.serializers.transaction import DynamicFeeTransaction
from bxcommon.utils import convert
tx_contents = {
'nonce': '0x1adcf8',
'gasPrice': None,
'maxFeePerGas': '0x746a528800',
'maxPriorityFeePerGas': '0x77359400',
'gas': '0x5208',
'to': '0x4c20d6c790dcd8474df3ec22dd199ff03e0fcd7d',
'value': '0x9928e2912b8a000',
'input': '0x',
'v': '0x0',
'r': '0x5683c39ded7133bfe5caf572644e3822c761c894347cd5323cf605fa28f3c752',
's': '0x4c8a80b282b5872e233f582ece775830a10556b96c18513f0d5b37d1171d60a3',
'chainId': '0x1',
'accessList': [],
'type': '0x2'
}
transaction = DynamicFeeTransaction.from_json_with_validation(tx_contents)
raw_transaction = blxr_rlp.decode_lazy(blxr_rlp.encode(transaction)).hex()
print(raw_transaction)
# 02f876...1d60a3
"""
// Type 0 legacy transaction:
const Transaction = require('@ethereumjs/tx').Transaction
const Common = require('@ethereumjs/common').Common
const txContents = {
nonce: '0x21',
gasPrice: '0x46c7cfe5b3',
gasLimit: '0xc62d',
to: '0xdac17f958d2ee523a2206206994597c13d831ec7',
value: '0x0',
data: '0xa9059cbb000000000000000000000000a1ff1a11cac7c4f183c6fcb9554a7249fca271d5000000000000000000000000000000000000000000000000000000033e87f780',
v: '0x25',
r: '0xc79b76b082ab0a24c98119097e87a4dad6e1dceb6b0c2c65d9267beb6da1cbeb',
s: '0x433e77847d7e4f53805dedbc7f9954464f7b44a09fca9cbf75209b1bc14192bf',
}
const tx = Transaction.fromTxData(txContents, { Common })
console.log(tx.serialize().toString('hex'))
// f8a921...4192bf
/*
//Type 2 dynamic fee transaction:
const Transaction = require('@ethereumjs/tx').Transaction
const FeeMarketEIP1559Transaction = require('@ethereumjs/tx').FeeMarketEIP1559Transaction
const Common = require('@ethereumjs/common').Common
const txContents = {
nonce: '0x1adcf8',
maxFeePerGas: '0x746a528800',
maxPriorityFeePerGas: '0x77359400',
gasLimit: '0x5208',
to: '0x4c20d6c790dcd8474df3ec22dd199ff03e0fcd7d',
value: '0x9928e2912b8a000',
data: '0x',
v: '0x0',
r: '0x5683c39ded7133bfe5caf572644e3822c761c894347cd5323cf605fa28f3c752',
s: '0x4c8a80b282b5872e233f582ece775830a10556b96c18513f0d5b37d1171d60a3',
}
const tx = FeeMarketEIP1559Transaction.fromTxData(txContents, { Common })
console.log(tx.serialize().toString('hex'))
// 02f876...1d60a3
*/