ghost-eye/src/components/wallet/current_validator_details.rs
Uncle Stinky 987d8e544d
pop-up overlay issue fixed on the nominator table
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-04-24 19:42:09 +03:00

226 lines
9.1 KiB
Rust

use std::sync::mpsc::Sender;
use color_eyre::Result;
use ratatui::{
widgets::Clear,
layout::{Alignment, Constraint, Rect},
text::Text,
widgets::{Block, Cell, Row, Table},
Frame
};
use super::{Component, PartialComponent, CurrentTab};
use crate::{action::Action, config::Config, palette::StylePalette};
#[derive(Debug)]
pub struct CurrentValidatorDetails {
network_tx: Option<Sender<Action>>,
is_active: bool,
palette: StylePalette,
choosen: [u8; 32],
total_balance: u128,
own_balance: u128,
active_stake: u128,
others_len: usize,
commission: f64,
points_ratio: f64,
latest_era_claim: u32,
is_active_validator: bool,
is_nomination_disabled: bool,
}
impl Default for CurrentValidatorDetails {
fn default() -> Self {
Self::new()
}
}
impl CurrentValidatorDetails {
const TICKER: &str = " CSPR";
const DECIMALS: usize = 5;
pub fn new() -> Self {
CurrentValidatorDetails {
network_tx: None,
is_active: false,
choosen: [0u8; 32],
total_balance: 0,
own_balance: 0,
active_stake: 0,
others_len: 0,
commission: 0.0,
points_ratio: 0.0,
palette: Default::default(),
latest_era_claim: 0,
is_active_validator: false,
is_nomination_disabled: false,
}
}
fn prepare_u128(&self, value: u128) -> String {
let value = value as f64 / 10f64.powi(18);
let after = Self::DECIMALS;
format!("{:.after$}{}", value, Self::TICKER)
}
fn prepare_stake_imbalance(&self) -> String {
let (value, prefix) = if self.own_balance <= self.active_stake {
(self.active_stake.saturating_sub(self.own_balance), "+")
} else {
(self.own_balance.saturating_sub(self.active_stake), "-")
};
format!("{}{}", prefix, self.prepare_u128(value))
}
fn prepare_state_string(&self) -> String {
if self.is_active_validator {
"active".to_string()
} else {
"chilling".to_string()
}
}
fn update_commission(&mut self, maybe_commission: Option<u32>, is_disabled: bool) {
self.is_nomination_disabled = is_disabled;
match maybe_commission {
Some(commission) => {
self.commission = commission as f64 / 10_000_000.0;
self.is_active_validator = true;
},
None => {
self.commission = 0.0;
self.is_active_validator = false;
},
}
}
fn update_choosen_validator(&mut self, account_id: [u8; 32], individual: u32, total: u32) {
self.points_ratio = match total {
0 => 0.0,
_ => (individual as f64 / total as f64) * 100.0,
};
if self.choosen != account_id {
self.choosen = account_id;
if let Some(network_tx) = &self.network_tx {
let _ = network_tx.send(Action::GetErasStakersOverview(account_id, false));
let _ = network_tx.send(Action::GetNominatorsByValidator(account_id, false));
let _ = network_tx.send(Action::GetValidatorPrefs(account_id, false));
let _ = network_tx.send(Action::GetValidatorLedger(account_id, false));
let _ = network_tx.send(Action::GetValidatorLatestClaim(account_id, false));
}
}
}
}
impl PartialComponent for CurrentValidatorDetails {
fn set_active(&mut self, current_tab: CurrentTab) {
match current_tab {
CurrentTab::CurrentValidatorsPopup => self.is_active = true,
_ => self.is_active = false,
}
}
}
impl Component for CurrentValidatorDetails {
fn register_network_handler(&mut self, tx: Sender<Action>) -> 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_hover_style(style.get("hover_style").copied());
self.palette.with_normal_border_style(style.get("normal_border_style").copied());
self.palette.with_hover_border_style(style.get("hover_border_style").copied());
self.palette.with_normal_title_style(style.get("normal_title_style").copied());
self.palette.with_hover_title_style(style.get("hover_title_style").copied());
self.palette.with_highlight_style(style.get("highlight_style").copied());
self.palette.with_scrollbar_style(style.get("scrollbar_style").copied());
}
Ok(())
}
fn update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::SetChoosenValidator(account_id, individual, total) => self.update_choosen_validator(account_id, individual, total),
Action::SetValidatorPrefs(commission, is_disabled, account_id) if self.choosen == account_id => self.update_commission(commission, is_disabled),
Action::SetNominatorsByValidator(noms, account_id) if self.choosen == account_id => self.others_len = noms.len(),
Action::SetStakedAmountRatio(_, active_stake, account_id) if self.choosen == account_id => self.active_stake = active_stake.unwrap_or_default(),
Action::SetValidatorLatestClaim(era_index, account_id) if self.choosen == account_id => self.latest_era_claim = era_index,
Action::SetStakedRatio(total, own, account_id) if self.choosen == account_id => {
self.total_balance = total;
self.own_balance = own;
},
_ => {}
};
Ok(None)
}
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
if self.is_active {
let (border_style, border_type) = self.palette.create_border_style(self.is_active);
let [_, place] = super::nominator_layout(area);
let table = Table::new(
vec![
Row::new(vec![
Cell::from(Text::from("Forbidden".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.is_nomination_disabled.to_string()).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Nominators".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.others_len.to_string()).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Total staked".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_u128(self.total_balance)).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Own stake".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_u128(self.own_balance)).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Imbalance".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_stake_imbalance()).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Commission".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(format!("{:.4}%", self.commission)).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Points ratio".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(format!("{:.4}%", self.points_ratio)).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("In next era".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_state_string()).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("Last payout".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(format!("{} days ago", self.latest_era_claim)).alignment(Alignment::Right)),
]),
],
[
Constraint::Max(12),
Constraint::Min(0),
],
)
.column_spacing(1)
.highlight_style(self.palette.create_highlight_style())
.block(Block::bordered()
.border_style(border_style)
.border_type(border_type)
.title_alignment(Alignment::Right)
.title_style(self.palette.create_title_style(false))
.title("Validator details"));
frame.render_widget(Clear, place);
frame.render_widget(table, place);
}
Ok(())
}
}