fix send panel: reload coins

When going back to send panel, coins are fetched
again and passed to DefineSpendStep. A redraft
is triggered because some coins may have been
removed from the list and new coins were deposited.
This commit is contained in:
edouardparis 2024-03-01 12:10:15 +01:00
parent 710883b6a2
commit a6832ad0b7

View File

@ -1,4 +1,10 @@
use std::{cmp::Ordering, collections::HashMap, str::FromStr, sync::Arc};
use std::{
cmp::Ordering,
collections::{HashMap, HashSet},
iter::FromIterator,
str::FromStr,
sync::Arc,
};
use iced::{Command, Subscription};
use liana::{
@ -143,6 +149,11 @@ impl DefineSpend {
}
pub fn with_coins_sorted(mut self, blockheight: u32) -> Self {
self.sort_coins(blockheight);
self
}
fn sort_coins(&mut self, blockheight: u32) {
let timelock = self.timelock;
self.coins.sort_by(|(a, a_selected), (b, b_selected)| {
if *a_selected && !b_selected || !a_selected && *b_selected {
@ -157,7 +168,6 @@ impl DefineSpend {
a.block_height.cmp(&b.block_height)
}
});
self
}
pub fn self_send(mut self) -> Self {
@ -542,6 +552,35 @@ impl Step for DefineSpend {
}
Err(e) => self.warning = Some(e),
},
Message::Coins(res) => match res {
Ok(coins) => {
let selected: HashSet<OutPoint> =
HashSet::from_iter(self.coins.iter().filter_map(|(c, selected)| {
if *selected {
Some(c.outpoint)
} else {
None
}
}));
self.coins = coins
.into_iter()
.filter_map(|coin| {
if coin.spend_info.is_none() && !coin.is_immature {
let selected = selected.contains(&coin.outpoint);
Some((coin, selected))
} else {
None
}
})
.collect();
self.sort_coins(cache.blockheight as u32);
// In case some selected coins are not spendable anymore and
// new coins make more sense to be selected. A redraft is triggered
// if all forms are valid (checked in the redraft method)
self.redraft(daemon);
}
Err(e) => self.warning = Some(e),
},
_ => {}
};
Command::none()