Skip to content

Commit bc171c8

Browse files
committed
feat(chain): add AncestorPackage struct and fee_deficit()
1 parent 0f89edb commit bc171c8

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use bitcoin::{Amount, FeeRate, Weight};
2+
3+
/// Aggregated fee and weight for an unconfirmed ancestor chain.
4+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5+
pub struct AncestorPackage {
6+
/// Total weight of all unconfirmed transactions in the package.
7+
pub weight: Weight,
8+
/// Total fee of all unconfirmed transactions in the package.
9+
pub fee: Amount,
10+
}
11+
12+
impl AncestorPackage {
13+
/// Create a new [`AncestorPackage`].
14+
pub fn new(weight: Weight, fee: Amount) -> Self {
15+
Self { weight, fee }
16+
}
17+
18+
/// The additional fee a child transaction must contribute so that
19+
/// the package feerate reaches `target_feerate`.
20+
///
21+
/// Returns [`Amount::ZERO`] if the package already meets or exceeds
22+
/// the target.
23+
pub fn fee_deficit(&self, target_feerate: FeeRate) -> Amount {
24+
let required = target_feerate * self.weight;
25+
required.checked_sub(self.fee).unwrap_or(Amount::ZERO)
26+
}
27+
}

crates/chain/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ mod canonical_iter;
4848
pub use canonical_iter::*;
4949
mod canonical_view;
5050
pub use canonical_view::*;
51+
mod ancestor_package;
52+
pub use ancestor_package::*;
5153

5254
#[doc(hidden)]
5355
pub mod example_utils;

0 commit comments

Comments
 (0)