Skip to content

Commit 8e05c29

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 970ea94 commit 8e05c29

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/async.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,11 @@ impl<S: Sleeper> AsyncClient<S> {
372372
}
373373

374374
/// Broadcast a [`Transaction`] to Esplora
375-
pub async fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
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`] to Esplora

src/blocking.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl BlockingClient {
290290
}
291291

292292
/// Broadcast a [`Transaction`] to Esplora
293-
pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
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)