docs(wallet): update docs for calculate_fee/fee_rate and add_foreign_utxo

This commit is contained in:
Steve Myers 2023-08-23 21:41:38 -05:00
parent 036299803f
commit 465d53cc88
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
2 changed files with 80 additions and 12 deletions

View File

@ -431,10 +431,17 @@ impl<D> Wallet<D> {
.next() .next()
} }
/// Inserts the given foreign `TxOut` at `OutPoint` into the wallet's transaction graph. Any /// Inserts a [`TxOut`] at [`OutPoint`] into the wallet's transaction graph.
/// inserted foreign TxOuts are not persisted until [`Self::commit`] is called. /// Any inserted TxOuts are not persisted until [`commit`] is called.
///
/// This can be used to add a `TxOut` that the wallet doesn't own but is used as an input to
/// a [`Transaction`] passed to the [`calculate_fee`] or [`calculate_fee_rate`] functions.
/// ///
/// Only insert TxOuts you trust the values for! /// Only insert TxOuts you trust the values for!
///
/// [`calculate_fee`]: Self::calculate_fee
/// [`calculate_fee_rate`]: Self::calculate_fee_rate
/// [`commit`]: Self::commit
pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut)
where where
D: PersistBackend<ChangeSet>, D: PersistBackend<ChangeSet>,
@ -445,12 +452,30 @@ impl<D> Wallet<D> {
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction. /// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
/// ///
/// To calculate the fee for a [`Transaction`] that depends on foreign [`TxOut`] values you must /// To calculate the fee for a [`Transaction`] with inputs not owned by this wallet you must
/// first manually insert the foreign TxOuts into the tx graph using the [`insert_txout`] function. /// manually insert the TxOut(s) into the tx graph using the [`insert_txout`] function.
/// Only insert TxOuts you trust the values for!
/// ///
/// Note `tx` does not have to be in the graph for this to work. /// Note `tx` does not have to be in the graph for this to work.
/// ///
/// # Examples
///
/// ```rust, no_run
/// # use bitcoin::Txid;
/// # use bdk::Wallet;
/// # let mut wallet: Wallet<()> = todo!();
/// # let txid:Txid = todo!();
/// let tx = wallet.get_tx(txid).expect("transaction").tx_node.tx;
/// let fee = wallet.calculate_fee(tx).expect("fee");
/// ```
///
/// ```rust, no_run
/// # use bitcoin::psbt::PartiallySignedTransaction;
/// # use bdk::Wallet;
/// # let mut wallet: Wallet<()> = todo!();
/// # let mut psbt: PartiallySignedTransaction = todo!();
/// let tx = &psbt.clone().extract_tx();
/// let fee = wallet.calculate_fee(tx).expect("fee");
/// ```
/// [`insert_txout`]: Self::insert_txout /// [`insert_txout`]: Self::insert_txout
pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> { pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> {
self.indexed_graph.graph().calculate_fee(tx) self.indexed_graph.graph().calculate_fee(tx)
@ -458,12 +483,30 @@ impl<D> Wallet<D> {
/// Calculate the [`FeeRate`] for a given transaction. /// Calculate the [`FeeRate`] for a given transaction.
/// ///
/// To calculate the fee rate for a [`Transaction`] that depends on foreign [`TxOut`] values you /// To calculate the fee rate for a [`Transaction`] with inputs not owned by this wallet you must
/// must first manually insert the foreign TxOuts into the tx graph using the [`insert_txout`] function. /// manually insert the TxOut(s) into the tx graph using the [`insert_txout`] function.
/// Only insert TxOuts you trust the values for!
/// ///
/// Note `tx` does not have to be in the graph for this to work. /// Note `tx` does not have to be in the graph for this to work.
/// ///
/// # Examples
///
/// ```rust, no_run
/// # use bitcoin::Txid;
/// # use bdk::Wallet;
/// # let mut wallet: Wallet<()> = todo!();
/// # let txid:Txid = todo!();
/// let tx = wallet.get_tx(txid).expect("transaction").tx_node.tx;
/// let fee_rate = wallet.calculate_fee_rate(tx).expect("fee rate");
/// ```
///
/// ```rust, no_run
/// # use bitcoin::psbt::PartiallySignedTransaction;
/// # use bdk::Wallet;
/// # let mut wallet: Wallet<()> = todo!();
/// # let mut psbt: PartiallySignedTransaction = todo!();
/// let tx = &psbt.clone().extract_tx();
/// let fee_rate = wallet.calculate_fee_rate(tx).expect("fee rate");
/// ```
/// [`insert_txout`]: Self::insert_txout /// [`insert_txout`]: Self::insert_txout
pub fn calculate_fee_rate(&self, tx: &Transaction) -> Result<FeeRate, CalculateFeeError> { pub fn calculate_fee_rate(&self, tx: &Transaction) -> Result<FeeRate, CalculateFeeError> {
self.calculate_fee(tx).map(|fee| { self.calculate_fee(tx).map(|fee| {
@ -473,10 +516,31 @@ impl<D> Wallet<D> {
} }
/// Computes total input value going from script pubkeys in the index (sent) and the total output /// Computes total input value going from script pubkeys in the index (sent) and the total output
/// value going to script pubkeys in the index (received) in `tx`. For the `sent` to be computed /// value going to script pubkeys in the index (received) in `tx`.
/// correctly, the output being spent must have already been scanned by the index. Calculating ///
/// received just uses the [`Transaction`] outputs directly, so it will be correct even if it has /// For the `sent` to be computed correctly, the outputs being spent must have already been
/// not been scanned. /// scanned by the index. Calculating received just uses the [`Transaction`] outputs directly,
/// so it will be correct even if it has not been scanned.
///
/// # Examples
///
/// ```rust, no_run
/// # use bitcoin::Txid;
/// # use bdk::Wallet;
/// # let mut wallet: Wallet<()> = todo!();
/// # let txid:Txid = todo!();
/// let tx = wallet.get_tx(txid).expect("transaction").tx_node.tx;
/// let (sent, received) = wallet.sent_and_received(tx);
/// ```
///
/// ```rust, no_run
/// # use bitcoin::psbt::PartiallySignedTransaction;
/// # use bdk::Wallet;
/// # let mut wallet: Wallet<()> = todo!();
/// # let mut psbt: PartiallySignedTransaction = todo!();
/// let tx = &psbt.clone().extract_tx();
/// let (sent, received) = wallet.sent_and_received(tx);
/// ```
pub fn sent_and_received(&self, tx: &Transaction) -> (u64, u64) { pub fn sent_and_received(&self, tx: &Transaction) -> (u64, u64) {
self.indexed_graph.index.sent_and_received(tx) self.indexed_graph.index.sent_and_received(tx)
} }

View File

@ -335,6 +335,10 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
/// ///
/// This is an **EXPERIMENTAL** feature, API and other major changes are expected. /// This is an **EXPERIMENTAL** feature, API and other major changes are expected.
/// ///
/// In order to use [`Wallet::calculate_fee`] or [`Wallet::calculate_fee_rate`] for a transaction
/// created with foreign UTXO(s) you must manually insert the corresponding TxOut(s) into the tx
/// graph using the [`Wallet::insert_txout`] function.
///
/// # Errors /// # Errors
/// ///
/// This method returns errors in the following circumstances: /// This method returns errors in the following circumstances: