Skip to content

Commit a0e3ec4

Browse files
committed
docs(wallet): add example to appl_update_events
1 parent b401a2d commit a0e3ec4

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/wallet/mod.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,78 @@ impl Wallet {
23842384
/// After applying updates you should process the events in your app before persisting the
23852385
/// staged wallet changes. For an example of how to persist staged wallet changes see
23862386
/// [`Wallet::reveal_next_address`].
2387+
///
2388+
/// ```rust,no_run
2389+
/// # use bitcoin::*;
2390+
/// # use bdk_wallet::*;
2391+
/// use bdk_wallet::event::WalletEvent;
2392+
/// # let wallet_update = Update::default();
2393+
/// # let mut wallet = doctest_wallet!();
2394+
/// let events = wallet.apply_update_events(wallet_update)?;
2395+
/// // Handle wallet relevant events from this update.
2396+
/// events.iter().for_each(|event| {
2397+
/// match event {
2398+
/// // The chain tip changed.
2399+
/// WalletEvent::ChainTipChanged { old_tip, new_tip } => {
2400+
/// todo!() // handle event
2401+
/// }
2402+
/// // An unconfirmed tx is now confirmed in a block.
2403+
/// WalletEvent::TxConfirmed {
2404+
/// txid,
2405+
/// tx,
2406+
/// block_time,
2407+
/// old_block_time: None,
2408+
/// } => {
2409+
/// todo!() // handle event
2410+
/// }
2411+
/// // A confirmed tx is now confirmed in a new block (reorg).
2412+
/// WalletEvent::TxConfirmed {
2413+
/// txid,
2414+
/// tx,
2415+
/// block_time,
2416+
/// old_block_time: Some(old_block_time),
2417+
/// } => {
2418+
/// todo!() // handle event
2419+
/// }
2420+
/// // A new unconfirmed tx was seen in the mempool.
2421+
/// WalletEvent::TxUnconfirmed {
2422+
/// txid,
2423+
/// tx,
2424+
/// old_block_time: None,
2425+
/// } => {
2426+
/// todo!() // handle event
2427+
/// }
2428+
/// // A previously confirmed tx in now unconfirmed in the mempool (reorg).
2429+
/// WalletEvent::TxUnconfirmed {
2430+
/// txid,
2431+
/// tx,
2432+
/// old_block_time: Some(old_block_time),
2433+
/// } => {
2434+
/// todo!() // handle event
2435+
/// }
2436+
/// // An unconfirmed tx was replaced in the mempool (RBF or double spent input).
2437+
/// WalletEvent::TxReplaced {
2438+
/// txid,
2439+
/// tx,
2440+
/// conflicts,
2441+
/// } => {
2442+
/// todo!() // handle event
2443+
/// }
2444+
/// // An unconfirmed tx was dropped from the mempool (fee too low).
2445+
/// WalletEvent::TxDropped { txid, tx } => {
2446+
/// todo!() // handle event
2447+
/// }
2448+
/// _ => {
2449+
/// // unexpected event, do nothing
2450+
/// }
2451+
/// }
2452+
/// // take staged wallet changes
2453+
/// let staged = wallet.take_staged();
2454+
/// // persist staged changes
2455+
/// });
2456+
/// # Ok::<(), anyhow::Error>(())
2457+
/// ```
2458+
/// [`TxBuilder`]: crate::TxBuilder
23872459
pub fn apply_update_events(
23882460
&mut self,
23892461
update: impl Into<Update>,

wallet/src/wallet/event.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use chain::{BlockId, ChainPosition, ConfirmationBlockTime};
1010

1111
/// Events representing changes to wallet transactions.
1212
///
13-
/// Returned after calling [`Wallet::apply_update`](crate::wallet::Wallet::apply_update).
13+
/// Returned after calling
14+
/// [`Wallet::apply_update_events`](crate::wallet::Wallet::apply_update_events).
1415
#[derive(Debug, Clone, PartialEq, Eq)]
1516
#[non_exhaustive]
1617
pub enum WalletEvent {

0 commit comments

Comments
 (0)