Skip to content

Commit 60dbb35

Browse files
committed
Fix rounding error in btc_per_kb
The function gives a `TooPreciceError` if 1e-5 is passed to it, i.e. 1 Sat / vb, due to the imprecise calculation in base2 f64. Move the division to after the conversion to Sats to remove the rounding error.
1 parent a1e9faf commit 60dbb35

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

types/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ impl std::error::Error for NumericError {}
8989

9090
/// Converts `fee_rate` in BTC/kB to `FeeRate`.
9191
fn btc_per_kb(btc_per_kb: f64) -> Result<Option<FeeRate>, ParseAmountError> {
92-
let btc_per_byte = btc_per_kb / 1000_f64;
93-
let sats_per_byte = Amount::from_btc(btc_per_byte)?;
92+
let sats_per_kb = Amount::from_btc(btc_per_kb)?;
93+
let sats_per_byte = sats_per_kb.to_sat() / 1000;
9494

9595
// Virtual bytes equal bytes before segwit.
96-
let rate = FeeRate::from_sat_per_vb(sats_per_byte.to_sat());
96+
let rate = FeeRate::from_sat_per_vb(sats_per_byte);
9797

9898
Ok(rate)
9999
}

0 commit comments

Comments
 (0)