Skip to content

Commit

Permalink
Select further away coins which meet target (#2724)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmart7t2 authored Nov 29, 2023
1 parent cf28497 commit 55916b8
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/subcommand/wallet/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,13 @@ impl TransactionBuilder {
current_value >= target_value && is_closer
};

if is_preference_and_closer || not_preference_but_closer {
let newly_meets_preference = if prefer_under {
best_value > target_value && current_value <= target_value
} else {
best_value < target_value && current_value >= target_value
};

if is_preference_and_closer || not_preference_but_closer || newly_meets_preference {
best_match = Some((*utxo, current_value))
}
}
Expand Down Expand Up @@ -1979,4 +1985,58 @@ mod tests {
outpoint(2),
);
}

#[test]
fn prefer_further_away_utxos_if_they_are_newly_under_target() {
let utxos = vec![
(outpoint(1), Amount::from_sat(510)),
(outpoint(2), Amount::from_sat(400)),
];

let mut tx_builder = TransactionBuilder::new(
satpoint(0, 0),
BTreeMap::new(),
utxos.into_iter().collect(),
BTreeSet::new(),
recipient(),
[change(0), change(1)],
FeeRate::try_from(1.0).unwrap(),
Target::Value(Amount::from_sat(10_000)),
);

assert_eq!(
tx_builder
.select_cardinal_utxo(Amount::from_sat(500), true)
.unwrap()
.0,
outpoint(2),
);
}

#[test]
fn prefer_further_away_utxos_if_they_are_newly_over_target() {
let utxos = vec![
(outpoint(1), Amount::from_sat(490)),
(outpoint(2), Amount::from_sat(600)),
];

let mut tx_builder = TransactionBuilder::new(
satpoint(0, 0),
BTreeMap::new(),
utxos.into_iter().collect(),
BTreeSet::new(),
recipient(),
[change(0), change(1)],
FeeRate::try_from(1.0).unwrap(),
Target::Value(Amount::from_sat(10_000)),
);

assert_eq!(
tx_builder
.select_cardinal_utxo(Amount::from_sat(500), false)
.unwrap()
.0,
outpoint(2),
);
}
}

0 comments on commit 55916b8

Please sign in to comment.