@@ -431,10 +431,17 @@ impl<D> Wallet<D> {
431
431
. next ( )
432
432
}
433
433
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.
436
439
///
437
440
/// 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
438
445
pub fn insert_txout ( & mut self , outpoint : OutPoint , txout : TxOut )
439
446
where
440
447
D : PersistBackend < ChangeSet > ,
@@ -445,25 +452,61 @@ impl<D> Wallet<D> {
445
452
446
453
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
447
454
///
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.
451
457
///
452
458
/// Note `tx` does not have to be in the graph for this to work.
453
459
///
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
+ /// ```
454
479
/// [`insert_txout`]: Self::insert_txout
455
480
pub fn calculate_fee ( & self , tx : & Transaction ) -> Result < u64 , CalculateFeeError > {
456
481
self . indexed_graph . graph ( ) . calculate_fee ( tx)
457
482
}
458
483
459
484
/// Calculate the [`FeeRate`] for a given transaction.
460
485
///
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.
464
488
///
465
489
/// Note `tx` does not have to be in the graph for this to work.
466
490
///
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
+ /// ```
467
510
/// [`insert_txout`]: Self::insert_txout
468
511
pub fn calculate_fee_rate ( & self , tx : & Transaction ) -> Result < FeeRate , CalculateFeeError > {
469
512
self . calculate_fee ( tx) . map ( |fee| {
@@ -473,10 +516,31 @@ impl<D> Wallet<D> {
473
516
}
474
517
475
518
/// 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
+ /// ```
480
544
pub fn sent_and_received ( & self , tx : & Transaction ) -> ( u64 , u64 ) {
481
545
self . indexed_graph . index . sent_and_received ( tx)
482
546
}
0 commit comments