From 5bfd4e678a17173ad182b59da7b291d432ab503b Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Fri, 6 Dec 2024 16:16:42 +0300 Subject: [PATCH] dynamic balances for the chosen wallet Signed-off-by: Uncle Stretch --- src/action.rs | 1 + src/components/wallet/accounts.rs | 28 +++++++----- src/components/wallet/balance.rs | 74 ++++++++++++++++++++++--------- src/components/wallet/mod.rs | 2 +- src/components/wallet/overview.rs | 2 +- 5 files changed, 74 insertions(+), 33 deletions(-) diff --git a/src/action.rs b/src/action.rs index 75cecbb..fd319e7 100644 --- a/src/action.rs +++ b/src/action.rs @@ -32,6 +32,7 @@ pub enum Action { BalanceRequest([u8; 32], bool), BalanceResponse([u8; 32], SystemAccount), + BalanceSetActive(Option), RenameAccount(String), RenameAddressBookRecord(String), diff --git a/src/components/wallet/accounts.rs b/src/components/wallet/accounts.rs index 01cf7a8..b556d09 100644 --- a/src/components/wallet/accounts.rs +++ b/src/components/wallet/accounts.rs @@ -81,6 +81,14 @@ impl Accounts { } } + fn set_balance_active(&mut self, index: usize) { + if let Some(action_tx) = &self.action_tx { + let account_id = self.wallet_keys[index].account_id; + let _ = action_tx.send(Action::BalanceSetActive( + self.balances.get(&account_id).cloned())); + } + } + fn create_new_account(&mut self, name: String) { let (pair, seed) = Pair::generate(); let secret_seed = hex::encode(seed); @@ -276,23 +284,15 @@ impl Accounts { }; self.table_state.select(Some(0)); self.scroll_state = self.scroll_state.content_length(self.wallet_keys.len()); - self.send_wallet_change(0); + self.set_balance_active(0); Ok(()) } - fn send_wallet_change(&mut self, index: usize) { - if let Some(action_tx) = &self.action_tx { - let _ = action_tx.send(Action::UsedAccount(self.wallet_keys[index] - .pair_signer - .account_id() - .clone())); - } - } - fn first_row(&mut self) { if self.wallet_keys.len() > 0 { self.table_state.select(Some(0)); self.scroll_state = self.scroll_state.position(0); + self.set_balance_active(0); } } @@ -309,6 +309,7 @@ impl Accounts { }; self.table_state.select(Some(i)); self.scroll_state = self.scroll_state.position(i); + self.set_balance_active(i); } fn last_row(&mut self) { @@ -316,6 +317,7 @@ impl Accounts { let last = self.wallet_keys.len() - 1; self.table_state.select(Some(last)); self.scroll_state = self.scroll_state.position(last); + self.set_balance_active(last); } } @@ -332,6 +334,7 @@ impl Accounts { }; self.table_state.select(Some(i)); self.scroll_state = self.scroll_state.position(i); + self.set_balance_active(i); } fn prepare_u128(&self, value: u128, after: usize, ticker: Option<&str>) -> String { @@ -385,6 +388,11 @@ impl Component for Accounts { Action::UpdateAccountName(new_name) => self.rename_account(new_name), Action::BalanceResponse(account_id, balance) => { let _ = self.balances.insert(account_id, balance); + if let Some(index) = self.table_state.selected() { + if self.wallet_keys[index].account_id == account_id { + self.set_balance_active(index); + } + } }, _ => {} }; diff --git a/src/components/wallet/balance.rs b/src/components/wallet/balance.rs index 66d5a48..529c74a 100644 --- a/src/components/wallet/balance.rs +++ b/src/components/wallet/balance.rs @@ -8,6 +8,7 @@ use ratatui::{ use super::{Component, PartialComponent, CurrentTab}; use crate::{ + widgets::DotSpinner, action::Action, config::Config, palette::StylePalette, @@ -20,6 +21,7 @@ pub struct Balance { transferable_balance: Option, locked_balance: Option, bonded_balance: Option, + nonce: Option, palette: StylePalette } @@ -39,13 +41,19 @@ impl Balance { transferable_balance: None, locked_balance: None, bonded_balance: None, + nonce: None, palette: StylePalette::default(), } } - fn prepare_u128(&self, value: u128, after: usize) -> String { - let value = value as f64 / 10f64.powi(18); - format!("{:.after$}", value) + fn prepare_u128(&self, maybe_value: Option, ticker: Option<&str>, after: usize) -> String { + match maybe_value { + Some(value) => { + let value = value as f64 / 10f64.powi(18); + format!("{:.after$}{}", value, ticker.unwrap_or_default()) + }, + None => DotSpinner::default().to_string() + } } } @@ -73,6 +81,28 @@ impl Component for Balance { fn update(&mut self, action: Action) -> Result> { match action { + Action::BalanceSetActive(maybe_balance) => { + match maybe_balance { + Some(balance) => { + self.transferable_balance = Some(balance.free); + self.locked_balance = Some(balance.reserved); + self.bonded_balance = Some(balance.frozen); + + let total_balance = balance.free + .saturating_add(balance.reserved) + .saturating_add(balance.frozen); + self.total_balance = Some(total_balance); + self.nonce = Some(balance.nonce); + }, + None => { + self.transferable_balance = None; + self.locked_balance = None; + self.bonded_balance = None; + self.total_balance = None; + self.nonce = None; + } + } + }, _ => {} }; Ok(None) @@ -85,43 +115,45 @@ impl Component for Balance { let table = Table::new( [ + Row::new(vec![ + Cell::from(Text::from("nonce: ".to_string()).alignment(Alignment::Left)), + Cell::from(Text::from(self.prepare_u128( + self.nonce.map(|n| n as u128), + None, + 0)).alignment(Alignment::Right)), + ]), Row::new(vec![ Cell::from(Text::from("account: ".to_string()).alignment(Alignment::Left)), Cell::from(Text::from(self.prepare_u128( - self.total_balance.unwrap_or_default(), - Self::DECIMALS_FOR_BALANCE, - )).alignment(Alignment::Center)), - Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)) + self.total_balance, + Some(" CSPR"), + Self::DECIMALS_FOR_BALANCE)).alignment(Alignment::Right)), ]), Row::new(vec![ Cell::from(Text::from("free: ".to_string()).alignment(Alignment::Left)), Cell::from(Text::from(self.prepare_u128( - self.transferable_balance.unwrap_or_default(), - Self::DECIMALS_FOR_BALANCE, - )).alignment(Alignment::Center)), - Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)), + self.transferable_balance, + Some(" CSPR"), + Self::DECIMALS_FOR_BALANCE)).alignment(Alignment::Right)) ]), Row::new(vec![ Cell::from(Text::from("locked: ".to_string()).alignment(Alignment::Left)), Cell::from(Text::from(self.prepare_u128( - self.locked_balance.unwrap_or_default(), - Self::DECIMALS_FOR_BALANCE, - )).alignment(Alignment::Center)), - Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)), + self.locked_balance, + Some(" CSPR"), + Self::DECIMALS_FOR_BALANCE)).alignment(Alignment::Right)), ]), Row::new(vec![ Cell::from(Text::from("bonded: ".to_string()).alignment(Alignment::Left)), Cell::from(Text::from(self.prepare_u128( - self.bonded_balance.unwrap_or_default(), - Self::DECIMALS_FOR_BALANCE, - )).alignment(Alignment::Center)), - Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)), + self.bonded_balance, + Some(" CSPR"), + Self::DECIMALS_FOR_BALANCE)).alignment(Alignment::Right)), ]), ], [ Constraint::Max(10), - Constraint::Min(0), - Constraint::Length(5), + Constraint::Min(14), ] ) .block(Block::bordered() diff --git a/src/components/wallet/mod.rs b/src/components/wallet/mod.rs index 18da2d1..4853cca 100644 --- a/src/components/wallet/mod.rs +++ b/src/components/wallet/mod.rs @@ -229,6 +229,6 @@ pub fn account_layout(area: Rect) -> [Rect; 3] { Layout::vertical([ Constraint::Max(4), Constraint::Min(0), - Constraint::Max(6), + Constraint::Max(7), ]).areas(place) } diff --git a/src/components/wallet/overview.rs b/src/components/wallet/overview.rs index 23cb1a4..e82ac49 100644 --- a/src/components/wallet/overview.rs +++ b/src/components/wallet/overview.rs @@ -28,7 +28,7 @@ impl Default for Overview { } impl Overview { - const DECIMALS_FOR_BALANCE: usize = 5; + const DECIMALS_FOR_BALANCE: usize = 6; pub fn new() -> Self { Self {