|
118 | 118 | //! assert!(changeset.is_empty());
|
119 | 119 | //! ```
|
120 | 120 | //! [`insert_txout`]: TxGraph::insert_txout
|
| 121 | +//! |
| 122 | +//! # Minimum confirmations |
| 123 | +//! |
| 124 | +//! You can simulate a “minimum confirmations” filter by adjusting the `chain_tip` height passed to |
| 125 | +//! [`TxGraph::balance`]. |
| 126 | +//! |
| 127 | +//! By using an earlier checkpoint as the tip, you ensure that only transactions with the required |
| 128 | +//! number of confirmations are counted as confirmed in the returned [`Balance`]. |
| 129 | +//! |
| 130 | +//! ``` |
| 131 | +//! # use bdk_chain::tx_graph::TxGraph; |
| 132 | +//! # use bdk_chain::{local_chain::LocalChain, CanonicalizationParams, ConfirmationBlockTime}; |
| 133 | +//! # use bdk_testenv::{hash, utils::new_tx}; |
| 134 | +//! # use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut}; |
| 135 | +//! |
| 136 | +//! # let spk = ScriptBuf::from_hex("0014c692ecf13534982a9a2834565cbd37add8027140").unwrap(); |
| 137 | +//! # let chain = |
| 138 | +//! # LocalChain::from_blocks((0..=15).map(|i| (i as u32, hash!("h"))).collect()).unwrap(); |
| 139 | +//! # let tip = chain.tip(); |
| 140 | +//! # let mut graph: TxGraph = TxGraph::default(); |
| 141 | +//! # let coinbase_tx = Transaction { |
| 142 | +//! # input: vec![TxIn { |
| 143 | +//! # previous_output: OutPoint::null(), |
| 144 | +//! # ..Default::default() |
| 145 | +//! # }], |
| 146 | +//! # output: vec![TxOut { |
| 147 | +//! # value: Amount::from_sat(70000), |
| 148 | +//! # script_pubkey: spk.clone(), |
| 149 | +//! # }], |
| 150 | +//! # ..new_tx(0) |
| 151 | +//! # }; |
| 152 | +//! |
| 153 | +//! // Create a confirmed transaction with 6 confirmations. |
| 154 | +//! let tx = Transaction { |
| 155 | +//! input: vec![TxIn { |
| 156 | +//! previous_output: OutPoint::new(coinbase_tx.compute_txid(), 0), |
| 157 | +//! ..Default::default() |
| 158 | +//! }], |
| 159 | +//! output: vec![TxOut { |
| 160 | +//! value: Amount::from_sat(42_000), |
| 161 | +//! script_pubkey: spk.clone(), |
| 162 | +//! }], |
| 163 | +//! ..new_tx(1) |
| 164 | +//! }; |
| 165 | +//! let txid = tx.compute_txid(); |
| 166 | +//! let _ = graph.insert_tx(tx.clone()); |
| 167 | +//! let _ = graph.insert_anchor( |
| 168 | +//! txid, |
| 169 | +//! ConfirmationBlockTime { |
| 170 | +//! block_id: chain.get(10).unwrap().block_id(), |
| 171 | +//! confirmation_time: 123456, |
| 172 | +//! }, |
| 173 | +//! ); |
| 174 | +//! |
| 175 | +//! // With a confirmation threshold of 6, our transaction should show in the balance. |
| 176 | +//! let confirmation_threshold = 6; |
| 177 | +//! let target_height = tip.height().saturating_sub(confirmation_threshold - 1); |
| 178 | +//! let target_tip = chain |
| 179 | +//! .range(..=target_height) |
| 180 | +//! .next() |
| 181 | +//! .expect("checkpoint from local chain must have genesis"); |
| 182 | +//! let balance = graph.balance( |
| 183 | +//! &chain, |
| 184 | +//! target_tip.block_id(), |
| 185 | +//! CanonicalizationParams::default(), |
| 186 | +//! std::iter::once(((), OutPoint::new(txid, 0))), |
| 187 | +//! |_: &(), _| true, |
| 188 | +//! ); |
| 189 | +//! assert_eq!(balance.confirmed, Amount::from_sat(42_000)); |
| 190 | +//! |
| 191 | +//! // With a confirmation threshold of 7, our transaction should not show in the balance. |
| 192 | +//! let confirmation_threshold = 7; |
| 193 | +//! let target_height = tip.height().saturating_sub(confirmation_threshold - 1); |
| 194 | +//! let target_tip = chain |
| 195 | +//! .range(..=target_height) |
| 196 | +//! .next() |
| 197 | +//! .expect("checkpoint from local chain must have genesis"); |
| 198 | +//! let balance = graph.balance( |
| 199 | +//! &chain, |
| 200 | +//! target_tip.block_id(), |
| 201 | +//! CanonicalizationParams::default(), |
| 202 | +//! std::iter::once(((), OutPoint::new(txid, 0))), |
| 203 | +//! |_: &(), _| true, |
| 204 | +//! ); |
| 205 | +//! assert_eq!(balance.confirmed, Amount::from_sat(0)); |
| 206 | +//! ``` |
121 | 207 |
|
122 | 208 | use crate::collections::*;
|
123 | 209 | use crate::spk_txout::SpkTxOutIndex;
|
|
0 commit comments