Skip to content

Commit 2bb4aaa

Browse files
committed
feat(client): add get_mempool_recent_txs
1 parent 49c4d24 commit 2bb4aaa

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/async.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use log::{debug, error, info, trace};
2727
use reqwest::{header, Client, Response};
2828

2929
use crate::{
30-
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolStats,
31-
MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo,
32-
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
30+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolRecentTx,
31+
MempoolStats, MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus,
32+
Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
3333
};
3434

3535
#[derive(Debug, Clone)]
@@ -458,6 +458,11 @@ impl<S: Sleeper> AsyncClient<S> {
458458
self.get_response_json("/mempool").await
459459
}
460460

461+
// Get a list of the last 10 [`Transaction`]s to enter the mempool.
462+
pub async fn get_mempool_recent_txs(&self) -> Result<Vec<MempoolRecentTx>, Error> {
463+
self.get_response_json("/mempool/recent").await
464+
}
465+
461466
/// Get an map where the key is the confirmation target (in number of
462467
/// blocks) and the value is the estimated feerate (in sat/vB).
463468
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/blocking.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use bitcoin::hex::{DisplayHex, FromHex};
2828
use bitcoin::{Address, Block, BlockHash, MerkleBlock, Script, Transaction, Txid};
2929

3030
use crate::{
31-
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolStats,
32-
MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo,
33-
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
31+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolRecentTx,
32+
MempoolStats, MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus,
33+
Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
3434
};
3535

3636
#[derive(Debug, Clone)]
@@ -321,6 +321,11 @@ impl BlockingClient {
321321
self.get_response_json("/mempool")
322322
}
323323

324+
// Get a list of the last 10 [`Transaction`]s to enter the mempool.
325+
pub fn get_mempool_recent_txs(&self) -> Result<Vec<MempoolRecentTx>, Error> {
326+
self.get_response_json("/mempool/recent")
327+
}
328+
324329
/// Get an map where the key is the confirmation target (in number of
325330
/// blocks) and the value is the estimated feerate (in sat/vB).
326331
pub fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,56 @@ mod test {
908908
}
909909
}
910910

911+
#[cfg(all(feature = "blocking", feature = "async"))]
912+
#[tokio::test]
913+
async fn test_get_mempool_recent_txs() {
914+
let (blocking_client, async_client) = setup_clients().await;
915+
let address = BITCOIND
916+
.client
917+
.new_address_with_type(AddressType::Legacy)
918+
.unwrap();
919+
920+
let _miner = MINER.lock().await;
921+
922+
let mut expected_txids = Vec::new();
923+
for _ in 0..5 {
924+
let txid = BITCOIND
925+
.client
926+
.send_to_address(&address, Amount::from_sat(1000))
927+
.unwrap()
928+
.txid()
929+
.unwrap();
930+
expected_txids.push(txid);
931+
}
932+
933+
// Sleep for 5 seconds so transactions have time to propagate
934+
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
935+
936+
let recent_blocking = blocking_client.get_mempool_recent_txs().unwrap();
937+
let recent_async = async_client.get_mempool_recent_txs().await.unwrap();
938+
939+
assert_eq!(recent_blocking, recent_async);
940+
941+
// Should return up to 10 transactions
942+
assert!(recent_blocking.len() <= 10);
943+
assert!(!recent_blocking.is_empty());
944+
945+
// Our most recent transactions should be in there
946+
let returned_txids: Vec<Txid> = recent_blocking.iter().map(|tx| tx.txid).collect();
947+
for expected_txid in &expected_txids {
948+
assert!(
949+
returned_txids.contains(expected_txid),
950+
"Txid {} not found in recent mempool txs",
951+
expected_txid
952+
);
953+
}
954+
955+
for tx in &recent_blocking {
956+
assert!(tx.vsize > 0);
957+
assert!(tx.value > 0);
958+
}
959+
}
960+
911961
#[cfg(all(feature = "blocking", feature = "async"))]
912962
#[tokio::test]
913963
async fn test_get_fee_estimates() {

0 commit comments

Comments
 (0)