Skip to content

Commit f62287a

Browse files
committed
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.
1 parent a28076a commit f62287a

File tree

1 file changed

+74
-60
lines changed

1 file changed

+74
-60
lines changed

src/handlers.rs

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -703,66 +703,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
703703
(Some(_), Some(_)) => panic!("Both `psbt` and `tx` options not allowed"),
704704
(None, None) => panic!("Missing `psbt` and `tx` option"),
705705
};
706-
let txid = match client {
707-
#[cfg(feature = "electrum")]
708-
Electrum {
709-
client,
710-
batch_size: _,
711-
} => client
712-
.transaction_broadcast(&tx)
713-
.map_err(|e| Error::Generic(e.to_string()))?,
714-
#[cfg(feature = "esplora")]
715-
Esplora {
716-
client,
717-
parallel_requests: _,
718-
} => client
719-
.broadcast(&tx)
720-
.await
721-
.map(|()| tx.compute_txid())
722-
.map_err(|e| Error::Generic(e.to_string()))?,
723-
#[cfg(feature = "rpc")]
724-
RpcClient { client } => client
725-
.send_raw_transaction(&tx)
726-
.map_err(|e| Error::Generic(e.to_string()))?,
727-
728-
#[cfg(feature = "cbf")]
729-
KyotoClient { client } => {
730-
let LightClient {
731-
requester,
732-
mut info_subscriber,
733-
mut warning_subscriber,
734-
update_subscriber: _,
735-
node,
736-
} = *client;
737-
738-
let subscriber = tracing_subscriber::FmtSubscriber::new();
739-
tracing::subscriber::set_global_default(subscriber)
740-
.map_err(|e| Error::Generic(format!("SetGlobalDefault error: {e}")))?;
741-
742-
tokio::task::spawn(async move { node.run().await });
743-
tokio::task::spawn(async move {
744-
select! {
745-
info = info_subscriber.recv() => {
746-
if let Some(info) = info {
747-
tracing::info!("{info}");
748-
}
749-
},
750-
warn = warning_subscriber.recv() => {
751-
if let Some(warn) = warn {
752-
tracing::warn!("{warn}");
753-
}
754-
}
755-
}
756-
});
757-
let txid = tx.compute_txid();
758-
let wtxid = requester.broadcast_random(tx.clone()).await.map_err(|_| {
759-
tracing::warn!("Broadcast was unsuccessful");
760-
Error::Generic("Transaction broadcast timed out after 30 seconds".into())
761-
})?;
762-
tracing::info!("Successfully broadcast WTXID: {wtxid}");
763-
txid
764-
}
765-
};
706+
let txid = broadcast_transaction(client, tx).await?;
766707
Ok(serde_json::to_string_pretty(&json!({ "txid": txid }))?)
767708
}
768709
}
@@ -1340,6 +1281,79 @@ pub async fn sync_wallet(client: BlockchainClient, wallet: &mut Wallet) -> Resul
13401281
}
13411282
}
13421283

1284+
#[cfg(any(
1285+
feature = "electrum",
1286+
feature = "esplora",
1287+
feature = "cbf",
1288+
feature = "rpc"
1289+
))]
1290+
/// Broadcasts a given transaction using the blockchain client.
1291+
pub async fn broadcast_transaction(
1292+
client: BlockchainClient,
1293+
tx: Transaction,
1294+
) -> Result<Txid, Error> {
1295+
match client {
1296+
#[cfg(feature = "electrum")]
1297+
Electrum {
1298+
client,
1299+
batch_size: _,
1300+
} => client
1301+
.transaction_broadcast(&tx)
1302+
.map_err(|e| Error::Generic(e.to_string())),
1303+
#[cfg(feature = "esplora")]
1304+
Esplora {
1305+
client,
1306+
parallel_requests: _,
1307+
} => client
1308+
.broadcast(&tx)
1309+
.await
1310+
.map(|()| tx.compute_txid())
1311+
.map_err(|e| Error::Generic(e.to_string())),
1312+
#[cfg(feature = "rpc")]
1313+
RpcClient { client } => client
1314+
.send_raw_transaction(&tx)
1315+
.map_err(|e| Error::Generic(e.to_string())),
1316+
1317+
#[cfg(feature = "cbf")]
1318+
KyotoClient { client } => {
1319+
let LightClient {
1320+
requester,
1321+
mut info_subscriber,
1322+
mut warning_subscriber,
1323+
update_subscriber: _,
1324+
node,
1325+
} = *client;
1326+
1327+
let subscriber = tracing_subscriber::FmtSubscriber::new();
1328+
tracing::subscriber::set_global_default(subscriber)
1329+
.map_err(|e| Error::Generic(format!("SetGlobalDefault error: {e}")))?;
1330+
1331+
tokio::task::spawn(async move { node.run().await });
1332+
tokio::task::spawn(async move {
1333+
select! {
1334+
info = info_subscriber.recv() => {
1335+
if let Some(info) = info {
1336+
tracing::info!("{info}");
1337+
}
1338+
},
1339+
warn = warning_subscriber.recv() => {
1340+
if let Some(warn) = warn {
1341+
tracing::warn!("{warn}");
1342+
}
1343+
}
1344+
}
1345+
});
1346+
let txid = tx.compute_txid();
1347+
let wtxid = requester.broadcast_random(tx.clone()).await.map_err(|_| {
1348+
tracing::warn!("Broadcast was unsuccessful");
1349+
Error::Generic("Transaction broadcast timed out after 30 seconds".into())
1350+
})?;
1351+
tracing::info!("Successfully broadcast WTXID: {wtxid}");
1352+
Ok(txid)
1353+
}
1354+
}
1355+
}
1356+
13431357
#[cfg(feature = "repl")]
13441358
fn readline() -> Result<String, Error> {
13451359
write!(std::io::stdout(), "> ").map_err(|e| Error::Generic(e.to_string()))?;

0 commit comments

Comments
 (0)