Skip to content

Commit df9e210

Browse files
committed
docs(wallet): add example to appl_update_events
1 parent 6921b2b commit df9e210

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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 {

wallet/src/wallet/mod.rs

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

0 commit comments

Comments
 (0)