]> Untitled Git - bdk/commitdiff
Move estimate -> fee rate logic to esplora module
authorTobin Harding <me@tobin.cc>
Wed, 28 Jul 2021 23:58:47 +0000 (09:58 +1000)
committerTobin Harding <me@tobin.cc>
Thu, 29 Jul 2021 00:12:19 +0000 (10:12 +1000)
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
src/blockchain/esplora/reqwest.rs
src/blockchain/esplora/ureq.rs

index b3fe721eef1150e37858f8a039e4f6a915ab3fc3..917aa4ad4401aff2243a41ed01cf2e588e3e941f 100644 (file)
@@ -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<String, f64>) -> Result<FeeRate, Error> {
+    let fee_val = estimates
+        .into_iter()
+        .map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::<usize>()?, v)))
+        .collect::<Result<Vec<_>, _>>()
+        .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 {
index 6666792e54a0c5193609314ec8b5160b62ce14fe..056c391345f8912fc7be4eecb19a270da55a2958 100644 (file)
@@ -119,19 +119,7 @@ impl Blockchain for EsploraBlockchain {
 
     fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
         let estimates = self.url_client._get_fee_estimates().await?;
-
-        let fee_val = estimates
-            .into_iter()
-            .map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::<usize>()?, v)))
-            .collect::<Result<Vec<_>, _>>()
-            .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)
     }
 }
 
index 0efe483cc0db5eb3a9e10bd77dfcb8e2b4c98210..24a37e865dc525ac73f57fbb0e968837c639ff75 100644 (file)
@@ -112,19 +112,7 @@ impl Blockchain for EsploraBlockchain {
 
     fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
         let estimates = self.url_client._get_fee_estimates()?;
-
-        let fee_val = estimates
-            .into_iter()
-            .map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::<usize>()?, v)))
-            .collect::<Result<Vec<_>, _>>()
-            .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)
     }
 }