Skip to content

Commit 90b8a6e

Browse files
committed
feat(client)!: update broadcast API
- updates the return type of `broadcast` API. BREAKING CHANGE: changes the `broadcast` method to return the `txid` for broadcasted transaction, on both `AsyncClient` and `BlockingClient`.
1 parent 788b67b commit 90b8a6e

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/async.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,12 @@ impl<S: Sleeper> AsyncClient<S> {
371371
.await
372372
}
373373

374-
/// Broadcast a [`Transaction`] to Esplora.
375-
pub async fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
374+
/// Broadcast a [`Transaction`] to Esplora
375+
pub async fn broadcast(&self, transaction: &Transaction) -> Result<Txid, Error> {
376376
let body = serialize::<Transaction>(transaction).to_lower_hex_string();
377-
match self.post_request_bytes("/tx", body, None).await {
378-
Ok(_resp) => Ok(()),
379-
Err(e) => Err(e),
380-
}
377+
let response = self.post_request_bytes("/tx", body, None).await?;
378+
let txid = Txid::from_str(&response.text().await?).map_err(Error::HexToArray)?;
379+
Ok(txid)
381380
}
382381

383382
/// Broadcast a package of [`Transaction`]s to Esplora.

src/blocking.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ impl BlockingClient {
289289
self.get_opt_response_json(&format!("/tx/{txid}/outspend/{index}"))
290290
}
291291

292-
/// Broadcast a [`Transaction`] to Esplora.
293-
pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
292+
/// Broadcast a [`Transaction`] to Esplora
293+
pub fn broadcast(&self, transaction: &Transaction) -> Result<Txid, Error> {
294294
let request = self.post_request(
295295
"/tx",
296296
serialize(transaction)
@@ -305,7 +305,10 @@ impl BlockingClient {
305305
let message = resp.as_str().unwrap_or_default().to_string();
306306
Err(Error::HttpResponse { status, message })
307307
}
308-
Ok(_resp) => Ok(()),
308+
Ok(resp) => {
309+
let txid = Txid::from_str(resp.as_str()?).map_err(Error::HexToArray)?;
310+
Ok(txid)
311+
}
309312
Err(e) => Err(Error::Minreq(e)),
310313
}
311314
}

src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,4 +1408,41 @@ mod test {
14081408
assert_eq!(mempool_address_txs_blocking, mempool_address_txs_async);
14091409
assert_eq!(mempool_address_txs_blocking.len(), 5);
14101410
}
1411+
1412+
#[cfg(all(feature = "blocking", feature = "async"))]
1413+
#[tokio::test]
1414+
async fn test_broadcast() {
1415+
let (blocking_client, async_client) = setup_clients().await;
1416+
1417+
let address = BITCOIND
1418+
.client
1419+
.new_address_with_type(AddressType::Legacy)
1420+
.unwrap();
1421+
1422+
let txid = BITCOIND
1423+
.client
1424+
.send_to_address(&address, Amount::from_sat(1000))
1425+
.unwrap()
1426+
.txid()
1427+
.unwrap();
1428+
1429+
let tx = BITCOIND
1430+
.client
1431+
.get_transaction(txid)
1432+
.expect("tx should exist for given `txid`")
1433+
.into_model()
1434+
.expect("should convert successfully")
1435+
.tx;
1436+
1437+
let blocking_res = blocking_client
1438+
.broadcast(&tx)
1439+
.expect("should succesfully broadcast tx");
1440+
let async_res = async_client
1441+
.broadcast(&tx)
1442+
.await
1443+
.expect("should successfully broadcast tx");
1444+
1445+
assert_eq!(blocking_res, txid);
1446+
assert_eq!(async_res, txid);
1447+
}
14111448
}

0 commit comments

Comments
 (0)