//! ```
use crate::types::FeeRate;
+use crate::wallet::Vbytes;
use crate::{database::Database, WeightedUtxo};
use crate::{error::Error, Utxo};
impl OutputGroup {
fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
- let fee = (TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as f32 / 4.0
- * fee_rate.as_sat_vb();
+ let fee =
+ (TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight).vbytes() * fee_rate.as_sat_vb();
let effective_value = weighted_utxo.utxo.txout().value as i64 - fee.ceil() as i64;
OutputGroup {
weighted_utxo,
assert_eq!(result.selected.len(), 1);
assert_eq!(result.selected_amount(), 100_000);
- let input_size = (TXIN_BASE_WEIGHT as f32) / 4.0 + P2WPKH_WITNESS_SIZE as f32 / 4.0;
+ let input_size = (TXIN_BASE_WEIGHT + P2WPKH_WITNESS_SIZE).vbytes();
let epsilon = 0.5;
assert!((1.0 - (result.fee_amount / input_size)).abs() < epsilon);
}
return Err(Error::IrreplaceableTransaction);
}
- let vbytes = tx.get_weight() as f32 / 4.0;
+ let vbytes = tx.get_weight().vbytes();
let feerate = details.fee.ok_or(Error::FeeRateUnavailable)? as f32 / vbytes;
// remove the inputs from the tx and process them
}
}
+/// Trait implemented by types that can be used to measure weight units.
+pub trait Vbytes {
+ /// Convert weight units to virtual bytes.
+ fn vbytes(self) -> f32;
+}
+
+impl Vbytes for usize {
+ fn vbytes(self) -> f32 {
+ self as f32 / 4.0
+ }
+}
+
#[cfg(test)]
pub(crate) mod test {
use std::str::FromStr;
dust_change = true;
)*
- let tx_fee_rate = $fees as f32 / (tx.get_weight() as f32 / 4.0);
+ let tx_fee_rate = $fees as f32 / (tx.get_weight().vbytes());
let fee_rate = $fee_rate.as_sat_vb();
if !dust_change {