Skip to content

Commit 465d53c

Browse files
committed
docs(wallet): update docs for calculate_fee/fee_rate and add_foreign_utxo
1 parent 0362998 commit 465d53c

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,17 @@ impl<D> Wallet<D> {
431431
.next()
432432
}
433433

434-
/// Inserts the given foreign `TxOut` at `OutPoint` into the wallet's transaction graph. Any
435-
/// inserted foreign TxOuts are not persisted until [`Self::commit`] is called.
434+
/// Inserts a [`TxOut`] at [`OutPoint`] into the wallet's transaction graph.
435+
/// Any inserted TxOuts are not persisted until [`commit`] is called.
436+
///
437+
/// This can be used to add a `TxOut` that the wallet doesn't own but is used as an input to
438+
/// a [`Transaction`] passed to the [`calculate_fee`] or [`calculate_fee_rate`] functions.
436439
///
437440
/// Only insert TxOuts you trust the values for!
441+
///
442+
/// [`calculate_fee`]: Self::calculate_fee
443+
/// [`calculate_fee_rate`]: Self::calculate_fee_rate
444+
/// [`commit`]: Self::commit
438445
pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut)
439446
where
440447
D: PersistBackend<ChangeSet>,
@@ -445,25 +452,61 @@ impl<D> Wallet<D> {
445452

446453
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
447454
///
448-
/// To calculate the fee for a [`Transaction`] that depends on foreign [`TxOut`] values you must
449-
/// first manually insert the foreign TxOuts into the tx graph using the [`insert_txout`] function.
450-
/// Only insert TxOuts you trust the values for!
455+
/// To calculate the fee for a [`Transaction`] with inputs not owned by this wallet you must
456+
/// manually insert the TxOut(s) into the tx graph using the [`insert_txout`] function.
451457
///
452458
/// Note `tx` does not have to be in the graph for this to work.
453459
///
460+
/// # Examples
461+
///
462+
/// ```rust, no_run
463+
/// # use bitcoin::Txid;
464+
/// # use bdk::Wallet;
465+
/// # let mut wallet: Wallet<()> = todo!();
466+
/// # let txid:Txid = todo!();
467+
/// let tx = wallet.get_tx(txid).expect("transaction").tx_node.tx;
468+
/// let fee = wallet.calculate_fee(tx).expect("fee");
469+
/// ```
470+
///
471+
/// ```rust, no_run
472+
/// # use bitcoin::psbt::PartiallySignedTransaction;
473+
/// # use bdk::Wallet;
474+
/// # let mut wallet: Wallet<()> = todo!();
475+
/// # let mut psbt: PartiallySignedTransaction = todo!();
476+
/// let tx = &psbt.clone().extract_tx();
477+
/// let fee = wallet.calculate_fee(tx).expect("fee");
478+
/// ```
454479
/// [`insert_txout`]: Self::insert_txout
455480
pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> {
456481
self.indexed_graph.graph().calculate_fee(tx)
457482
}
458483

459484
/// Calculate the [`FeeRate`] for a given transaction.
460485
///
461-
/// To calculate the fee rate for a [`Transaction`] that depends on foreign [`TxOut`] values you
462-
/// must first manually insert the foreign TxOuts into the tx graph using the [`insert_txout`] function.
463-
/// Only insert TxOuts you trust the values for!
486+
/// To calculate the fee rate for a [`Transaction`] with inputs not owned by this wallet you must
487+
/// manually insert the TxOut(s) into the tx graph using the [`insert_txout`] function.
464488
///
465489
/// Note `tx` does not have to be in the graph for this to work.
466490
///
491+
/// # Examples
492+
///
493+
/// ```rust, no_run
494+
/// # use bitcoin::Txid;
495+
/// # use bdk::Wallet;
496+
/// # let mut wallet: Wallet<()> = todo!();
497+
/// # let txid:Txid = todo!();
498+
/// let tx = wallet.get_tx(txid).expect("transaction").tx_node.tx;
499+
/// let fee_rate = wallet.calculate_fee_rate(tx).expect("fee rate");
500+
/// ```
501+
///
502+
/// ```rust, no_run
503+
/// # use bitcoin::psbt::PartiallySignedTransaction;
504+
/// # use bdk::Wallet;
505+
/// # let mut wallet: Wallet<()> = todo!();
506+
/// # let mut psbt: PartiallySignedTransaction = todo!();
507+
/// let tx = &psbt.clone().extract_tx();
508+
/// let fee_rate = wallet.calculate_fee_rate(tx).expect("fee rate");
509+
/// ```
467510
/// [`insert_txout`]: Self::insert_txout
468511
pub fn calculate_fee_rate(&self, tx: &Transaction) -> Result<FeeRate, CalculateFeeError> {
469512
self.calculate_fee(tx).map(|fee| {
@@ -473,10 +516,31 @@ impl<D> Wallet<D> {
473516
}
474517

475518
/// Computes total input value going from script pubkeys in the index (sent) and the total output
476-
/// value going to script pubkeys in the index (received) in `tx`. For the `sent` to be computed
477-
/// correctly, the output being spent must have already been scanned by the index. Calculating
478-
/// received just uses the [`Transaction`] outputs directly, so it will be correct even if it has
479-
/// not been scanned.
519+
/// value going to script pubkeys in the index (received) in `tx`.
520+
///
521+
/// For the `sent` to be computed correctly, the outputs being spent must have already been
522+
/// scanned by the index. Calculating received just uses the [`Transaction`] outputs directly,
523+
/// so it will be correct even if it has not been scanned.
524+
///
525+
/// # Examples
526+
///
527+
/// ```rust, no_run
528+
/// # use bitcoin::Txid;
529+
/// # use bdk::Wallet;
530+
/// # let mut wallet: Wallet<()> = todo!();
531+
/// # let txid:Txid = todo!();
532+
/// let tx = wallet.get_tx(txid).expect("transaction").tx_node.tx;
533+
/// let (sent, received) = wallet.sent_and_received(tx);
534+
/// ```
535+
///
536+
/// ```rust, no_run
537+
/// # use bitcoin::psbt::PartiallySignedTransaction;
538+
/// # use bdk::Wallet;
539+
/// # let mut wallet: Wallet<()> = todo!();
540+
/// # let mut psbt: PartiallySignedTransaction = todo!();
541+
/// let tx = &psbt.clone().extract_tx();
542+
/// let (sent, received) = wallet.sent_and_received(tx);
543+
/// ```
480544
pub fn sent_and_received(&self, tx: &Transaction) -> (u64, u64) {
481545
self.indexed_graph.index.sent_and_received(tx)
482546
}

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
335335
///
336336
/// This is an **EXPERIMENTAL** feature, API and other major changes are expected.
337337
///
338+
/// In order to use [`Wallet::calculate_fee`] or [`Wallet::calculate_fee_rate`] for a transaction
339+
/// created with foreign UTXO(s) you must manually insert the corresponding TxOut(s) into the tx
340+
/// graph using the [`Wallet::insert_txout`] function.
341+
///
338342
/// # Errors
339343
///
340344
/// This method returns errors in the following circumstances:

0 commit comments

Comments
 (0)