From 8ff752e018a553d8496cc9e94c149064f23ab9a4 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Wed, 3 Jul 2024 20:32:27 +0300 Subject: [PATCH] feat: impl Transaction for TxEnvelope --- crates/consensus/src/transaction/envelope.rs | 69 +++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index b586efffcb3..e8fdd71c79c 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -1,8 +1,8 @@ use core::fmt; -use crate::{Signed, TxEip1559, TxEip2930, TxLegacy}; +use crate::{Signed, Transaction, TxEip1559, TxEip2930, TxLegacy}; use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718}; -use alloy_primitives::B256; +use alloy_primitives::{TxKind, B256}; use alloy_rlp::{Decodable, Encodable, Header}; use crate::transaction::eip4844::{TxEip4844, TxEip4844Variant, TxEip4844WithSidecar}; @@ -361,6 +361,71 @@ impl Encodable2718 for TxEnvelope { } } +impl Transaction for TxEnvelope { + fn chain_id(&self) -> Option { + match self { + Self::Legacy(tx) => tx.tx().chain_id(), + Self::Eip2930(tx) => tx.tx().chain_id(), + Self::Eip1559(tx) => tx.tx().chain_id(), + Self::Eip4844(tx) => tx.tx().chain_id(), + } + } + + fn gas_limit(&self) -> u128 { + match self { + Self::Legacy(tx) => tx.tx().gas_limit(), + Self::Eip2930(tx) => tx.tx().gas_limit(), + Self::Eip1559(tx) => tx.tx().gas_limit(), + Self::Eip4844(tx) => tx.tx().gas_limit(), + } + } + + fn gas_price(&self) -> Option { + match self { + Self::Legacy(tx) => tx.tx().gas_price(), + Self::Eip2930(tx) => tx.tx().gas_price(), + Self::Eip1559(tx) => tx.tx().gas_price(), + Self::Eip4844(tx) => tx.tx().gas_price(), + } + } + + fn input(&self) -> &[u8] { + match self { + Self::Legacy(tx) => tx.tx().input(), + Self::Eip2930(tx) => tx.tx().input(), + Self::Eip1559(tx) => tx.tx().input(), + Self::Eip4844(tx) => tx.tx().input(), + } + } + + fn nonce(&self) -> u64 { + match self { + Self::Legacy(tx) => tx.tx().nonce(), + Self::Eip2930(tx) => tx.tx().nonce(), + Self::Eip1559(tx) => tx.tx().nonce(), + Self::Eip4844(tx) => tx.tx().nonce(), + } + } + + fn to(&self) -> TxKind { + match self { + Self::Legacy(tx) => tx.tx().to(), + Self::Eip2930(tx) => tx.tx().to(), + Self::Eip1559(tx) => tx.tx().to(), + Self::Eip4844(tx) => tx.tx().to(), + } + } + + fn value(&self) -> alloy_primitives::U256 { + match self { + Self::Legacy(tx) => tx.tx().value(), + Self::Eip2930(tx) => tx.tx().value(), + Self::Eip1559(tx) => tx.tx().value(), + Self::Eip4844(tx) => tx.tx().value(), + } + } +} + #[cfg(test)] mod tests { use super::*;