//! 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;
use bitcoin::consensus;
use bitcoin::{BlockHash, Txid};
+use crate::error::Error;
+use crate::FeeRate;
+
#[cfg(all(
feature = "esplora",
feature = "reqwest",
))]
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 {
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)
}
}
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)
}
}