Skip to content

Commit b0ef4e7

Browse files
committed
Add waste field and getter in CoinSelectionResult
1 parent 23f2f80 commit b0ef4e7

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/wallet/coin_selection.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,16 @@ pub type DefaultCoinSelectionAlgorithm = LargestFirstCoinSelection; // make the
110110
// prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes) + script_len (1 bytes)
111111
pub(crate) const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4 + 1) * 4;
112112

113+
pub type Waste = i64;
113114
/// Result of a successful coin selection
114115
#[derive(Debug)]
115116
pub struct CoinSelectionResult {
116117
/// List of outputs selected for use as inputs
117118
pub selected: Vec<Utxo>,
118119
/// Total fee amount in satoshi
119120
pub fee_amount: u64,
121+
/// Waste value of current coin selection
122+
waste: Option<Waste>,
120123
}
121124

122125
impl CoinSelectionResult {
@@ -135,9 +138,28 @@ impl CoinSelectionResult {
135138
})
136139
.sum()
137140
}
138-
}
139141

140-
type Waste = i64;
142+
pub fn get_waste(
143+
&self,
144+
selected: Vec<WeightedUtxo>,
145+
cost_of_change: Option<u64>,
146+
target: u64,
147+
fee_rate: FeeRate,
148+
long_term_fee_rate: FeeRate,
149+
) -> Waste {
150+
match self.waste {
151+
Some(waste) => waste,
152+
None => calculate_waste(
153+
selected,
154+
cost_of_change,
155+
target,
156+
fee_rate,
157+
long_term_fee_rate,
158+
)
159+
.unwrap(),
160+
}
161+
}
162+
}
141163

142164
pub fn calculate_waste(
143165
selected: Vec<WeightedUtxo>,
@@ -152,7 +174,7 @@ pub fn calculate_waste(
152174
.map(|u| OutputGroup::new(u, fee_rate))
153175
.collect();
154176

155-
// if fee_rate < long_term_fee_rate timing cost can be negative
177+
// If fee_rate < long_term_fee_rate timing cost can be negative
156178
let timing_cost: i64 = utxo_groups.iter().fold(0, |acc, utxo| {
157179
let fee: i64 = utxo.fee as i64;
158180
let long_term_fee: i64 = long_term_fee_rate

0 commit comments

Comments
 (0)