]> Untitled Git - bdk-cli/commitdiff
feat(WalletEvent): Add WalletEvent to rpc and cbf
authorVihiga Tyonum <withtvpeter@gmail.com>
Sun, 28 Jun 2026 18:31:15 +0000 (19:31 +0100)
committerVihiga Tyonum <withtvpeter@gmail.com>
Tue, 30 Jun 2026 19:12:25 +0000 (20:12 +0100)
- add WalletEvent to rpc and cbf clients
- Update Wallet to v2.4.0

Cargo.lock
Cargo.toml
src/handlers.rs
src/utils.rs

index 077ed59370a8742bfb7de65bee87fd6510626e1a..7bf049afe1920bad651e4ba49f8a1b88705ff1d8 100644 (file)
@@ -331,9 +331,9 @@ dependencies = [
 
 [[package]]
 name = "bdk_wallet"
-version = "2.3.0"
+version = "2.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b03f1e31ccc562f600981f747d2262b84428cbff52c9c9cdf14d15fb15bd2286"
+checksum = "c99821af39c7df004bd411ece2ef22d9fba5a631b4489f3d9a0ac0d19637e2d0"
 dependencies = [
  "bdk_chain",
  "bip39",
index 47aa6697c20164a29cd03faa91688c229bb862db..07a4336a26e4c8f14b9cf91e0a368f52c1726696 100644 (file)
@@ -12,7 +12,7 @@ readme = "README.md"
 license = "MIT"
 
 [dependencies]
-bdk_wallet = { version = "2.3.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] }
+bdk_wallet = { version = "2.4.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] }
 clap = { version = "4.6", features = ["derive","env"] }
 clap_complete = "4.6"
 dirs = {  version = "6.0.0" }
index 1b75e35f4c7ff552c5d7f01c221e08d501bb9575..e1e640fe60b45809b49bf0d2a79bc66be0f47f1e 100644 (file)
@@ -922,27 +922,35 @@ pub(crate) async fn handle_online_wallet_subcommand(
                         NO_EXPECTED_MEMPOOL_TXS,
                     );
 
-                    while let Some(block_event) = emitter.next_block()? {
-                        if block_event.block_height() % 10_000 == 0 {
-                            let percent_done = f64::from(block_event.block_height())
-                                / f64::from(blockchain_info.headers as u32)
-                                * 100f64;
-                            println!(
-                                "Applying block at height: {}, {:.2}% done.",
+                    let block_events = wallet.events_helper(|w| {
+                        while let Some(block_event) = emitter.next_block()? {
+                            if block_event.block_height() % 10_000 == 0 {
+                                let percent_done = f64::from(block_event.block_height())
+                                    / f64::from(blockchain_info.headers as u32)
+                                    * 100f64;
+                                println!(
+                                    "Applying block at height: {}, {:.2}% done.",
+                                    block_event.block_height(),
+                                    percent_done
+                                );
+                            }
+
+                            w.apply_block_connected_to(
+                                &block_event.block,
                                 block_event.block_height(),
-                                percent_done
-                            );
+                                block_event.connected_to(),
+                            )?;
                         }
-
-                        wallet.apply_block_connected_to(
-                            &block_event.block,
-                            block_event.block_height(),
-                            block_event.connected_to(),
-                        )?;
-                    }
+                        Ok::<_, Error>(())
+                    })?;
+                    print_wallet_events(&block_events);
 
                     let mempool_txs = emitter.mempool()?;
-                    wallet.apply_unconfirmed_txs(mempool_txs.update);
+                    let mempool_events = wallet.apply_unconfirmed_txs_events(mempool_txs.update);
+                    print_wallet_events(&mempool_events);
+
+                    let evicted_events = wallet.apply_evicted_txs_events(mempool_txs.evicted);
+                    print_wallet_events(&evicted_events);
                 }
                 #[cfg(feature = "cbf")]
                 KyotoClient { client } => {
@@ -1879,27 +1887,34 @@ pub async fn sync_wallet(client: &BlockchainClient, wallet: &mut Wallet) -> Resu
                     .filter(|tx| tx.chain_position.is_unconfirmed()),
             );
 
-            while let Some(block_event) = emitter.next_block()? {
-                if block_event.block_height() % 10_000 == 0 {
-                    let percent_done = f64::from(block_event.block_height())
-                        / f64::from(blockchain_info.headers as u32)
-                        * 100f64;
-                    println!(
-                        "Applying block at height: {}, {:.2}% done.",
+            let block_events = wallet.events_helper(|w| {
+                while let Some(block_event) = emitter.next_block()? {
+                    if block_event.block_height() % 10_000 == 0 {
+                        let percent_done = f64::from(block_event.block_height())
+                            / f64::from(blockchain_info.headers as u32)
+                            * 100f64;
+                        println!(
+                            "Applying block at height: {}, {:.2}% done.",
+                            block_event.block_height(),
+                            percent_done
+                        );
+                    }
+                    w.apply_block_connected_to(
+                        &block_event.block,
                         block_event.block_height(),
-                        percent_done
-                    );
+                        block_event.connected_to(),
+                    )?;
                 }
-
-                wallet.apply_block_connected_to(
-                    &block_event.block,
-                    block_event.block_height(),
-                    block_event.connected_to(),
-                )?;
-            }
+                Ok::<_, Error>(())
+            })?;
+            print_wallet_events(&block_events);
 
             let mempool_txs = emitter.mempool()?;
-            wallet.apply_unconfirmed_txs(mempool_txs.update);
+            let mempool_events = wallet.apply_unconfirmed_txs_events(mempool_txs.update);
+            print_wallet_events(&mempool_events);
+
+            let evicted_events = wallet.apply_evicted_txs_events(mempool_txs.evicted);
+            print_wallet_events(&evicted_events);
             Ok(())
         }
         #[cfg(feature = "cbf")]
index 3f77539a2d4d8632965b6dee5fc1366650a9398e..7d357e818b65b40ab2d2bb0058e1f96e89fb9a92 100644 (file)
@@ -44,9 +44,7 @@ use cli_table::{Cell, CellStruct, Style, Table};
     feature = "rpc",
     feature = "cbf"
 ))]
-use crate::commands::ClientType;
-#[cfg(any(feature = "electrum", feature = "esplora",))]
-use bdk_wallet::event::WalletEvent;
+use {crate::commands::ClientType, bdk_wallet::event::WalletEvent};
 
 use bdk_wallet::Wallet;
 #[cfg(any(feature = "sqlite", feature = "redb"))]
@@ -400,10 +398,12 @@ pub async fn sync_kyoto_client(
 
     let update = handle.update_subscriber.lock().await.update().await?;
     tracing::info!("Received update: applying to wallet");
-    wallet
-        .apply_update(update)
+    let events = wallet
+        .apply_update_events(update)
         .map_err(|e| Error::Generic(format!("Failed to apply update: {e}")))?;
 
+    print_wallet_events(&events);
+
     tracing::info!(
         "Chain tip: {}, Transactions: {}, Balance: {}",
         wallet.local_chain().tip().height(),
@@ -706,45 +706,61 @@ pub fn load_wallet_config(
 
     Ok((wallet_opts, network))
 }
-#[cfg(any(feature = "electrum", feature = "esplora",))]
-pub fn print_wallet_events(events: &Vec<WalletEvent>) {
+
+#[cfg(any(
+    feature = "electrum",
+    feature = "esplora",
+    feature = "cbf",
+    feature = "rpc"
+))]
+pub fn print_wallet_events(events: &[WalletEvent]) {
     for event in events {
         match event {
-            WalletEvent::TxConfirmed {
-                txid,
-                tx: _,
-                block_time,
-                old_block_time: _,
-            } => {
+            WalletEvent::ChainTipChanged { old_tip, new_tip } => {
                 eprintln!(
-                    "Transaction {} confirmed in block {:?}",
-                    txid, block_time.block_id
+                    "Chain tip advanced from height {} to {}",
+                    old_tip.height, new_tip.height
                 );
             }
+            WalletEvent::TxConfirmed {
+                txid,
+                block_time,
+                old_block_time,
+                ..
+            } => match old_block_time {
+                Some(old) => eprintln!(
+                    "Transaction {txid} re-confirmed at height {} (was height {}, likely a reorg)",
+                    block_time.block_id.height, old.block_id.height
+                ),
+                None => eprintln!(
+                    "Transaction {txid} confirmed at height {}",
+                    block_time.block_id.height
+                ),
+            },
             WalletEvent::TxUnconfirmed {
                 txid,
-                tx: _,
-                old_block_time: _,
-            } => {
-                eprintln!("Transaction {txid} became unconfirmed");
-            }
+                old_block_time,
+                ..
+            } => match old_block_time {
+                Some(old) => eprintln!(
+                    "Transaction {txid} became unconfirmed (was confirmed at height {}, likely a reorg)",
+                    old.block_id.height
+                ),
+                None => eprintln!("Transaction {txid} seen in mempool"),
+            },
             WalletEvent::TxReplaced {
-                txid,
-                tx: _,
-                conflicts: _,
+                txid, conflicts, ..
             } => {
-                eprintln!("Received new transaction: {txid}");
-            }
-            WalletEvent::TxDropped { txid, tx: _ } => {
-                eprintln!("Transaction {txid} has been dropped");
-            }
-            WalletEvent::ChainTipChanged { old_tip, new_tip } => {
+                let ids: Vec<String> = conflicts.iter().map(|(_, c)| c.to_string()).collect();
                 eprintln!(
-                    "Wallet has synced to {:?} chain tip from {:?}",
-                    new_tip.height, old_tip.height
+                    "Transaction {txid} was replaced (conflicts with: {})",
+                    ids.join(", ")
                 );
             }
-            _ => eprintln!(),
+            WalletEvent::TxDropped { txid, .. } => {
+                eprintln!("Transaction {txid} dropped from the mempool");
+            }
+            _ => {}
         }
     }
 }