]> Untitled Git - bdk/commitdiff
feat(chain): Add convenience conversions for `CanonicalTx`
author志宇 <hello@evanlinjin.me>
Thu, 1 May 2025 00:28:42 +0000 (10:28 +1000)
committerWei Chen <wzc110@gmail.com>
Thu, 1 May 2025 14:41:55 +0000 (14:41 +0000)
* `From<CanonicalTx> for Txid`
* `From<CanonicalTx> for Arc<Transaction>`

Also added a convenience method `ChainPosition::is_unconfirmed`.

These are intended to simplify the calls needed to populate the
`expected_mempool_txids` field of `Emitter::new`.

crates/chain/src/chain_data.rs
crates/chain/src/tx_graph.rs

index a4d764c026ed4551e9117c6b87c41a70de7b8585..4890b08a7961797802c5e2f58420edd6f8b4425a 100644 (file)
@@ -40,6 +40,11 @@ impl<A> ChainPosition<A> {
     pub fn is_confirmed(&self) -> bool {
         matches!(self, Self::Confirmed { .. })
     }
+
+    /// Returns whether [`ChainPosition`] is unconfirmed or not.
+    pub fn is_unconfirmed(&self) -> bool {
+        matches!(self, Self::Unconfirmed { .. })
+    }
 }
 
 impl<A: Clone> ChainPosition<&A> {
index a10f3e7578937319c3082bfda0abcf405706df67..7916bf954e1dbd22b6d923a7fa1074f3b8d5bb85 100644 (file)
@@ -251,6 +251,18 @@ pub struct CanonicalTx<'a, T, A> {
     pub tx_node: TxNode<'a, T, A>,
 }
 
+impl<'a, T, A> From<CanonicalTx<'a, T, A>> for Txid {
+    fn from(tx: CanonicalTx<'a, T, A>) -> Self {
+        tx.tx_node.txid
+    }
+}
+
+impl<'a, A> From<CanonicalTx<'a, Arc<Transaction>, A>> for Arc<Transaction> {
+    fn from(tx: CanonicalTx<'a, Arc<Transaction>, A>) -> Self {
+        tx.tx_node.tx
+    }
+}
+
 /// Errors returned by `TxGraph::calculate_fee`.
 #[derive(Debug, PartialEq, Eq)]
 pub enum CalculateFeeError {