179 lines
6.4 KiB
Rust
179 lines
6.4 KiB
Rust
use color_eyre::Result;
|
|
use ratatui::{
|
|
text::Text,
|
|
layout::{Alignment, Constraint, Rect},
|
|
widgets::{Block, Cell, Row, Table},
|
|
Frame
|
|
};
|
|
use subxt::ext::sp_core::crypto::{Ss58Codec, Ss58AddressFormat, AccountId32};
|
|
use std::sync::mpsc::Sender;
|
|
|
|
use super::{Component, PartialComponent, CurrentTab};
|
|
use crate::{
|
|
action::Action, config::Config, palette::StylePalette,
|
|
types::RewardDestination, widgets::DotSpinner,
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
pub struct StakingLedger {
|
|
is_active: bool,
|
|
is_bonded: bool,
|
|
account_id: [u8; 32],
|
|
network_tx: Option<Sender<Action>>,
|
|
total_staked: Option<u128>,
|
|
active_staked: Option<u128>,
|
|
reward_destination: RewardDestination,
|
|
palette: StylePalette
|
|
}
|
|
|
|
impl Default for StakingLedger {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl StakingLedger {
|
|
const TICKER: &str = " CSPR";
|
|
const DECIMALS: usize = 6;
|
|
|
|
pub fn new() -> Self {
|
|
Self {
|
|
is_active: false,
|
|
is_bonded: false,
|
|
account_id: [0u8; 32],
|
|
network_tx: None,
|
|
total_staked: None,
|
|
active_staked: None,
|
|
reward_destination: Default::default(),
|
|
palette: StylePalette::default(),
|
|
}
|
|
}
|
|
|
|
fn set_used_account_id(&mut self, account_id: [u8; 32]) {
|
|
self.account_id = account_id;
|
|
if let Some(network_tx) = &self.network_tx {
|
|
let _ = network_tx.send(Action::GetValidatorLedger(account_id, false));
|
|
let _ = network_tx.send(Action::GetStakingPayee(account_id, false));
|
|
let _ = network_tx.send(Action::GetIsStashBonded(account_id, false));
|
|
}
|
|
}
|
|
|
|
fn prepare_u128(&self, maybe_value: Option<u128>) -> String {
|
|
match maybe_value {
|
|
Some(value) => {
|
|
let value = value as f64 / 10f64.powi(18);
|
|
let after = Self::DECIMALS;
|
|
format!("{:.after$}{}", value, Self::TICKER)
|
|
},
|
|
None => format!("{}{}", DotSpinner::default().to_string(), Self::TICKER)
|
|
}
|
|
}
|
|
|
|
fn get_reward_destination(&self) -> String {
|
|
match self.reward_destination {
|
|
RewardDestination::Staked => "re-stake".to_string(),
|
|
RewardDestination::Stash => "stake".to_string(),
|
|
RewardDestination::Controller => "controller".to_string(),
|
|
RewardDestination::None => "none".to_string(),
|
|
RewardDestination::Account(account_id) => {
|
|
let address = AccountId32::from(account_id)
|
|
.to_ss58check_with_version(Ss58AddressFormat::custom(1996));
|
|
let tail = address.len().saturating_sub(5);
|
|
format!("{}..{}", &address[..5], &address[tail..])
|
|
},
|
|
}
|
|
}
|
|
|
|
fn is_bonded_to_string(&self) -> String {
|
|
if self.is_bonded {
|
|
"bonded".to_string()
|
|
} else {
|
|
"no bond".to_string()
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PartialComponent for StakingLedger {
|
|
fn set_active(&mut self, current_tab: CurrentTab) {
|
|
match current_tab {
|
|
CurrentTab::Accounts => self.is_active = true,
|
|
_ => self.is_active = false,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Component for StakingLedger {
|
|
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());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
|
match action {
|
|
Action::UsedAccount(account_id, _) => self.set_used_account_id(account_id),
|
|
Action::SetIsBonded(is_bonded, account_id) if self.account_id == account_id =>
|
|
self.is_bonded = is_bonded,
|
|
Action::SetStakingPayee(reward_destination, account_id) if self.account_id == account_id =>
|
|
self.reward_destination = reward_destination,
|
|
Action::SetStakedAmountRatio(total, active, account_id) if self.account_id == account_id => {
|
|
self.total_staked = total;
|
|
self.active_staked = active;
|
|
},
|
|
_ => {}
|
|
};
|
|
Ok(None)
|
|
}
|
|
|
|
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
|
|
let [_, _, _, place] = super::account_layout(area);
|
|
let (border_style, border_type) = self.palette
|
|
.create_border_style(self.is_active);
|
|
|
|
let table = Table::new(
|
|
[
|
|
Row::new(vec![
|
|
Cell::from(Text::from("bond ready:".to_string()).alignment(Alignment::Left)),
|
|
Cell::from(Text::from(self.is_bonded_to_string()).alignment(Alignment::Right)),
|
|
]),
|
|
Row::new(vec![
|
|
Cell::from(Text::from("destination:".to_string()).alignment(Alignment::Left)),
|
|
Cell::from(Text::from(self.get_reward_destination()).alignment(Alignment::Right)),
|
|
]),
|
|
Row::new(vec![
|
|
Cell::from(Text::from("total:".to_string()).alignment(Alignment::Left)),
|
|
Cell::from(Text::from(self.prepare_u128(self.total_staked)).alignment(Alignment::Right))
|
|
]),
|
|
Row::new(vec![
|
|
Cell::from(Text::from("active:".to_string()).alignment(Alignment::Left)),
|
|
Cell::from(Text::from(self.prepare_u128(self.active_staked)).alignment(Alignment::Right)),
|
|
]),
|
|
],
|
|
[
|
|
Constraint::Max(12),
|
|
Constraint::Min(14),
|
|
]
|
|
)
|
|
.block(Block::bordered()
|
|
.border_style(border_style)
|
|
.border_type(border_type)
|
|
.title_alignment(Alignment::Right)
|
|
.title_style(self.palette.create_title_style(false))
|
|
.title("Nomination stake"));
|
|
|
|
frame.render_widget(table, place);
|
|
Ok(())
|
|
}
|
|
}
|