reth_codecs/alloy/transaction/
eip1559.rs

1//! Compact implementation for [`AlloyTxEip1559`]
2
3use crate::Compact;
4use alloy_consensus::TxEip1559 as AlloyTxEip1559;
5use alloy_eips::eip2930::AccessList;
6use alloy_primitives::{Bytes, ChainId, TxKind, U256};
7/// [EIP-1559 Transaction](https://eips.ethereum.org/EIPS/eip-1559)
8///
9/// This is a helper type to use derive on it instead of manually managing `bitfield`.
10///
11/// By deriving `Compact` here, any future changes or enhancements to the `Compact` derive
12/// will automatically apply to this type.
13///
14/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip1559`]
15#[derive(Debug, Clone, PartialEq, Eq, Hash, Compact, Default)]
16#[reth_codecs(crate = "crate")]
17#[cfg_attr(
18    any(test, feature = "test-utils"),
19    derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
20)]
21#[cfg_attr(any(test, feature = "test-utils"), crate::add_arbitrary_tests(crate, compact))]
22#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
23pub(crate) struct TxEip1559 {
24    chain_id: ChainId,
25    nonce: u64,
26    gas_limit: u64,
27    max_fee_per_gas: u128,
28    max_priority_fee_per_gas: u128,
29    to: TxKind,
30    value: U256,
31    access_list: AccessList,
32    input: Bytes,
33}
34
35impl Compact for AlloyTxEip1559 {
36    fn to_compact<B>(&self, buf: &mut B) -> usize
37    where
38        B: bytes::BufMut + AsMut<[u8]>,
39    {
40        let tx = TxEip1559 {
41            chain_id: self.chain_id,
42            nonce: self.nonce,
43            gas_limit: self.gas_limit,
44            max_fee_per_gas: self.max_fee_per_gas,
45            max_priority_fee_per_gas: self.max_priority_fee_per_gas,
46            to: self.to,
47            value: self.value,
48            access_list: self.access_list.clone(),
49            input: self.input.clone(),
50        };
51
52        tx.to_compact(buf)
53    }
54
55    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
56        let (tx, _) = TxEip1559::from_compact(buf, len);
57
58        let alloy_tx = Self {
59            chain_id: tx.chain_id,
60            nonce: tx.nonce,
61            gas_limit: tx.gas_limit,
62            max_fee_per_gas: tx.max_fee_per_gas,
63            max_priority_fee_per_gas: tx.max_priority_fee_per_gas,
64            to: tx.to,
65            value: tx.value,
66            access_list: tx.access_list,
67            input: tx.input,
68        };
69
70        (alloy_tx, buf)
71    }
72}