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 authored and notmandatory committed Mar 6, 2024
1 parent c487b31 commit 82e87fb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,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 @@ -382,7 +382,7 @@ impl AsyncClient {
message: resp.text().await?,
})
} else {
Ok(resp.json::<HashMap<String, f64>>().await?)
Ok(resp.json::<HashMap<u16, f64>>().await?)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ 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> {
self.get_response_json("/fee-estimates")
}

Expand Down
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,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 @@ -336,7 +333,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 82e87fb

Please sign in to comment.