From a6832ad0b7a881889e26c10c65f4d0ecd6e8c098 Mon Sep 17 00:00:00 2001 From: edouardparis Date: Fri, 1 Mar 2024 12:10:15 +0100 Subject: [PATCH] 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. --- gui/src/app/state/spend/step.rs | 43 +++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/gui/src/app/state/spend/step.rs b/gui/src/app/state/spend/step.rs index 0066bcc7..6efe4647 100644 --- a/gui/src/app/state/spend/step.rs +++ b/gui/src/app/state/spend/step.rs @@ -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 = + 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()