Skip to content

Commit c01037e

Browse files
authored
chore: add helpers to unwrap a variant (#1792)
1 parent c14c5be commit c01037e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

crates/consensus/src/transaction/pooled.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,51 @@ impl PooledTransaction {
170170
_ => None,
171171
}
172172
}
173+
174+
/// Attempts to unwrap the transaction into a legacy transaction variant.
175+
/// If the transaction is not a legacy transaction, it will return `Err(self)`.
176+
pub fn try_into_legacy(self) -> Result<Signed<TxLegacy>, Self> {
177+
match self {
178+
Self::Legacy(tx) => Ok(tx),
179+
tx => Err(tx),
180+
}
181+
}
182+
183+
/// Attempts to unwrap the transaction into an EIP-2930 transaction variant.
184+
/// If the transaction is not an EIP-2930 transaction, it will return `Err(self)`.
185+
pub fn try_into_eip2930(self) -> Result<Signed<TxEip2930>, Self> {
186+
match self {
187+
Self::Eip2930(tx) => Ok(tx),
188+
tx => Err(tx),
189+
}
190+
}
191+
192+
/// Attempts to unwrap the transaction into an EIP-1559 transaction variant.
193+
/// If the transaction is not an EIP-1559 transaction, it will return `Err(self)`.
194+
pub fn try_into_eip1559(self) -> Result<Signed<TxEip1559>, Self> {
195+
match self {
196+
Self::Eip1559(tx) => Ok(tx),
197+
tx => Err(tx),
198+
}
199+
}
200+
201+
/// Attempts to unwrap the transaction into an EIP-4844 transaction variant.
202+
/// If the transaction is not an EIP-4844 transaction, it will return `Err(self)`.
203+
pub fn try_into_eip4844(self) -> Result<Signed<TxEip4844WithSidecar>, Self> {
204+
match self {
205+
Self::Eip4844(tx) => Ok(tx),
206+
tx => Err(tx),
207+
}
208+
}
209+
210+
/// Attempts to unwrap the transaction into an EIP-7702 transaction variant.
211+
/// If the transaction is not an EIP-7702 transaction, it will return `Err(self)`.
212+
pub fn try_into_eip7702(self) -> Result<Signed<TxEip7702>, Self> {
213+
match self {
214+
Self::Eip7702(tx) => Ok(tx),
215+
tx => Err(tx),
216+
}
217+
}
173218
}
174219

175220
impl From<Signed<TxLegacy>> for PooledTransaction {

0 commit comments

Comments
 (0)