Skip to content

Commit

Permalink
fix: use saturating sub while calculating change amount (#4026)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel <daniel@chainflip.io>
Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 21, 2023
1 parent 178df88 commit c9bcd5b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions state-chain/pallets/cf-environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,19 @@ impl<T: Config> Pallet<T> {
match utxo_selection_type {
UtxoSelectionType::SelectAllForRotation => {
let available_utxos = BitcoinAvailableUtxos::<T>::take();
(!available_utxos.is_empty()).then_some(available_utxos).map(|available_utxos| {
(
available_utxos.clone(),
available_utxos.iter().map(|Utxo { amount, .. }| *amount).sum::<u64>() -
(available_utxos.len() as u64) * fee_per_input_utxo -
fee_per_output_utxo - min_fee_required_per_tx,
)
})
(!available_utxos.is_empty()).then_some(available_utxos).and_then(
|available_utxos| {
available_utxos
.iter()
.map(|Utxo { amount, .. }| *amount)
.sum::<u64>()
.checked_sub(
((available_utxos.len() as u64) * fee_per_input_utxo) +
fee_per_output_utxo + min_fee_required_per_tx,
)
.map(|change_amount| (available_utxos, change_amount))
},
)
},
UtxoSelectionType::Some { output_amount, number_of_outputs } =>
BitcoinAvailableUtxos::<T>::try_mutate(|available_utxos| {
Expand Down

0 comments on commit c9bcd5b

Please sign in to comment.