Merge pull request #2415 from wpaulino/update-fee-anchors
[rust-lightning] / lightning / src / chain / chaininterface.rs
index 4204d714f1b12be5a7a1a04fed5092746562c827..6073ed61aaf0cc88beea1c899b687fbb407fbc93 100644 (file)
 //! disconnections, transaction broadcasting, and feerate information requests.
 
 use core::{cmp, ops::Deref};
+use core::convert::TryInto;
 
 use bitcoin::blockdata::transaction::Transaction;
 
+// TODO: Define typed abstraction over feerates to handle their conversions.
+pub(crate) fn compute_feerate_sat_per_1000_weight(fee_sat: u64, weight: u64) -> u32 {
+       (fee_sat * 1000 / weight).try_into().unwrap_or(u32::max_value())
+}
+pub(crate) const fn fee_for_weight(feerate_sat_per_1000_weight: u32, weight: u64) -> u64 {
+       ((feerate_sat_per_1000_weight as u64 * weight) + 1000 - 1) / 1000
+}
+
 /// An interface to send a transaction to the Bitcoin network.
 pub trait BroadcasterInterface {
        /// Sends a list of transactions out to (hopefully) be mined.