reth_codecs/alloy/transaction/
eip7702.rs

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