Skip to content

Commit 41879dd

Browse files
committed
feat(client): add get_mempool_address_txs and get_mempool_scripthash_txs
1 parent d8d2a25 commit 41879dd

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

src/async.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<S: Sleeper> AsyncClient<S> {
401401
self.get_response_json(&path).await
402402
}
403403

404-
/// Get transaction history for the specified address/scripthash, sorted with newest first.
404+
/// Get transaction history for the specified address, sorted with newest first.
405405
///
406406
/// Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
407407
/// More can be requested by specifying the last txid seen by the previous query.
@@ -418,7 +418,14 @@ impl<S: Sleeper> AsyncClient<S> {
418418
self.get_response_json(&path).await
419419
}
420420

421-
/// Get confirmed transaction history for the specified address/scripthash,
421+
/// Get mempool [`Transaction`]s for the specified [`Address`], sorted with newest first.
422+
pub async fn get_mempool_address_txs(&self, address: &Address) -> Result<Vec<Tx>, Error> {
423+
let path = format!("/address/{address}/txs/mempool");
424+
425+
self.get_response_json(&path).await
426+
}
427+
428+
/// Get transaction history for the specified address/scripthash,
422429
/// sorted with newest first. Returns 25 transactions per page.
423430
/// More can be requested by specifying the last txid seen by the previous
424431
/// query.
@@ -436,6 +443,15 @@ impl<S: Sleeper> AsyncClient<S> {
436443
self.get_response_json(&path).await
437444
}
438445

446+
/// Get mempool [`Transaction`] history for the
447+
/// specified [`Script`] hash, sorted with newest first.
448+
pub async fn get_mempool_scripthash_txs(&self, script: &Script) -> Result<Vec<Tx>, Error> {
449+
let script_hash = sha256::Hash::hash(script.as_bytes());
450+
let path = format!("/scripthash/{script_hash:x}/txs/mempool");
451+
452+
self.get_response_json(&path).await
453+
}
454+
439455
/// Get an map where the key is the confirmation target (in number of
440456
/// blocks) and the value is the estimated feerate (in sat/vB).
441457
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/blocking.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ impl BlockingClient {
335335
self.get_response_json(&path)
336336
}
337337

338-
/// Get transaction history for the specified address/scripthash, sorted with newest first.
338+
/// Get transaction history for the specified address, sorted with newest
339+
/// first.
339340
///
340341
/// Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
341342
/// More can be requested by specifying the last txid seen by the previous query.
@@ -352,7 +353,14 @@ impl BlockingClient {
352353
self.get_response_json(&path)
353354
}
354355

355-
/// Get confirmed transaction history for the specified address/scripthash,
356+
/// Get mempool [`Transaction`]s for the specified [`Address`], sorted with newest first.
357+
pub fn get_mempool_address_txs(&self, address: &Address) -> Result<Vec<Tx>, Error> {
358+
let path = format!("/address/{address}/txs/mempool");
359+
360+
self.get_response_json(&path)
361+
}
362+
363+
/// Get transaction history for the specified scripthash,
356364
/// sorted with newest first. Returns 25 transactions per page.
357365
/// More can be requested by specifying the last txid seen by the previous
358366
/// query.
@@ -369,6 +377,15 @@ impl BlockingClient {
369377
self.get_response_json(&path)
370378
}
371379

380+
/// Get mempool [`Transaction`] history for the
381+
/// specified [`Script`] hash, sorted with newest first.
382+
pub fn get_mempool_scripthash_txs(&self, script: &Script) -> Result<Vec<Tx>, Error> {
383+
let script_hash = sha256::Hash::hash(script.as_bytes());
384+
let path = format!("/scripthash/{script_hash:x}/txs/mempool");
385+
386+
self.get_response_json(&path)
387+
}
388+
372389
/// Gets some recent block summaries starting at the tip or at `height` if
373390
/// provided.
374391
///

src/lib.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,50 @@ mod test {
924924
assert_eq!(scripthash_txs_txids, scripthash_txs_txids_async);
925925
}
926926

927+
#[cfg(all(feature = "blocking", feature = "async"))]
928+
#[tokio::test]
929+
async fn test_get_mempool_scripthash_txs() {
930+
let (blocking_client, async_client) = setup_clients().await;
931+
932+
let address = BITCOIND
933+
.client
934+
.new_address_with_type(AddressType::Legacy)
935+
.unwrap();
936+
937+
let txid = BITCOIND
938+
.client
939+
.send_to_address(&address, Amount::from_sat(1000))
940+
.unwrap()
941+
.txid()
942+
.unwrap();
943+
944+
// Sleep for 5 seconds so the transaction has time to propagate to electrs' mempool.
945+
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
946+
947+
let expected_tx = BITCOIND
948+
.client
949+
.get_transaction(txid)
950+
.unwrap()
951+
.into_model()
952+
.unwrap()
953+
.tx;
954+
let script = &expected_tx.output[0].script_pubkey;
955+
let scripthash_txs_txids: Vec<Txid> = blocking_client
956+
.get_mempool_scripthash_txs(script)
957+
.unwrap()
958+
.iter()
959+
.map(|tx| tx.txid)
960+
.collect();
961+
let scripthash_txs_txids_async: Vec<Txid> = async_client
962+
.get_mempool_scripthash_txs(script)
963+
.await
964+
.unwrap()
965+
.iter()
966+
.map(|tx| tx.txid)
967+
.collect();
968+
assert_eq!(scripthash_txs_txids, scripthash_txs_txids_async);
969+
}
970+
927971
#[cfg(all(feature = "blocking", feature = "async"))]
928972
#[tokio::test]
929973
async fn test_get_blocks() {
@@ -1176,6 +1220,37 @@ mod test {
11761220
assert_eq!(address_txs_async[0].txid, txid);
11771221
}
11781222

1223+
#[cfg(all(feature = "blocking", feature = "async"))]
1224+
#[tokio::test]
1225+
async fn test_get_mempool_address_txs() {
1226+
let (blocking_client, async_client) = setup_clients().await;
1227+
1228+
let address = BITCOIND
1229+
.client
1230+
.new_address_with_type(AddressType::Legacy)
1231+
.unwrap();
1232+
1233+
let txid = BITCOIND
1234+
.client
1235+
.send_to_address(&address, Amount::from_sat(1000))
1236+
.unwrap()
1237+
.txid()
1238+
.unwrap();
1239+
1240+
// Sleep for 5 seconds so the transaction has time to propagate to electrs' mempool.
1241+
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
1242+
1243+
let mempool_address_txs_blocking =
1244+
blocking_client.get_mempool_address_txs(&address).unwrap();
1245+
let mempool_address_txs_async = async_client
1246+
.get_mempool_address_txs(&address)
1247+
.await
1248+
.unwrap();
1249+
1250+
assert_eq!(mempool_address_txs_blocking, mempool_address_txs_async);
1251+
assert_eq!(mempool_address_txs_async[0].txid, txid);
1252+
}
1253+
11791254
#[cfg(all(feature = "blocking", feature = "async"))]
11801255
#[tokio::test]
11811256
async fn test_get_address_utxos() {

0 commit comments

Comments
 (0)