@@ -1315,6 +1315,69 @@ impl<A: Anchor> TxGraph<A> {
13151315 ///
13161316 /// This is the infallible version of [`try_balance`].
13171317 ///
1318+ /// ### Minimum confirmations
1319+ ///
1320+ /// To filter for transactions with at least `N` confirmations, pass a `chain_tip` that is
1321+ /// `N - 1` blocks below the actual tip. This ensures that only transactions with at least `N`
1322+ /// confirmations are counted as confirmed in the returned [`Balance`].
1323+ ///
1324+ /// ```
1325+ /// # use bdk_chain::tx_graph::TxGraph;
1326+ /// # use bdk_chain::{local_chain::LocalChain, CanonicalizationParams, ConfirmationBlockTime};
1327+ /// # use bdk_testenv::{hash, utils::new_tx};
1328+ /// # use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
1329+ ///
1330+ /// # let spk = ScriptBuf::from_hex("0014c692ecf13534982a9a2834565cbd37add8027140").unwrap();
1331+ /// # let chain =
1332+ /// # LocalChain::from_blocks((0..=15).map(|i| (i as u32, hash!("h"))).collect()).unwrap();
1333+ /// # let mut graph: TxGraph = TxGraph::default();
1334+ /// # let coinbase_tx = Transaction {
1335+ /// # input: vec![TxIn {
1336+ /// # previous_output: OutPoint::null(),
1337+ /// # ..Default::default()
1338+ /// # }],
1339+ /// # output: vec![TxOut {
1340+ /// # value: Amount::from_sat(70000),
1341+ /// # script_pubkey: spk.clone(),
1342+ /// # }],
1343+ /// # ..new_tx(0)
1344+ /// # };
1345+ /// # let tx = Transaction {
1346+ /// # input: vec![TxIn {
1347+ /// # previous_output: OutPoint::new(coinbase_tx.compute_txid(), 0),
1348+ /// # ..Default::default()
1349+ /// # }],
1350+ /// # output: vec![TxOut {
1351+ /// # value: Amount::from_sat(42_000),
1352+ /// # script_pubkey: spk.clone(),
1353+ /// # }],
1354+ /// # ..new_tx(1)
1355+ /// # };
1356+ /// # let txid = tx.compute_txid();
1357+ /// # let _ = graph.insert_tx(tx.clone());
1358+ /// # let _ = graph.insert_anchor(
1359+ /// # txid,
1360+ /// # ConfirmationBlockTime {
1361+ /// # block_id: chain.get(10).unwrap().block_id(),
1362+ /// # confirmation_time: 123456,
1363+ /// # },
1364+ /// # );
1365+ ///
1366+ /// let minimum_confirmations = 6;
1367+ /// let target_tip = chain
1368+ /// .tip()
1369+ /// .floor_below(minimum_confirmations - 1)
1370+ /// .expect("checkpoint from local chain must have genesis");
1371+ /// let balance = graph.balance(
1372+ /// &chain,
1373+ /// target_tip.block_id(),
1374+ /// CanonicalizationParams::default(),
1375+ /// std::iter::once(((), OutPoint::new(txid, 0))),
1376+ /// |_: &(), _| true,
1377+ /// );
1378+ /// assert_eq!(balance.confirmed, Amount::from_sat(42_000));
1379+ /// ```
1380+ ///
13181381 /// [`try_balance`]: Self::try_balance
13191382 pub fn balance < C : ChainOracle < Error = Infallible > , OI : Clone > (
13201383 & self ,
0 commit comments