Skip to content

Commit

Permalink
add more helper functions to PooledTransactionElement (#7126)
Browse files Browse the repository at this point in the history
  • Loading branch information
int88 committed Mar 13, 2024
1 parent 610731c commit 8fbab73
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions crates/primitives/src/transaction/pooled.rs
Expand Up @@ -7,8 +7,8 @@
use super::error::TransactionConversionError;
use crate::{
Address, BlobTransaction, BlobTransactionSidecar, Bytes, Signature, Transaction,
TransactionSigned, TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxHash, TxLegacy, B256,
EIP4844_TX_TYPE_ID,
TransactionSigned, TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxHash,
TxLegacy, B256, EIP4844_TX_TYPE_ID,
};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header, EMPTY_LIST_CODE};
use bytes::Buf;
Expand Down Expand Up @@ -339,6 +339,89 @@ impl PooledTransactionsElement {
}
}
}

/// Retruns true if the transaction is an EIP-4844 transaction.
#[inline]
pub const fn is_eip4844(&self) -> bool {
matches!(self, Self::BlobTransaction(_))
}

/// Returns the [TxLegacy] variant if the transaction is a legacy transaction.
pub fn as_legacy(&self) -> Option<&TxLegacy> {
match self {
Self::Legacy { transaction, .. } => Some(transaction),
_ => None,
}
}

/// Returns the [TxEip2930] variant if the transaction is an EIP-2930 transaction.
pub fn as_eip2930(&self) -> Option<&TxEip2930> {
match self {
Self::Eip2930 { transaction, .. } => Some(transaction),
_ => None,
}
}

/// Returns the [TxEip1559] variant if the transaction is an EIP-1559 transaction.
pub fn as_eip1559(&self) -> Option<&TxEip1559> {
match self {
Self::Eip1559 { transaction, .. } => Some(transaction),
_ => None,
}
}

/// Returns the [TxEip4844] variant if the transaction is an EIP-4844 transaction.
pub fn as_eip4844(&self) -> Option<&TxEip4844> {
match self {
Self::BlobTransaction(tx) => Some(&tx.transaction),
_ => None,
}
}

/// Returns the blob gas used for all blobs of the EIP-4844 transaction if it is an EIP-4844
/// transaction.
///
/// This is the number of blobs times the
/// [DATA_GAS_PER_BLOB](crate::constants::eip4844::DATA_GAS_PER_BLOB) a single blob consumes.
pub fn blob_gas_used(&self) -> Option<u64> {
self.as_eip4844().map(TxEip4844::blob_gas)
}

/// Max fee per blob gas for eip4844 transaction [TxEip4844].
///
/// Returns `None` for non-eip4844 transactions.
///
/// This is also commonly referred to as the "Blob Gas Fee Cap" (`BlobGasFeeCap`).
pub fn max_fee_per_blob_gas(&self) -> Option<u128> {
match self {
Self::BlobTransaction(tx) => Some(tx.transaction.max_fee_per_blob_gas),
_ => None,
}
}

/// Max priority fee per gas for eip1559 transaction, for legacy and eip2930 transactions this
/// is `None`
///
/// This is also commonly referred to as the "Gas Tip Cap" (`GasTipCap`).
pub fn max_priority_fee_per_gas(&self) -> Option<u128> {
match self {
Self::Legacy { .. } | Self::Eip2930 { .. } => None,
Self::Eip1559 { transaction, .. } => Some(transaction.max_priority_fee_per_gas),
Self::BlobTransaction(tx) => Some(tx.transaction.max_priority_fee_per_gas),
}
}

/// Max fee per gas for eip1559 transaction, for legacy transactions this is gas_price.
///
/// This is also commonly referred to as the "Gas Fee Cap" (`GasFeeCap`).
pub fn max_fee_per_gas(&self) -> u128 {
match self {
Self::Legacy { transaction, .. } => transaction.gas_price,
Self::Eip2930 { transaction, .. } => transaction.gas_price,
Self::Eip1559 { transaction, .. } => transaction.max_fee_per_gas,
Self::BlobTransaction(tx) => tx.transaction.max_fee_per_gas,
}
}
}

impl Encodable for PooledTransactionsElement {
Expand Down

0 comments on commit 8fbab73

Please sign in to comment.