Skip to main content

smplx_sdk/transaction/
tx_receipt.rs

1use std::fmt;
2use std::fmt::{Debug, Display, Formatter};
3
4use simplicityhl::elements::Txid;
5
6use crate::provider::{ProviderError, ProviderTrait};
7
8/// A receipt for a broadcast transaction, containing the provider context and the transaction ID.
9#[derive(Clone, Copy)]
10pub struct TxReceipt<'a> {
11    provider: &'a dyn ProviderTrait,
12    tx_id: Txid,
13}
14
15impl Display for TxReceipt<'_> {
16    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
17        Display::fmt(&self.tx_id, f)
18    }
19}
20
21impl Debug for TxReceipt<'_> {
22    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
23        Debug::fmt(&self.tx_id, f)
24    }
25}
26
27impl AsRef<Txid> for TxReceipt<'_> {
28    fn as_ref(&self) -> &Txid {
29        &self.tx_id
30    }
31}
32
33impl<'a> TxReceipt<'a> {
34    /// Creates a new `TxReceipt` associated with a specific provider and transaction ID.
35    pub fn new(provider: &'a dyn ProviderTrait, tx_id: Txid) -> Self {
36        Self { provider, tx_id }
37    }
38
39    /// Returns the ID of the broadcasted transaction.
40    #[must_use]
41    pub fn txid(self) -> Txid {
42        self.tx_id
43    }
44
45    /// Blocks and waits for the transaction to be confirmed by the provider.
46    ///
47    /// # Errors
48    /// Returns a `ProviderError` if the provider encounters an error while tracking the transaction state.
49    #[inline]
50    pub fn wait(&self) -> Result<(), ProviderError> {
51        self.provider.wait(&self.tx_id)
52    }
53}