Skip to content

Commit 98edbc5

Browse files
committed
Merge #65: fix: change target's type in fee estimate map
82e87fb fix: change target's type in fee estimate map (Vladimir Fomene) Pull request description: Fixes #64. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Updated the method for fetching fee estimates to use more efficient data types. - Improved the fee rate conversion process for enhanced accuracy and performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Top commit has no ACKs. Tree-SHA512: ba1d73ff3654e92691358ff35aa249736342820e720f603092b1327b4214efa533d2f2b9a662e8a228a0c42252157c82f0bc78be7c6e2891ac4d58a8ef403d5c
2 parents c487b31 + 82e87fb commit 98edbc5

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

src/async.rs

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

370370
/// Get an map where the key is the confirmation target (in number of blocks)
371371
/// and the value is the estimated feerate (in sat/vB).
372-
pub async fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
372+
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {
373373
let resp = self
374374
.client
375375
.get(&format!("{}/fee-estimates", self.url,))
@@ -382,7 +382,7 @@ impl AsyncClient {
382382
message: resp.text().await?,
383383
})
384384
} else {
385-
Ok(resp.json::<HashMap<String, f64>>().await?)
385+
Ok(resp.json::<HashMap<u16, f64>>().await?)
386386
}
387387
}
388388

src/blocking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl BlockingClient {
281281

282282
/// Get an map where the key is the confirmation target (in number of blocks)
283283
/// and the value is the estimated feerate (in sat/vB).
284-
pub fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
284+
pub fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {
285285
self.get_response_json("/fee-estimates")
286286
}
287287

src/lib.rs

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

8989
/// Get a fee value in sats/vbytes from the estimates
9090
/// that matches the confirmation target set as parameter.
91-
pub fn convert_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<f32, Error> {
91+
pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Result<f32, Error> {
9292
let fee_val = {
93-
let mut pairs = estimates
94-
.into_iter()
95-
.filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
96-
.collect::<Vec<_>>();
93+
let mut pairs = estimates.into_iter().collect::<Vec<(u16, f64)>>();
9794
pairs.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(*k));
9895
pairs
9996
.into_iter()
100-
.find(|(k, _)| k <= &target)
97+
.find(|(k, _)| *k as usize <= target)
10198
.map(|(_, v)| v)
10299
.unwrap_or(1.0)
103100
};
@@ -336,7 +333,7 @@ mod test {
336333

337334
#[test]
338335
fn feerate_parsing() {
339-
let esplora_fees = serde_json::from_str::<HashMap<String, f64>>(
336+
let esplora_fees = serde_json::from_str::<HashMap<u16, f64>>(
340337
r#"{
341338
"25": 1.015,
342339
"5": 2.3280000000000003,

0 commit comments

Comments
 (0)