use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Bytes, ChainId, Parity, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[doc(alias = "Eip1559Transaction", alias = "TransactionEip1559", alias = "Eip1559Tx")]
pub struct TxEip1559 {
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub chain_id: ChainId,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub nonce: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
pub gas_limit: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub max_fee_per_gas: u128,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub max_priority_fee_per_gas: u128,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
pub to: TxKind,
pub value: U256,
pub access_list: AccessList,
pub input: Bytes,
}
impl TxEip1559 {
pub const fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
match base_fee {
None => self.max_fee_per_gas,
Some(base_fee) => {
let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128);
if tip > self.max_priority_fee_per_gas {
self.max_priority_fee_per_gas + base_fee as u128
} else {
self.max_fee_per_gas
}
}
}
}
pub fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
nonce: Decodable::decode(buf)?,
max_priority_fee_per_gas: Decodable::decode(buf)?,
max_fee_per_gas: Decodable::decode(buf)?,
gas_limit: Decodable::decode(buf)?,
to: Decodable::decode(buf)?,
value: Decodable::decode(buf)?,
input: Decodable::decode(buf)?,
access_list: Decodable::decode(buf)?,
})
}
#[doc(hidden)]
pub fn fields_len(&self) -> usize {
let mut len = 0;
len += self.chain_id.length();
len += self.nonce.length();
len += self.max_priority_fee_per_gas.length();
len += self.max_fee_per_gas.length();
len += self.gas_limit.length();
len += self.to.length();
len += self.value.length();
len += self.input.0.length();
len += self.access_list.length();
len
}
pub(crate) fn encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
self.chain_id.encode(out);
self.nonce.encode(out);
self.max_priority_fee_per_gas.encode(out);
self.max_fee_per_gas.encode(out);
self.gas_limit.encode(out);
self.to.encode(out);
self.value.encode(out);
self.input.0.encode(out);
self.access_list.encode(out);
}
pub fn encoded_len_with_signature<S>(&self, signature: &S, with_header: bool) -> usize
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
let inner_payload_length =
1 + Header { list: true, payload_length }.length() + payload_length;
if with_header {
Header { list: false, payload_length: inner_payload_length }.length()
+ inner_payload_length
} else {
inner_payload_length
}
}
#[doc(hidden)]
pub fn encode_with_signature<S>(&self, signature: &S, out: &mut dyn BufMut, with_header: bool)
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
if with_header {
Header {
list: false,
payload_length: 1 + Header { list: true, payload_length }.length() + payload_length,
}
.encode(out);
}
out.put_u8(self.tx_type() as u8);
self.encode_with_signature_fields(signature, out);
}
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
}
let original_len = buf.len();
let tx = Self::decode_fields(buf)?;
let signature = Signature::decode_rlp_vrs(buf)?;
if !matches!(signature.v(), Parity::Parity(_)) {
return Err(alloy_rlp::Error::Custom("invalid parity for typed transaction"));
}
let signed = tx.into_signed(signature);
if buf.len() + header.payload_length != original_len {
return Err(alloy_rlp::Error::ListLengthMismatch {
expected: header.payload_length,
got: original_len - buf.len(),
});
}
Ok(signed)
}
pub fn encode_with_signature_fields<S>(&self, signature: &S, out: &mut dyn BufMut)
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
let header = Header { list: true, payload_length };
header.encode(out);
self.encode_fields(out);
signature.write_rlp_vrs(out);
}
#[doc(alias = "transaction_type")]
pub(crate) const fn tx_type(&self) -> TxType {
TxType::Eip1559
}
#[inline]
pub fn size(&self) -> usize {
mem::size_of::<ChainId>() + mem::size_of::<u64>() + mem::size_of::<u64>() + mem::size_of::<u128>() + mem::size_of::<u128>() + self.to.size() + mem::size_of::<U256>() + self.access_list.size() + self.input.len() }
}
impl Transaction for TxEip1559 {
fn chain_id(&self) -> Option<ChainId> {
Some(self.chain_id)
}
fn nonce(&self) -> u64 {
self.nonce
}
fn gas_limit(&self) -> u64 {
self.gas_limit
}
fn gas_price(&self) -> Option<u128> {
None
}
fn max_fee_per_gas(&self) -> u128 {
self.max_fee_per_gas
}
fn max_priority_fee_per_gas(&self) -> Option<u128> {
Some(self.max_priority_fee_per_gas)
}
fn max_fee_per_blob_gas(&self) -> Option<u128> {
None
}
fn priority_fee_or_price(&self) -> u128 {
self.max_priority_fee_per_gas
}
fn kind(&self) -> TxKind {
self.to
}
fn value(&self) -> U256 {
self.value
}
fn input(&self) -> &Bytes {
&self.input
}
fn ty(&self) -> u8 {
TxType::Eip1559 as u8
}
fn access_list(&self) -> Option<&AccessList> {
Some(&self.access_list)
}
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}
fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}
impl SignableTransaction<Signature> for TxEip1559 {
fn set_chain_id(&mut self, chain_id: ChainId) {
self.chain_id = chain_id;
}
fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
out.put_u8(self.tx_type() as u8);
self.encode(out)
}
fn payload_len_for_signature(&self) -> usize {
self.length() + 1
}
fn into_signed(self, signature: Signature) -> Signed<Self> {
let signature = signature.with_parity_bool();
let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false));
self.encode_with_signature(&signature, &mut buf, false);
let hash = keccak256(&buf);
Signed::new_unchecked(self, signature, hash)
}
}
impl Encodable for TxEip1559 {
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.fields_len() }.encode(out);
self.encode_fields(out);
}
fn length(&self) -> usize {
let payload_length = self.fields_len();
Header { list: true, payload_length }.length() + payload_length
}
}
impl Decodable for TxEip1559 {
fn decode(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
let header = Header::decode(data)?;
let remaining_len = data.len();
if header.payload_length > remaining_len {
return Err(alloy_rlp::Error::InputTooShort);
}
Self::decode_fields(data)
}
}
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(super) mod serde_bincode_compat {
use alloc::borrow::Cow;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{Bytes, ChainId, TxKind, U256};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
#[derive(Debug, Serialize, Deserialize)]
pub struct TxEip1559<'a> {
chain_id: ChainId,
nonce: u64,
gas_limit: u64,
max_fee_per_gas: u128,
max_priority_fee_per_gas: u128,
#[serde(default)]
to: TxKind,
value: U256,
access_list: Cow<'a, AccessList>,
input: Cow<'a, Bytes>,
}
impl<'a> From<&'a super::TxEip1559> for TxEip1559<'a> {
fn from(value: &'a super::TxEip1559) -> Self {
Self {
chain_id: value.chain_id,
nonce: value.nonce,
gas_limit: value.gas_limit,
max_fee_per_gas: value.max_fee_per_gas,
max_priority_fee_per_gas: value.max_priority_fee_per_gas,
to: value.to,
value: value.value,
access_list: Cow::Borrowed(&value.access_list),
input: Cow::Borrowed(&value.input),
}
}
}
impl<'a> From<TxEip1559<'a>> for super::TxEip1559 {
fn from(value: TxEip1559<'a>) -> Self {
Self {
chain_id: value.chain_id,
nonce: value.nonce,
gas_limit: value.gas_limit,
max_fee_per_gas: value.max_fee_per_gas,
max_priority_fee_per_gas: value.max_priority_fee_per_gas,
to: value.to,
value: value.value,
access_list: value.access_list.into_owned(),
input: value.input.into_owned(),
}
}
}
impl SerializeAs<super::TxEip1559> for TxEip1559<'_> {
fn serialize_as<S>(source: &super::TxEip1559, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
TxEip1559::from(source).serialize(serializer)
}
}
impl<'de> DeserializeAs<'de, super::TxEip1559> for TxEip1559<'de> {
fn deserialize_as<D>(deserializer: D) -> Result<super::TxEip1559, D::Error>
where
D: Deserializer<'de>,
{
TxEip1559::deserialize(deserializer).map(Into::into)
}
}
#[cfg(test)]
mod tests {
use arbitrary::Arbitrary;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use super::super::{serde_bincode_compat, TxEip1559};
#[test]
fn test_tx_eip1559_bincode_roundtrip() {
#[serde_as]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Data {
#[serde_as(as = "serde_bincode_compat::TxEip1559")]
transaction: TxEip1559,
}
let mut bytes = [0u8; 1024];
rand::thread_rng().fill(bytes.as_mut_slice());
let data = Data {
transaction: TxEip1559::arbitrary(&mut arbitrary::Unstructured::new(&bytes))
.unwrap(),
};
let encoded = bincode::serialize(&data).unwrap();
let decoded: Data = bincode::deserialize(&encoded).unwrap();
assert_eq!(decoded, data);
}
}
}
#[cfg(all(test, feature = "k256"))]
mod tests {
use super::TxEip1559;
use crate::SignableTransaction;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{address, b256, hex, Address, Signature, B256, U256};
#[test]
fn recover_signer_eip1559() {
let signer: Address = address!("dd6b8b3dc6b7ad97db52f08a275ff4483e024cea");
let hash: B256 = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0");
let tx = TxEip1559 {
chain_id: 1,
nonce: 0x42,
gas_limit: 44386,
to: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
value: U256::from(0_u64),
input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(),
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
access_list: AccessList::default(),
};
let sig = Signature::from_scalars_and_parity(
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
assert_eq!(
tx.signature_hash(),
hex!("0d5688ac3897124635b6cf1bc0e29d6dfebceebdc10a54d74f2ef8b56535b682")
);
let signed_tx = tx.into_signed(sig);
assert_eq!(*signed_tx.hash(), hash, "Expected same hash");
assert_eq!(signed_tx.recover_signer().unwrap(), signer, "Recovering signer should pass.");
}
#[test]
fn encode_decode_eip1559() {
let hash: B256 = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0");
let tx = TxEip1559 {
chain_id: 1,
nonce: 0x42,
gas_limit: 44386,
to: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
value: U256::from(0_u64),
input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(),
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
access_list: AccessList::default(),
};
let sig = Signature::from_scalars_and_parity(
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap();
let mut buf = vec![];
tx.encode_with_signature_fields(&sig, &mut buf);
let decoded = TxEip1559::decode_signed_fields(&mut &buf[..]).unwrap();
assert_eq!(decoded, tx.into_signed(sig));
assert_eq!(*decoded.hash(), hash);
}
}