add more helper functions to PooledTransactionElement (#7126)

This commit is contained in:
int88
2024-03-13 20:31:18 +08:00
committed by GitHub
parent 610731ced8
commit 8fbab739c8

View File

@@ -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;
@@ -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 {