Skip to content

Commit df3b306

Browse files
committed
fix(api): use .round() on sat_per_vbyte_to_feerate
Only casting to `u64` would always round down. Rounding (`.round()`) and then casting to `u64` is more precise. Also updates the fee JSON on `test_feerate_parsing` with current values which include sub-1 sat fees: ``` curl https://mempool.space/api/fee-estimates | jq 'to_entries | sort_by(.key | tonumber) | from_entries' ```
1 parent f6a114b commit df3b306

2 files changed

Lines changed: 39 additions & 35 deletions

File tree

src/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ pub struct MempoolFeesSubmitPackage {
490490
pub(crate) fn sat_per_vbyte_to_feerate(estimates: HashMap<u16, f64>) -> HashMap<u16, FeeRate> {
491491
estimates
492492
.into_iter()
493-
.map(|(k, v)| (k, FeeRate::from_sat_per_kwu((v * 250_000.0) as u64)))
493+
.map(|(k, v)| (k, FeeRate::from_sat_per_kwu((v * 250_000.0).round() as u64)))
494494
.collect()
495495
}
496496

src/lib.rs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -510,36 +510,35 @@ mod test {
510510
fn test_feerate_parsing() {
511511
let esplora_fees_raw = serde_json::from_str::<HashMap<u16, f64>>(
512512
r#"{
513-
"25": 1.015,
514-
"5": 2.3280000000000003,
515-
"12": 2.0109999999999997,
516-
"15": 1.018,
517-
"17": 1.018,
518-
"11": 2.0109999999999997,
519-
"3": 3.01,
520-
"2": 4.9830000000000005,
521-
"6": 2.2359999999999998,
522-
"21": 1.018,
523-
"13": 1.081,
524-
"7": 2.2359999999999998,
525-
"8": 2.2359999999999998,
526-
"16": 1.018,
527-
"20": 1.018,
528-
"22": 1.017,
529-
"23": 1.017,
530-
"504": 1,
531-
"9": 2.2359999999999998,
532-
"14": 1.018,
533-
"10": 2.0109999999999997,
534-
"24": 1.017,
535-
"1008": 1,
536-
"1": 4.9830000000000005,
537-
"4": 2.3280000000000003,
538-
"19": 1.018,
539-
"144": 1,
540-
"18": 1.018
541-
}
542-
"#,
513+
"1": 1.952,
514+
"2": 1.952,
515+
"3": 1.199,
516+
"4": 1.013,
517+
"5": 1.013,
518+
"6": 1.013,
519+
"7": 1.013,
520+
"8": 1.013,
521+
"9": 1.013,
522+
"10": 1.013,
523+
"11": 1.013,
524+
"12": 1.013,
525+
"13": 0.748,
526+
"14": 0.748,
527+
"15": 0.748,
528+
"16": 0.748,
529+
"17": 0.748,
530+
"18": 0.748,
531+
"19": 0.748,
532+
"20": 0.748,
533+
"21": 0.748,
534+
"22": 0.748,
535+
"23": 0.748,
536+
"24": 0.748,
537+
"25": 0.748,
538+
"144": 0.693,
539+
"504": 0.693,
540+
"1008": 0.693
541+
}"#,
543542
)
544543
.unwrap();
545544

@@ -550,16 +549,21 @@ mod test {
550549
assert!(convert_fee_rate(1, HashMap::new()).is_none());
551550
assert_eq!(
552551
convert_fee_rate(6, esplora_fees.clone()),
553-
Some(FeeRate::from_sat_per_kwu((2.236_f64 * 250_000.0) as u64))
552+
Some(FeeRate::from_sat_per_kwu(
553+
(1.013_f64 * 250_000.0).round() as u64
554+
)),
555+
"should inherit from value for target=6"
554556
);
555557
assert_eq!(
556558
convert_fee_rate(26, esplora_fees.clone()),
557-
Some(FeeRate::from_sat_per_kwu((1.015_f64 * 250_000.0) as u64)),
558-
"should inherit from value for 25"
559+
Some(FeeRate::from_sat_per_kwu(
560+
(0.748_f64 * 250_000.0).round() as u64
561+
)),
562+
"should inherit from value for target=25"
559563
);
560564
assert!(
561565
convert_fee_rate(0, esplora_fees).is_none(),
562-
"should not return feerate for 0 target"
566+
"should not return feerate for target=0"
563567
);
564568
}
565569

0 commit comments

Comments
 (0)