Skip to content

Commit

Permalink
fix: change target's type in fee estimate map
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirfomene committed Jan 2, 2024
1 parent 3f300dc commit d59775b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl AsyncClient {

/// Get an map where the key is the confirmation target (in number of blocks)
/// and the value is the estimated feerate (in sat/vB).
pub async fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {
let resp = self
.client
.get(&format!("{}/fee-estimates", self.url,))
Expand All @@ -393,7 +393,7 @@ impl AsyncClient {
message: resp.text().await?,
})
} else {
Ok(resp.json::<HashMap<String, f64>>().await?)
Ok(resp.json::<HashMap<u16, f64>>().await?)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,15 @@ impl BlockingClient {

/// Get an map where the key is the confirmation target (in number of blocks)
/// and the value is the estimated feerate (in sat/vB).
pub fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
pub fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {
let resp = self
.agent
.get(&format!("{}/fee-estimates", self.url,))
.call();

let map = match resp {
Ok(resp) => {
let map: HashMap<String, f64> = resp.into_json()?;
let map: HashMap<u16, f64> = resp.into_json()?;
Ok(map)
}
Err(ureq::Error::Status(code, resp)) => Err(Error::HttpResponse {
Expand Down
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,13 @@ pub use r#async::AsyncClient;

/// Get a fee value in sats/vbytes from the estimates
/// that matches the confirmation target set as parameter.
pub fn convert_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<f32, Error> {
pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Result<f32, Error> {
let fee_val = {
let mut pairs = estimates
.into_iter()
.filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
.collect::<Vec<_>>();
let mut pairs = estimates.into_iter().collect::<Vec<(u16, f64)>>();
pairs.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(*k));
pairs
.into_iter()
.find(|(k, _)| k <= &target)
.find(|(k, _)| *k as usize <= target)
.map(|(_, v)| v)
.unwrap_or(1.0)
};
Expand Down Expand Up @@ -332,7 +329,7 @@ mod test {

#[test]
fn feerate_parsing() {
let esplora_fees = serde_json::from_str::<HashMap<String, f64>>(
let esplora_fees = serde_json::from_str::<HashMap<u16, f64>>(
r#"{
"25": 1.015,
"5": 2.3280000000000003,
Expand Down

0 comments on commit d59775b

Please sign in to comment.