dynamic balances for the chosen wallet
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
parent
dc20ba936a
commit
5bfd4e678a
@ -32,6 +32,7 @@ pub enum Action {
|
||||
|
||||
BalanceRequest([u8; 32], bool),
|
||||
BalanceResponse([u8; 32], SystemAccount),
|
||||
BalanceSetActive(Option<SystemAccount>),
|
||||
|
||||
RenameAccount(String),
|
||||
RenameAddressBookRecord(String),
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
|
@ -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<u128>,
|
||||
locked_balance: Option<u128>,
|
||||
bonded_balance: Option<u128>,
|
||||
nonce: Option<u32>,
|
||||
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<u128>, 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<Option<Action>> {
|
||||
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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user