From: Mehmet Efe Umit Date: Wed, 19 Nov 2025 06:18:38 +0000 (-0800) Subject: refactor: move broadcast to a helper function X-Git-Url: http://internal-gitweb-vhost/script/%22https:/enum.FileStoreError.html?a=commitdiff_plain;h=f62287aff383555a301fd62be40064e1c54ec119;p=bdk-cli refactor: move broadcast to a helper function Prior to the Payjoin integration, we need to have the broadcast logic outside the broadcast command so that it can be shared between the existing online command and the Payjoin sender. --- diff --git a/src/handlers.rs b/src/handlers.rs index 1882b33..b863e25 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -703,66 +703,7 @@ pub(crate) async fn handle_online_wallet_subcommand( (Some(_), Some(_)) => panic!("Both `psbt` and `tx` options not allowed"), (None, None) => panic!("Missing `psbt` and `tx` option"), }; - let txid = match client { - #[cfg(feature = "electrum")] - Electrum { - client, - batch_size: _, - } => client - .transaction_broadcast(&tx) - .map_err(|e| Error::Generic(e.to_string()))?, - #[cfg(feature = "esplora")] - Esplora { - client, - parallel_requests: _, - } => client - .broadcast(&tx) - .await - .map(|()| tx.compute_txid()) - .map_err(|e| Error::Generic(e.to_string()))?, - #[cfg(feature = "rpc")] - RpcClient { client } => client - .send_raw_transaction(&tx) - .map_err(|e| Error::Generic(e.to_string()))?, - - #[cfg(feature = "cbf")] - KyotoClient { client } => { - let LightClient { - requester, - mut info_subscriber, - mut warning_subscriber, - update_subscriber: _, - node, - } = *client; - - let subscriber = tracing_subscriber::FmtSubscriber::new(); - tracing::subscriber::set_global_default(subscriber) - .map_err(|e| Error::Generic(format!("SetGlobalDefault error: {e}")))?; - - tokio::task::spawn(async move { node.run().await }); - tokio::task::spawn(async move { - select! { - info = info_subscriber.recv() => { - if let Some(info) = info { - tracing::info!("{info}"); - } - }, - warn = warning_subscriber.recv() => { - if let Some(warn) = warn { - tracing::warn!("{warn}"); - } - } - } - }); - let txid = tx.compute_txid(); - let wtxid = requester.broadcast_random(tx.clone()).await.map_err(|_| { - tracing::warn!("Broadcast was unsuccessful"); - Error::Generic("Transaction broadcast timed out after 30 seconds".into()) - })?; - tracing::info!("Successfully broadcast WTXID: {wtxid}"); - txid - } - }; + let txid = broadcast_transaction(client, tx).await?; Ok(serde_json::to_string_pretty(&json!({ "txid": txid }))?) } } @@ -1340,6 +1281,79 @@ pub async fn sync_wallet(client: BlockchainClient, wallet: &mut Wallet) -> Resul } } +#[cfg(any( + feature = "electrum", + feature = "esplora", + feature = "cbf", + feature = "rpc" +))] +/// Broadcasts a given transaction using the blockchain client. +pub async fn broadcast_transaction( + client: BlockchainClient, + tx: Transaction, +) -> Result { + match client { + #[cfg(feature = "electrum")] + Electrum { + client, + batch_size: _, + } => client + .transaction_broadcast(&tx) + .map_err(|e| Error::Generic(e.to_string())), + #[cfg(feature = "esplora")] + Esplora { + client, + parallel_requests: _, + } => client + .broadcast(&tx) + .await + .map(|()| tx.compute_txid()) + .map_err(|e| Error::Generic(e.to_string())), + #[cfg(feature = "rpc")] + RpcClient { client } => client + .send_raw_transaction(&tx) + .map_err(|e| Error::Generic(e.to_string())), + + #[cfg(feature = "cbf")] + KyotoClient { client } => { + let LightClient { + requester, + mut info_subscriber, + mut warning_subscriber, + update_subscriber: _, + node, + } = *client; + + let subscriber = tracing_subscriber::FmtSubscriber::new(); + tracing::subscriber::set_global_default(subscriber) + .map_err(|e| Error::Generic(format!("SetGlobalDefault error: {e}")))?; + + tokio::task::spawn(async move { node.run().await }); + tokio::task::spawn(async move { + select! { + info = info_subscriber.recv() => { + if let Some(info) = info { + tracing::info!("{info}"); + } + }, + warn = warning_subscriber.recv() => { + if let Some(warn) = warn { + tracing::warn!("{warn}"); + } + } + } + }); + let txid = tx.compute_txid(); + let wtxid = requester.broadcast_random(tx.clone()).await.map_err(|_| { + tracing::warn!("Broadcast was unsuccessful"); + Error::Generic("Transaction broadcast timed out after 30 seconds".into()) + })?; + tracing::info!("Successfully broadcast WTXID: {wtxid}"); + Ok(txid) + } + } +} + #[cfg(feature = "repl")] fn readline() -> Result { write!(std::io::stdout(), "> ").map_err(|e| Error::Generic(e.to_string()))?;