Skip to content

Commit d59775b

Browse files
fix: change target's type in fee estimate map
1 parent 3f300dc commit d59775b

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

src/async.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl AsyncClient {
380380

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

src/blocking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,15 @@ impl BlockingClient {
343343

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

352352
let map = match resp {
353353
Ok(resp) => {
354-
let map: HashMap<String, f64> = resp.into_json()?;
354+
let map: HashMap<u16, f64> = resp.into_json()?;
355355
Ok(map)
356356
}
357357
Err(ureq::Error::Status(code, resp)) => Err(Error::HttpResponse {

src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,13 @@ pub use r#async::AsyncClient;
8080

8181
/// Get a fee value in sats/vbytes from the estimates
8282
/// that matches the confirmation target set as parameter.
83-
pub fn convert_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<f32, Error> {
83+
pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Result<f32, Error> {
8484
let fee_val = {
85-
let mut pairs = estimates
86-
.into_iter()
87-
.filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
88-
.collect::<Vec<_>>();
85+
let mut pairs = estimates.into_iter().collect::<Vec<(u16, f64)>>();
8986
pairs.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(*k));
9087
pairs
9188
.into_iter()
92-
.find(|(k, _)| k <= &target)
89+
.find(|(k, _)| *k as usize <= target)
9390
.map(|(_, v)| v)
9491
.unwrap_or(1.0)
9592
};
@@ -332,7 +329,7 @@ mod test {
332329

333330
#[test]
334331
fn feerate_parsing() {
335-
let esplora_fees = serde_json::from_str::<HashMap<String, f64>>(
332+
let esplora_fees = serde_json::from_str::<HashMap<u16, f64>>(
336333
r#"{
337334
"25": 1.015,
338335
"5": 2.3280000000000003,

0 commit comments

Comments
 (0)