diff --git a/src/action.rs b/src/action.rs index af25bd6..d2f5aaa 100644 --- a/src/action.rs +++ b/src/action.rs @@ -57,6 +57,7 @@ pub enum Action { PayoutStakers([u8; 32], [u8; 32], u32), SetSessionKeys([u8; 32], String), ValidateFrom([u8; 32], u32), + ChillFrom([u8; 32]), EventLog(String, ActionLevel, ActionTarget), NewBestBlock(u32), diff --git a/src/components/validator/chill_popup.rs b/src/components/validator/chill_popup.rs new file mode 100644 index 0000000..3f17d9f --- /dev/null +++ b/src/components/validator/chill_popup.rs @@ -0,0 +1,112 @@ +use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; +use color_eyre::Result; +use ratatui::{ + layout::{Alignment, Constraint, Flex, Layout, Rect}, + widgets::{Block, Clear, Paragraph}, + Frame +}; +use std::sync::mpsc::Sender; + +use super::{Component, PartialComponent, CurrentTab}; +use crate::{ + action::Action, + config::Config, + palette::StylePalette, +}; + +#[derive(Debug)] +pub struct ChillPopup { + is_active: bool, + network_tx: Option>, + secret_seed: [u8; 32], + palette: StylePalette +} + +impl Default for ChillPopup { + fn default() -> Self { + Self::new() + } +} + +impl ChillPopup { + pub fn new() -> Self { + Self { + is_active: false, + secret_seed: [0u8; 32], + network_tx: None, + palette: StylePalette::default(), + } + } + + fn submit_message(&mut self) { + if let Some(network_tx) = &self.network_tx { + let _ = network_tx.send(Action::ChillFrom(self.secret_seed)); + } + } +} + +impl PartialComponent for ChillPopup { + fn set_active(&mut self, current_tab: CurrentTab) { + match current_tab { + CurrentTab::ChillPopup => self.is_active = true, + _ => self.is_active = false, + } + } +} + +impl Component for ChillPopup { + fn register_network_handler(&mut self, tx: Sender) -> Result<()> { + self.network_tx = Some(tx); + Ok(()) + } + + fn register_config_handler(&mut self, config: Config) -> Result<()> { + if let Some(style) = config.styles.get(&crate::app::Mode::Wallet) { + self.palette.with_normal_style(style.get("normal_style").copied()); + self.palette.with_normal_border_style(style.get("normal_border_style").copied()); + self.palette.with_normal_title_style(style.get("normal_title_style").copied()); + self.palette.with_popup_style(style.get("popup_style").copied()); + self.palette.with_popup_title_style(style.get("popup_title_style").copied()); + } + Ok(()) + } + fn handle_key_event(&mut self, key: KeyEvent) -> Result> { + if self.is_active && key.kind == KeyEventKind::Press { + match key.code { + KeyCode::Enter => self.submit_message(), + KeyCode::Esc => self.is_active = false, + _ => {}, + }; + } + Ok(None) + } + + fn update(&mut self, action: Action) -> Result> { + match action { + Action::SetStashSecret(secret_seed) => self.secret_seed = secret_seed, + _ => {} + }; + Ok(None) + } + + fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> { + if self.is_active { + let (border_style, border_type) = self.palette.create_popup_style(); + let input = Paragraph::new(" Do you want to chill stash account and stop validation?") + .block(Block::bordered() + .border_style(border_style) + .border_type(border_type) + .title_style(self.palette.create_popup_title_style()) + .title_alignment(Alignment::Right) + .title(format!("Chill stash"))); + let v = Layout::vertical([Constraint::Max(3)]).flex(Flex::Center); + let h = Layout::horizontal([Constraint::Max(57)]).flex(Flex::Center); + let [area] = v.areas(area); + let [area] = h.areas(area); + + frame.render_widget(Clear, area); + frame.render_widget(input, area); + } + Ok(()) + } +} diff --git a/src/components/validator/mod.rs b/src/components/validator/mod.rs index b936046..8f1139c 100644 --- a/src/components/validator/mod.rs +++ b/src/components/validator/mod.rs @@ -25,6 +25,7 @@ mod bond_popup; mod payout_popup; mod rotate_popup; mod validate_popup; +mod chill_popup; use stash_details::StashDetails; use staking_details::StakingDetails; @@ -40,6 +41,7 @@ use bond_popup::BondPopup; use payout_popup::PayoutPopup; use rotate_popup::RotatePopup; use validate_popup::ValidatePopup; +use chill_popup::ChillPopup; #[derive(Debug, Copy, Clone, PartialEq)] pub enum CurrentTab { @@ -55,6 +57,7 @@ pub enum CurrentTab { PayoutPopup, RotatePopup, ValidatePopup, + ChillPopup, } pub trait PartialComponent: Component { @@ -89,6 +92,7 @@ impl Default for Validator { Box::new(PayoutPopup::default()), Box::new(RotatePopup::default()), Box::new(ValidatePopup::default()), + Box::new(ChillPopup::default()), ], } } @@ -150,6 +154,7 @@ impl Component for Validator { CurrentTab::BondPopup | CurrentTab::RotatePopup | CurrentTab::ValidatePopup | + CurrentTab::ChillPopup | CurrentTab::PayoutPopup => match key.code { KeyCode::Esc => { self.current_tab = self.previous_tab; @@ -198,6 +203,13 @@ impl Component for Validator { component.set_active(self.current_tab.clone()); } }, + KeyCode::Char('C') => { + self.previous_tab = self.current_tab; + self.current_tab = CurrentTab::ChillPopup; + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, KeyCode::Char('B') => { self.previous_tab = self.current_tab; self.current_tab = CurrentTab::BondPopup; diff --git a/src/network/mod.rs b/src/network/mod.rs index 2fbcb49..17fb2ee 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -349,6 +349,23 @@ impl Network { } Ok(()) } + Action::ChillFrom(sender) => { + let sender_str = hex::encode(sender); + let maybe_nonce = self.senders.get_mut(&sender_str); + if let Ok(tx_progress) = predefined_txs::chill( + &self.action_tx, + &self.online_client_api, + &sender, + maybe_nonce, + ).await { + self.transactions_to_watch.push(TxToWatch { + tx_progress, + sender: sender_str, + target: ActionTarget::ValidatorLog, + }); + } + Ok(()) + } _ => Ok(()) } } diff --git a/src/network/predefined_txs.rs b/src/network/predefined_txs.rs index fa761f8..d1009c5 100644 --- a/src/network/predefined_txs.rs +++ b/src/network/predefined_txs.rs @@ -318,3 +318,47 @@ pub async fn validate( } } } + +pub async fn chill( + action_tx: &UnboundedSender, + api: &OnlineClient, + sender: &[u8; 32], + mut maybe_nonce: Option<&mut u32>, +) -> Result>> { + let transfer_tx = casper_network::tx() + .staking() + .chill(); + + let tx_params = match maybe_nonce { + Some(ref mut nonce) => { + **nonce = nonce.saturating_add(1); + CasperExtrinsicParamsBuilder::new() + .nonce(nonce.saturating_sub(1) as u64) + .build() + }, + None => CasperExtrinsicParamsBuilder::new().build(), + }; + + let pair = Pair::from_seed(sender); + let signer = PairSigner::::new(pair); + + match api + .tx() + .sign_and_submit_then_watch(&transfer_tx, &signer, tx_params) + .await { + Ok(tx_progress) => { + action_tx.send(Action::EventLog( + format!("chill {} sent", tx_progress.extrinsic_hash()), + ActionLevel::Info, + ActionTarget::ValidatorLog))?; + Ok(tx_progress) + }, + Err(err) => { + action_tx.send(Action::EventLog( + format!("error during chill: {err}"), + ActionLevel::Error, + ActionTarget::ValidatorLog))?; + Err(err.into()) + } + } +}