diff --git a/src/action.rs b/src/action.rs index c0aebc6..a3b8103 100644 --- a/src/action.rs +++ b/src/action.rs @@ -5,7 +5,8 @@ use subxt::utils::H256; use subxt::config::substrate::DigestItem; use crate::types::{ - ActionLevel, CasperExtrinsicDetails, EraInfo, EraRewardPoints, Nominator, PeerInformation, SessionKeyInfo, SystemAccount + ActionLevel, ActionTarget, CasperExtrinsicDetails, EraInfo, EraRewardPoints, + Nominator, PeerInformation, SessionKeyInfo, SystemAccount, }; #[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)] @@ -46,8 +47,7 @@ pub enum Action { TransferTo(String), TransferBalance(String, [u8; 32], u128), - WalletLog(String, ActionLevel), - ValidatorLog(String, ActionLevel), + EventLog(String, ActionLevel, ActionTarget), NewBestBlock(u32), NewBestHash(H256), diff --git a/src/components/nominator/event_log.rs b/src/components/nominator/event_log.rs index 47351f0..17fcc73 100644 --- a/src/components/nominator/event_log.rs +++ b/src/components/nominator/event_log.rs @@ -20,7 +20,7 @@ use crate::{ }; #[derive(Debug, Default)] -struct WalletLog { +struct LogDetails { time: chrono::DateTime, level: ActionLevel, message: String, @@ -31,7 +31,7 @@ pub struct EventLogs { is_active: bool, scroll_state: ScrollbarState, table_state: TableState, - logs: std::collections::VecDeque, + logs: std::collections::VecDeque, palette: StylePalette } @@ -43,7 +43,7 @@ impl Default for EventLogs { scroll_state: Default::default(), table_state: Default::default(), logs: std::collections::VecDeque::from(vec![ - WalletLog { + LogDetails { time: chrono::Local::now(), level: ActionLevel::Warn, message: "NOT FINALIZED PAGE! NEEDED IN ORDER TO SEE VALIDATORS POINTS".to_string(), @@ -58,7 +58,7 @@ impl EventLogs { //const MAX_LOGS: usize = 50; //fn add_new_log(&mut self, message: String, level: ActionLevel) { - // self.logs.push_front(WalletLog { + // self.logs.push_front(LogDetails { // time: chrono::Local::now(), // level, // message, @@ -156,7 +156,8 @@ impl Component for EventLogs { fn update(&mut self, _action: Action) -> Result> { //match action { - // Action::ValidatorLog(message, level) => self.add_new_log(message, level), + // Action::EventLog(message, level, target) if target == ActionTarget::NominatorLog => + // self.add_new_log(message, level), // _ => {} //}; Ok(None) diff --git a/src/components/validator/event_log.rs b/src/components/validator/event_log.rs index 695dc5c..9078613 100644 --- a/src/components/validator/event_log.rs +++ b/src/components/validator/event_log.rs @@ -13,14 +13,11 @@ use ratatui::{ use super::{Component, PartialComponent, CurrentTab}; use crate::{ - types::ActionLevel, - action::Action, - config::Config, - palette::StylePalette, + action::Action, config::Config, palette::StylePalette, types::{ActionLevel, ActionTarget} }; #[derive(Debug, Default)] -struct WalletLog { +struct LogDetails { time: chrono::DateTime, level: ActionLevel, message: String, @@ -31,7 +28,7 @@ pub struct EventLogs { is_active: bool, scroll_state: ScrollbarState, table_state: TableState, - logs: std::collections::VecDeque, + logs: std::collections::VecDeque, palette: StylePalette } @@ -39,7 +36,7 @@ impl EventLogs { const MAX_LOGS: usize = 50; fn add_new_log(&mut self, message: String, level: ActionLevel) { - self.logs.push_front(WalletLog { + self.logs.push_front(LogDetails { time: chrono::Local::now(), level, message, @@ -137,7 +134,8 @@ impl Component for EventLogs { fn update(&mut self, action: Action) -> Result> { match action { - Action::ValidatorLog(message, level) => self.add_new_log(message, level), + Action::EventLog(message, level, target) if target == ActionTarget::ValidatorLog => + self.add_new_log(message, level), _ => {} }; Ok(None) diff --git a/src/components/validator/stash_info.rs b/src/components/validator/stash_info.rs index 62c5bd2..942a37d 100644 --- a/src/components/validator/stash_info.rs +++ b/src/components/validator/stash_info.rs @@ -29,7 +29,7 @@ use std::sync::mpsc::Sender; use super::{PartialComponent, Component, CurrentTab}; use crate::casper::CasperConfig; -use crate::types::ActionLevel; +use crate::types::{ActionLevel, ActionTarget}; use crate::{ types::SessionKeyInfo, action::Action, @@ -76,7 +76,8 @@ impl StashInfo { fn log_event(&mut self, message: String, level: ActionLevel) { if let Some(action_tx) = &self.action_tx { - let _ = action_tx.send(Action::ValidatorLog(message, level)); + let _ = action_tx.send( + Action::EventLog(message, level, ActionTarget::ValidatorLog)); } } diff --git a/src/components/wallet/accounts.rs b/src/components/wallet/accounts.rs index 475d1d4..6ed1eb5 100644 --- a/src/components/wallet/accounts.rs +++ b/src/components/wallet/accounts.rs @@ -24,7 +24,7 @@ use tokio::sync::mpsc::UnboundedSender; use std::sync::mpsc::Sender; use super::{PartialComponent, Component, CurrentTab}; -use crate::types::{SystemAccount, ActionLevel}; +use crate::types::{ActionLevel, ActionTarget, SystemAccount}; use crate::widgets::DotSpinner; use crate::{ action::Action, @@ -96,7 +96,8 @@ impl Accounts { fn log_event(&mut self, message: String, level: ActionLevel) { if let Some(action_tx) = &self.action_tx { - let _ = action_tx.send(Action::WalletLog(message, level)); + let _ = action_tx.send( + Action::EventLog(message, level, ActionTarget::WalletLog)); } } diff --git a/src/components/wallet/address_book.rs b/src/components/wallet/address_book.rs index 89c6084..eae96d8 100644 --- a/src/components/wallet/address_book.rs +++ b/src/components/wallet/address_book.rs @@ -19,7 +19,7 @@ use tokio::sync::mpsc::UnboundedSender; use std::sync::mpsc::Sender; use super::{Component, PartialComponent, CurrentTab}; -use crate::types::{ActionLevel, SystemAccount}; +use crate::types::{ActionLevel, ActionTarget, SystemAccount}; use crate::widgets::DotSpinner; use crate::{ action::Action, @@ -94,7 +94,8 @@ impl AddressBook { fn log_event(&mut self, message: String, level: ActionLevel) { if let Some(action_tx) = &self.action_tx { - let _ = action_tx.send(Action::WalletLog(message, level)); + let _ = action_tx.send( + Action::EventLog(message, level, ActionTarget::WalletLog)); } } diff --git a/src/components/wallet/event_logs.rs b/src/components/wallet/event_logs.rs index f50c324..6569b96 100644 --- a/src/components/wallet/event_logs.rs +++ b/src/components/wallet/event_logs.rs @@ -13,14 +13,14 @@ use ratatui::{ use super::{Component, PartialComponent, CurrentTab}; use crate::{ - types::ActionLevel, action::Action, config::Config, palette::StylePalette, + types::{ActionLevel, ActionTarget}, }; #[derive(Debug, Default)] -struct WalletLog { +struct LogDetails { time: chrono::DateTime, level: ActionLevel, message: String, @@ -31,7 +31,7 @@ pub struct EventLogs { is_active: bool, scroll_state: ScrollbarState, table_state: TableState, - logs: std::collections::VecDeque, + logs: std::collections::VecDeque, palette: StylePalette } @@ -39,7 +39,7 @@ impl EventLogs { const MAX_LOGS: usize = 50; fn add_new_log(&mut self, message: String, level: ActionLevel) { - self.logs.push_front(WalletLog { + self.logs.push_front(LogDetails { time: chrono::Local::now(), level, message, @@ -137,7 +137,8 @@ impl Component for EventLogs { fn update(&mut self, action: Action) -> Result> { match action { - Action::WalletLog(message, level) => self.add_new_log(message, level), + Action::EventLog(message, level, target) if target == ActionTarget::WalletLog => + self.add_new_log(message, level), _ => {} }; Ok(None) diff --git a/src/components/wallet/transfer.rs b/src/components/wallet/transfer.rs index 5e4d5ed..fc6a65e 100644 --- a/src/components/wallet/transfer.rs +++ b/src/components/wallet/transfer.rs @@ -17,7 +17,7 @@ use crate::{ action::Action, config::Config, palette::StylePalette, - types::ActionLevel, + types::{ActionLevel, ActionTarget}, widgets::{Input, InputRequest}, }; @@ -61,7 +61,8 @@ impl Transfer { fn log_event(&mut self, message: String, level: ActionLevel) { if let Some(action_tx) = &self.action_tx { - let _ = action_tx.send(Action::WalletLog(message, level)); + let _ = action_tx.send( + Action::EventLog(message, level, ActionTarget::WalletLog)); } } diff --git a/src/network/mod.rs b/src/network/mod.rs index 974c092..0edaab7 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -17,13 +17,18 @@ mod raw_calls; pub use miscellaneous::{prepare_perbill_fraction_string, calculate_for_fraction}; use crate::{ - types::ActionLevel, + types::{ActionLevel, ActionTarget}, action::Action, casper::CasperConfig, }; pub use subscriptions::{FinalizedSubscription, BestSubscription}; +struct TxToWatch { + tx_progress: TxProgress>, + target: ActionTarget, +} + pub struct Network { action_tx: UnboundedSender, online_client_api: OnlineClient, @@ -32,7 +37,7 @@ pub struct Network { finalized_hash: Option, stash_to_watch: Option<[u8; 32]>, accounts_to_watch: std::collections::HashSet<[u8; 32]>, - transactions_to_watch: Vec>>, + transactions_to_watch: Vec, senders: std::collections::HashMap, } @@ -103,34 +108,35 @@ impl Network { let length = self.transactions_to_watch.len(); for i in (0..length).rev() { let pending_tx = &mut self.transactions_to_watch[i]; - let ext_hash = pending_tx.extrinsic_hash(); - match (*pending_tx).next().await { + let ext_hash = pending_tx.tx_progress.extrinsic_hash(); + let log_target = pending_tx.target.clone(); + match (*pending_tx).tx_progress.next().await { Some(Ok(status)) => { match status { - TxStatus::Validated => self.action_tx.send(Action::WalletLog(format!("transaction {} is part of future queue", ext_hash), ActionLevel::Info))?, - TxStatus::Broadcasted { num_peers } => self.action_tx.send(Action::WalletLog(format!("transaction {} has been broardcasted to {} nodes", ext_hash, num_peers), ActionLevel::Info))?, - TxStatus::NoLongerInBestBlock => self.action_tx.send(Action::WalletLog(format!("transaction {} is no longer in a best block", ext_hash), ActionLevel::Warn))?, - TxStatus::InBestBlock(b) => self.action_tx.send(Action::WalletLog(format!("transaction {} included in the block header {}", b.extrinsic_hash(), b.block_hash()), ActionLevel::Info))?, + TxStatus::Validated => self.action_tx.send(Action::EventLog(format!("transaction {} is part of future queue", ext_hash), ActionLevel::Info, log_target))?, + TxStatus::Broadcasted { num_peers } => self.action_tx.send(Action::EventLog(format!("transaction {} has been broardcasted to {} nodes", ext_hash, num_peers), ActionLevel::Info, log_target))?, + TxStatus::NoLongerInBestBlock => self.action_tx.send(Action::EventLog(format!("transaction {} is no longer in a best block", ext_hash), ActionLevel::Warn, log_target))?, + TxStatus::InBestBlock(b) => self.action_tx.send(Action::EventLog(format!("transaction {} included in the block header {}", b.extrinsic_hash(), b.block_hash()), ActionLevel::Info, log_target))?, TxStatus::InFinalizedBlock(b) => { - self.action_tx.send(Action::WalletLog(format!("transaction {} has been finalized in block header {}", b.extrinsic_hash(), b.block_hash()), ActionLevel::Info))?; + self.action_tx.send(Action::EventLog(format!("transaction {} has been finalized in block header {}", b.extrinsic_hash(), b.block_hash()), ActionLevel::Info, log_target))?; self.transactions_to_watch.remove(i); } TxStatus::Error { message } => { - self.action_tx.send(Action::WalletLog(format!("transaction {} error, something get wrong: {message}", ext_hash), ActionLevel::Error))?; + self.action_tx.send(Action::EventLog(format!("transaction {} error, something get wrong: {message}", ext_hash), ActionLevel::Error, log_target))?; self.transactions_to_watch.remove(i); } TxStatus::Invalid { message } => { - self.action_tx.send(Action::WalletLog(format!("transaction {} invalid: {message}", ext_hash), ActionLevel::Error))?; + self.action_tx.send(Action::EventLog(format!("transaction {} invalid: {message}", ext_hash), ActionLevel::Error, log_target))?; self.transactions_to_watch.remove(i); } TxStatus::Dropped { message } => { - self.action_tx.send(Action::WalletLog(format!("transaction {} was dropped: {message}", ext_hash), ActionLevel::Error))?; + self.action_tx.send(Action::EventLog(format!("transaction {} was dropped: {message}", ext_hash), ActionLevel::Error, log_target))?; self.transactions_to_watch.remove(i); } } }, _ => { - self.action_tx.send(Action::WalletLog(format!("transaction {} was dropped", ext_hash), ActionLevel::Error))?; + self.action_tx.send(Action::EventLog(format!("transaction {} was dropped", ext_hash), ActionLevel::Error, log_target))?; self.transactions_to_watch.remove(i); } } @@ -222,7 +228,10 @@ impl Network { &amount, maybe_nonce, ).await { - self.transactions_to_watch.push(tx_progress); + self.transactions_to_watch.push(TxToWatch { + tx_progress, + target: ActionTarget::WalletLog, + }); } Ok(()) } diff --git a/src/network/predefined_txs.rs b/src/network/predefined_txs.rs index 5b59d97..0a23d17 100644 --- a/src/network/predefined_txs.rs +++ b/src/network/predefined_txs.rs @@ -8,7 +8,7 @@ use tokio::sync::mpsc::UnboundedSender; use crate::{ action::Action, - types::ActionLevel, + types::{ActionLevel, ActionTarget}, casper::{CasperExtrinsicParamsBuilder, CasperConfig}, casper_network, }; @@ -47,14 +47,17 @@ pub async fn transfer_balance( .sign_and_submit_then_watch(&transfer_tx, &signer, tx_params) .await { Ok(tx_progress) => { - action_tx.send(Action::WalletLog( + action_tx.send(Action::EventLog( format!("transfer transaction {} sent", tx_progress.extrinsic_hash()), - ActionLevel::Info))?; + ActionLevel::Info, + ActionTarget::WalletLog))?; Ok(tx_progress) }, Err(err) => { - action_tx.send(Action::WalletLog( - format!("error during transfer: {err}"), ActionLevel::Error))?; + action_tx.send(Action::EventLog( + format!("error during transfer: {err}"), + ActionLevel::Error, + ActionTarget::WalletLog))?; Err(err.into()) } } diff --git a/src/types/log.rs b/src/types/log.rs index 3eca718..1c2d26e 100644 --- a/src/types/log.rs +++ b/src/types/log.rs @@ -8,3 +8,11 @@ pub enum ActionLevel { Warn, Error, } + +#[derive(Default, Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)] +pub enum ActionTarget { + #[default] + WalletLog, + ValidatorLog, + NominatorLog, +} diff --git a/src/types/mod.rs b/src/types/mod.rs index c196a5b..bc65171 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -9,6 +9,7 @@ mod nominator; pub use extrinsics::CasperExtrinsicDetails; pub use era::{EraRewardPoints, EraInfo}; pub use log::ActionLevel; +pub use log::ActionTarget; pub use account::SystemAccount; pub use peer::PeerInformation; pub use session::SessionKeyInfo;