Skip to content

Commit c7ec977

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 c7ec977

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-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::InvalidResponse)?;
379+
Ok(txid)
381380
}
382381

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

src/blocking.rs

Lines changed: 7 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,11 @@ 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 =
310+
Txid::from_str(resp.as_str().unwrap_or_default()).map_err(Error::HexToArray)?;
311+
Ok(txid)
312+
}
309313
Err(e) => Err(Error::Minreq(e)),
310314
}
311315
}

0 commit comments

Comments
 (0)