From 667d451e1d451df74868cebfe689fdd18221fd21 Mon Sep 17 00:00:00 2001 From: Ignacio Pulice Donatto Date: Fri, 16 Jan 2026 12:22:35 -0300 Subject: [PATCH] Added a new method to get tx info in mempool --- src/bitcoin_client.rs | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/bitcoin_client.rs b/src/bitcoin_client.rs index c32b622..7d7fb5f 100644 --- a/src/bitcoin_client.rs +++ b/src/bitcoin_client.rs @@ -9,7 +9,7 @@ use bitcoin::{ Address, Amount, Block, BlockHash, CompressedPublicKey, Network, PublicKey, Transaction, Txid, }; use bitcoincore_rpc::json::{EstimateMode, GetBlockchainInfoResult}; -use bitcoincore_rpc::json::{GetRawTransactionResult, GetTxOutResult}; +use bitcoincore_rpc::json::{GetMempoolEntryResult, GetRawTransactionResult, GetTxOutResult}; use bitcoincore_rpc::{jsonrpc, Client, RpcApi}; use mockall::automock; use redact::Secret; @@ -67,12 +67,15 @@ impl BitcoinClient { wallet_name: &str, ) -> Result { let url = if !wallet_name.is_empty() { - if !wallet_name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') { + if !wallet_name + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') + { return Err(BitcoinClientError::InvalidWalletName { wallet_name: wallet_name.to_string(), }); } - + format!("{}/wallet/{}", url.expose_secret(), wallet_name) } else { url.expose_secret().to_string() @@ -130,7 +133,11 @@ pub trait BitcoinClientApi { address: &Address, ) -> Result<(), BitcoinClientError>; - fn get_new_address(&self, pk: PublicKey, network: Network) -> Result; + fn get_new_address( + &self, + pk: PublicKey, + network: Network, + ) -> Result; fn init_wallet(&self, wallet_name: &str) -> Result; @@ -140,6 +147,8 @@ pub trait BitcoinClientApi { fn estimate_smart_fee(&self) -> Result; + fn get_mempool_entry(&self, txid: &Txid) -> Result; + #[cfg(feature = "testing")] fn get_raw_mempool(&self) -> Result, BitcoinClientError>; @@ -415,9 +424,16 @@ impl BitcoinClientApi for BitcoinClient { Ok(()) } - fn get_new_address(&self, pk: PublicKey, network: Network) -> Result { - let compressed = CompressedPublicKey::try_from(pk) - .map_err(|e| BitcoinClientError::FailedToConvertPublicKey { error: e.to_string() })?; + fn get_new_address( + &self, + pk: PublicKey, + network: Network, + ) -> Result { + let compressed = CompressedPublicKey::try_from(pk).map_err(|e| { + BitcoinClientError::FailedToConvertPublicKey { + error: e.to_string(), + } + })?; let address = Address::p2wpkh(&compressed, network).as_unchecked().clone(); debug!("New address for network {:?}: {:?}", network, address); Ok(address.clone().require_network(network).unwrap()) @@ -515,6 +531,20 @@ impl BitcoinClientApi for BitcoinClient { Ok(()) } + fn get_mempool_entry(&self, txid: &Txid) -> Result { + let txid_str = txid.to_string(); + let args = vec![serde_json::Value::String(txid_str)]; + + let entry: GetMempoolEntryResult = + self.client.call("getmempoolentry", &args).map_err(|e| { + error!("Error getmempoolentry for {}: {:?}", txid, e); + BitcoinClientError::RpcError(e) + })?; + + debug!("getmempoolentry({}) -> height: {}", txid, entry.height); + Ok(entry) + } + #[cfg(feature = "testing")] fn get_raw_mempool(&self) -> Result, BitcoinClientError> { let txids = self.client.get_raw_mempool().map_err(|e| {