alloy_provider/provider/
sendable.rs

1use alloy_network::Network;
2
3/// A transaction that can be sent. This is either a builder or an envelope.
4///
5/// This type is used to allow for fillers to convert a builder into an envelope
6/// without changing the user-facing API.
7///
8/// Users should NOT use this type directly. It should only be used as an
9/// implementation detail of [`Provider::send_transaction_internal`].
10#[doc(hidden, alias = "SendableTransaction")]
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub enum SendableTx<N: Network> {
13    /// A transaction that is not yet signed.
14    Builder(N::TransactionRequest),
15    /// A transaction that is signed and fully constructed.
16    Envelope(N::TxEnvelope),
17}
18
19impl<N: Network> SendableTx<N> {
20    /// Fallible cast to an unbuilt transaction request.
21    pub fn as_mut_builder(&mut self) -> Option<&mut N::TransactionRequest> {
22        match self {
23            Self::Builder(tx) => Some(tx),
24            _ => None,
25        }
26    }
27
28    /// Fallible cast to an unbuilt transaction request.
29    pub const fn as_builder(&self) -> Option<&N::TransactionRequest> {
30        match self {
31            Self::Builder(tx) => Some(tx),
32            _ => None,
33        }
34    }
35
36    /// Checks if the transaction is a builder.
37    pub const fn is_builder(&self) -> bool {
38        matches!(self, Self::Builder(_))
39    }
40
41    /// Check if the transaction is an envelope.
42    pub const fn is_envelope(&self) -> bool {
43        matches!(self, Self::Envelope(_))
44    }
45
46    /// Fallible cast to a built transaction envelope.
47    pub const fn as_envelope(&self) -> Option<&N::TxEnvelope> {
48        match self {
49            Self::Envelope(tx) => Some(tx),
50            _ => None,
51        }
52    }
53
54    /// Returns the envelope if this variant is an [`SendableTx::Envelope`].
55    ///
56    /// Returns a [`SendableTxErr`] with the request object otherwise.
57    pub fn try_into_envelope(self) -> Result<N::TxEnvelope, SendableTxErr<N::TransactionRequest>> {
58        match self {
59            Self::Builder(req) => Err(SendableTxErr::new(req)),
60            Self::Envelope(env) => Ok(env),
61        }
62    }
63
64    /// Returns the envelope if this variant is an [`SendableTx::Builder`].
65    ///
66    /// Returns a [`SendableTxErr`] with the request object otherwise.
67    pub fn try_into_request(self) -> Result<N::TransactionRequest, SendableTxErr<N::TxEnvelope>> {
68        match self {
69            Self::Builder(req) => Ok(req),
70            Self::Envelope(env) => Err(SendableTxErr::new(env)),
71        }
72    }
73}
74
75/// Error when converting a [`SendableTx`].
76#[derive(Debug, Clone, thiserror::Error)]
77#[error("Unexpected variant: {0:?}")]
78pub struct SendableTxErr<T>(pub T);
79
80impl<T> SendableTxErr<T> {
81    /// Create a new error.
82    pub const fn new(inner: T) -> Self {
83        Self(inner)
84    }
85
86    /// Unwrap the error and return the original value.
87    pub fn into_inner(self) -> T {
88        self.0
89    }
90}