From 5cdc5fb58a4635ce6a302093c78b511255bbc1a3 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Thu, 29 Jul 2021 09:58:47 +1000 Subject: [PATCH] Move estimate -> fee rate logic to esplora module Currently we have duplicate code for converting the fee estimate we get back from esplora into a fee rate. This logic can be moved to a separate function and live in the `esplora` module. --- src/blockchain/esplora/mod.rs | 19 +++++++++++++++++++ src/blockchain/esplora/reqwest.rs | 14 +------------- src/blockchain/esplora/ureq.rs | 14 +------------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/blockchain/esplora/mod.rs b/src/blockchain/esplora/mod.rs index b3fe721e..917aa4ad 100644 --- a/src/blockchain/esplora/mod.rs +++ b/src/blockchain/esplora/mod.rs @@ -17,6 +17,7 @@ //! Please note, to configure the Esplora HTTP client correctly use one of: //! Blocking: --features='esplora,ureq' //! Async: --features='async-interface,esplora,reqwest' --no-default-features +use std::collections::HashMap; use std::fmt; use std::io; @@ -25,6 +26,9 @@ use serde::Deserialize; use bitcoin::consensus; use bitcoin::{BlockHash, Txid}; +use crate::error::Error; +use crate::FeeRate; + #[cfg(all( feature = "esplora", feature = "reqwest", @@ -59,6 +63,21 @@ mod ureq; ))] pub use self::ureq::*; +fn into_fee_rate(target: usize, estimates: HashMap) -> Result { + let fee_val = estimates + .into_iter() + .map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::()?, v))) + .collect::, _>>() + .map_err(|e| Error::Generic(e.to_string()))? + .into_iter() + .take_while(|(k, _)| k <= &target) + .map(|(_, v)| v) + .last() + .unwrap_or(1.0); + + Ok(FeeRate::from_sat_per_vb(fee_val as f32)) +} + /// Data type used when fetching transaction history from Esplora. #[derive(Deserialize)] pub struct EsploraGetHistory { diff --git a/src/blockchain/esplora/reqwest.rs b/src/blockchain/esplora/reqwest.rs index 6666792e..056c3913 100644 --- a/src/blockchain/esplora/reqwest.rs +++ b/src/blockchain/esplora/reqwest.rs @@ -119,19 +119,7 @@ impl Blockchain for EsploraBlockchain { fn estimate_fee(&self, target: usize) -> Result { let estimates = self.url_client._get_fee_estimates().await?; - - let fee_val = estimates - .into_iter() - .map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::()?, v))) - .collect::, _>>() - .map_err(|e| Error::Generic(e.to_string()))? - .into_iter() - .take_while(|(k, _)| k <= &target) - .map(|(_, v)| v) - .last() - .unwrap_or(1.0); - - Ok(FeeRate::from_sat_per_vb(fee_val as f32)) + super::into_fee_rate(target, estimates) } } diff --git a/src/blockchain/esplora/ureq.rs b/src/blockchain/esplora/ureq.rs index 0efe483c..24a37e86 100644 --- a/src/blockchain/esplora/ureq.rs +++ b/src/blockchain/esplora/ureq.rs @@ -112,19 +112,7 @@ impl Blockchain for EsploraBlockchain { fn estimate_fee(&self, target: usize) -> Result { let estimates = self.url_client._get_fee_estimates()?; - - let fee_val = estimates - .into_iter() - .map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::()?, v))) - .collect::, _>>() - .map_err(|e| Error::Generic(e.to_string()))? - .into_iter() - .take_while(|(k, _)| k <= &target) - .map(|(_, v)| v) - .last() - .unwrap_or(1.0); - - Ok(FeeRate::from_sat_per_vb(fee_val as f32)) + super::into_fee_rate(target, estimates) } }