New Wallet Event Methods¶
Overview
- Lead Developer: @notmandatory
- Pull Request: #399
- Feature Type: Non-Breaking
Overview¶
Three new methods have been backported from the 3.0 release, completing the event system that was introduced in 2.3 with apply_block_events and apply_block_connected_to_events. These additions cover the remaining wallet operations that mutate state: applying unconfirmed transactions, applying mempool evictions, and any custom wallet-mutating function.
Why Do This?¶
The 2.3 release added event-returning variants for block-based updates. However, wallets also receive state changes through unconfirmed transaction feeds and mempool eviction notifications — and those paths had no equivalent event-returning API. The new methods fill that gap and expose a general-purpose helper (events_helper) for cases where neither of those fits.
New Methods¶
Wallet::apply_unconfirmed_txs_events¶
pub fn apply_unconfirmed_txs_events<T: Into<Arc<Transaction>>>(
&mut self,
unconfirmed_txs: impl IntoIterator<Item = (T, u64)>,
) -> Vec<WalletEvent>
The event-returning counterpart to apply_unconfirmed_txs. Applies a set of unconfirmed transactions to the wallet and returns the list of [WalletEvent]s that resulted from the operation.
let unconfirmed = vec![(tx, last_seen_timestamp)];
let events: Vec<WalletEvent> = wallet.apply_unconfirmed_txs_events(unconfirmed);
Wallet::apply_evicted_txs_events¶
pub fn apply_evicted_txs_events(
&mut self,
evicted_txs: impl IntoIterator<Item = (Txid, u64)>,
) -> Vec<WalletEvent>
The event-returning counterpart to apply_evicted_txs. Marks unconfirmed transactions as evicted from the mempool and returns the resulting events. An evicted transaction is no longer considered canonical by the wallet, though its data is retained in the transaction graph.
let evictions = vec![(txid, eviction_timestamp)];
let events: Vec<WalletEvent> = wallet.apply_evicted_txs_events(evictions);
Wallet::events_helper¶
pub fn events_helper<F, T, E>(&mut self, f: F) -> Result<Vec<WalletEvent>, E>
where
F: FnOnce(&mut Self) -> Result<T, E>,
E: fmt::Debug + fmt::Display,
A general-purpose helper for advanced users. It accepts any wallet-mutating closure, captures the wallet state before and after, and returns the [WalletEvent]s that describe what changed. All the event-returning methods (apply_update_events, apply_block_events, apply_unconfirmed_txs_events, apply_evicted_txs_events) are implemented on top of this.
use bdk_wallet::{Wallet, Update, event::WalletEvent};
let update = Update::default();
let events: Vec<WalletEvent> = wallet.events_helper(|w| w.apply_update(update))?;
WalletEvent Types¶
Events generated by all of these methods are the same [WalletEvent] variants used throughout the 2.x event system:
| Variant | Meaning |
|---|---|
WalletEvent::ChainTipChanged |
The blockchain tip advanced or reorganized |
WalletEvent::TxConfirmed |
A wallet transaction was confirmed in a block |
WalletEvent::TxUnconfirmed |
A previously confirmed transaction became unconfirmed |
WalletEvent::TxReplaced |
An unconfirmed transaction was replaced (e.g., via RBF) |
WalletEvent::TxDropped |
An unconfirmed transaction was dropped from the mempool |