From 25fffb2c52f87715a69cee3fd0f6cda64e2185f2 Mon Sep 17 00:00:00 2001 From: edouard Date: Sun, 7 May 2023 16:54:50 +0200 Subject: [PATCH] Make home warning message relative to timelock close #461 --- gui/src/app/state/mod.rs | 5 +++-- gui/src/app/view/coins.rs | 37 +++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/gui/src/app/state/mod.rs b/gui/src/app/state/mod.rs index ff284362..4acf5d05 100644 --- a/gui/src/app/state/mod.rs +++ b/gui/src/app/state/mod.rs @@ -133,8 +133,9 @@ impl State for Home { let timelock = self.wallet.main_descriptor.first_timelock_value(); let seq = remaining_sequence(&coin, cache.blockheight as u32, timelock); - // number of block in a day - if seq <= 144 { + // Warn user for coins that are expiring in less than 10 percent of + // the timelock. + if seq <= timelock as u32 * 10 / 100 { self.expiring_coins.push(coin.outpoint); } if let Some(last) = &mut self.remaining_sequence { diff --git a/gui/src/app/view/coins.rs b/gui/src/app/view/coins.rs index a1e31e33..29f8b002 100644 --- a/gui/src/app/view/coins.rs +++ b/gui/src/app/view/coins.rs @@ -230,14 +230,31 @@ pub fn expire_message_units(sequence: u32) -> Vec { n_minutes -= n_months * 43830; let n_days = n_minutes / 1440; - [(n_years, "year"), (n_months, "month"), (n_days, "day")] - .iter() - .filter_map(|(n, u)| { - if *n != 0 { - Some(format!("{} {}{}", n, u, if *n > 1 { "s" } else { "" })) - } else { - None - } - }) - .collect() + #[allow(clippy::nonminimal_bool)] + if n_days != 0 || n_months != 0 || n_days != 0 { + [(n_years, "year"), (n_months, "month"), (n_days, "day")] + .iter() + .filter_map(|(n, u)| { + if *n != 0 { + Some(format!("{} {}{}", n, u, if *n > 1 { "s" } else { "" })) + } else { + None + } + }) + .collect() + } else { + n_minutes -= n_days * 1440; + let n_hours = n_minutes / 60; + n_minutes -= n_hours * 60; + [(n_hours, "hour"), (n_minutes, "minute")] + .iter() + .filter_map(|(n, u)| { + if *n != 0 { + Some(format!("{} {}{}", n, u, if *n > 1 { "s" } else { "" })) + } else { + None + } + }) + .collect() + } }