Skip to content

Commit 78677f8

Browse files
committed
feat: add get_mempool_address_txs and get_mempool_scripthash_txs
1 parent 3e3144c commit 78677f8

File tree

3 files changed

+119
-4
lines changed

3 files changed

+119
-4
lines changed

src/async.rs

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

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

422-
/// Get confirmed transaction history for the specified address/scripthash,
422+
/// Get mempool transactions for the specified address, sorted with newest first.
423+
///
424+
/// Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
425+
/// More can be requested by specifying the last txid seen by the previous query.
426+
pub async fn get_mempool_address_txs(&self, address: &Address) -> Result<Vec<Tx>, Error> {
427+
let path = format!("/address/{address}/txs/mempool");
428+
429+
self.get_response_json(&path).await
430+
}
431+
432+
/// Get transaction history for the specified address/scripthash,
423433
/// sorted with newest first. Returns 25 transactions per page.
424434
/// More can be requested by specifying the last txid seen by the previous
425435
/// query.
@@ -437,6 +447,17 @@ impl<S: Sleeper> AsyncClient<S> {
437447
self.get_response_json(&path).await
438448
}
439449

450+
/// Get mempool transactions for the specified scripthash,
451+
/// sorted with newest first. Returns 25 transactions per page.
452+
/// More can be requested by specifying the last txid seen by the previous
453+
/// query.
454+
pub async fn get_mempool_scripthash_txs(&self, script: &Script) -> Result<Vec<Tx>, Error> {
455+
let script_hash = sha256::Hash::hash(script.as_bytes());
456+
let path = format!("/scripthash/{script_hash:x}/txs/mempool");
457+
458+
self.get_response_json(&path).await
459+
}
460+
440461
/// Get an map where the key is the confirmation target (in number of
441462
/// blocks) and the value is the estimated feerate (in sat/vB).
442463
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/blocking.rs

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

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

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

381+
/// Get mempool transaction history for the specified scripthash,
382+
/// sorted with newest first. Returns 25 transactions per page.
383+
/// More can be requested by specifying the last txid seen by the previous
384+
/// query.
385+
pub fn get_mempool_scripthash_txs(&self, script: &Script) -> Result<Vec<Tx>, Error> {
386+
let script_hash = sha256::Hash::hash(script.as_bytes());
387+
let path = format!("/scripthash/{script_hash:x}/txs/mempool");
388+
389+
self.get_response_json(&path)
390+
}
391+
373392
/// Gets some recent block summaries starting at the tip or at `height` if
374393
/// provided.
375394
///

src/lib.rs

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

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

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

0 commit comments

Comments
 (0)