use alloy_network::Network;
#[doc(alias = "SendableTransaction")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SendableTx<N: Network> {
Builder(N::TransactionRequest),
Envelope(N::TxEnvelope),
}
impl<N: Network> SendableTx<N> {
pub const fn as_mut_builder(&mut self) -> Option<&mut N::TransactionRequest> {
match self {
Self::Builder(tx) => Some(tx),
_ => None,
}
}
pub const fn as_builder(&self) -> Option<&N::TransactionRequest> {
match self {
Self::Builder(tx) => Some(tx),
_ => None,
}
}
pub const fn is_builder(&self) -> bool {
matches!(self, Self::Builder(_))
}
pub const fn is_envelope(&self) -> bool {
matches!(self, Self::Envelope(_))
}
pub const fn as_envelope(&self) -> Option<&N::TxEnvelope> {
match self {
Self::Envelope(tx) => Some(tx),
_ => None,
}
}
pub fn try_into_envelope(self) -> Result<N::TxEnvelope, SendableTxErr<N::TransactionRequest>> {
match self {
Self::Builder(req) => Err(SendableTxErr::new(req)),
Self::Envelope(env) => Ok(env),
}
}
pub fn try_into_request(self) -> Result<N::TransactionRequest, SendableTxErr<N::TxEnvelope>> {
match self {
Self::Builder(req) => Ok(req),
Self::Envelope(env) => Err(SendableTxErr::new(env)),
}
}
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("Unexpected variant: {0:?}")]
pub struct SendableTxErr<T>(pub T);
impl<T> SendableTxErr<T> {
pub const fn new(inner: T) -> Self {
Self(inner)
}
pub fn into_inner(self) -> T {
self.0
}
}