bdk_coin_select/metrics/
changeless.rs

1use super::change_lower_bound;
2use crate::{bnb::BnbMetric, float::Ordf32, ChangePolicy, CoinSelector, Target};
3
4#[derive(Clone, Debug)]
5/// Metric for finding changeless solutions only.
6pub struct Changeless {
7    /// The target parameters for the resultant selection.
8    pub target: Target,
9    /// Policy to determine whether a selection requires a change output.
10    pub change_policy: ChangePolicy,
11}
12
13impl BnbMetric for Changeless {
14    fn score(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32> {
15        if cs.is_target_met(self.target)
16            && cs.drain_value(self.target, self.change_policy).is_none()
17        {
18            Some(Ordf32(0.0))
19        } else {
20            None
21        }
22    }
23
24    fn bound(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32> {
25        if change_lower_bound(cs, self.target, self.change_policy).is_some() {
26            None
27        } else {
28            Some(Ordf32(0.0))
29        }
30    }
31
32    fn requires_ordering_by_descending_value_pwu(&self) -> bool {
33        true
34    }
35}