Skip to content

Commit 3224994

Browse files
committed
Merge #151: feat(client)!: update broadcast API
90b8a6e feat(client)!: update `broadcast` API (Leonardo Lima) Pull request description: ### Description It's a remaining follow-up from #114, it does the remaining breaking change to broadcast API. ### Changelog Notice ``` ### Changed BREAKING CHANGE: changes the `broadcast` method to return the `txid` for broadcasted transaction, on both `AsyncClient` and `BlockingClient`. ``` ACKs for top commit: ValuedMammal: ACK 90b8a6e luisschwab: ACK 90b8a6e Tree-SHA512: 61d517fa1bf17aae1b737d32dffa49207e444ed9cdb6e83a1521c66131cdefd899a0eee2781933d4df8f6bf507fafbf11828c23c931f6eec4cb6a198061aabec
2 parents 8187765 + 90b8a6e commit 3224994

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
@@ -1411,4 +1411,41 @@ mod test {
14111411
assert_eq!(mempool_address_txs_blocking, mempool_address_txs_async);
14121412
assert_eq!(mempool_address_txs_blocking.len(), 5);
14131413
}
1414+
1415+
#[cfg(all(feature = "blocking", feature = "async"))]
1416+
#[tokio::test]
1417+
async fn test_broadcast() {
1418+
let (blocking_client, async_client) = setup_clients().await;
1419+
1420+
let address = BITCOIND
1421+
.client
1422+
.new_address_with_type(AddressType::Legacy)
1423+
.unwrap();
1424+
1425+
let txid = BITCOIND
1426+
.client
1427+
.send_to_address(&address, Amount::from_sat(1000))
1428+
.unwrap()
1429+
.txid()
1430+
.unwrap();
1431+
1432+
let tx = BITCOIND
1433+
.client
1434+
.get_transaction(txid)
1435+
.expect("tx should exist for given `txid`")
1436+
.into_model()
1437+
.expect("should convert successfully")
1438+
.tx;
1439+
1440+
let blocking_res = blocking_client
1441+
.broadcast(&tx)
1442+
.expect("should succesfully broadcast tx");
1443+
let async_res = async_client
1444+
.broadcast(&tx)
1445+
.await
1446+
.expect("should successfully broadcast tx");
1447+
1448+
assert_eq!(blocking_res, txid);
1449+
assert_eq!(async_res, txid);
1450+
}
14141451
}

0 commit comments

Comments
 (0)