make logging in a function

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2024-12-06 17:30:10 +03:00
parent 6255703bbb
commit ac14c36760
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
2 changed files with 65 additions and 88 deletions

View File

@ -75,6 +75,12 @@ 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));
}
}
fn send_balance_request(&mut self, account_id: [u8; 32], remove: bool) {
if let Some(action_tx) = &self.network_tx {
let _ = action_tx.send(Action::BalanceRequest(account_id, remove));
@ -89,6 +95,16 @@ impl Accounts {
}
}
fn update_account_name(&mut self) {
if let Some(index) = self.table_state.selected() {
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::RenameAccount(
self.wallet_keys[index].name.clone()
));
}
}
}
fn create_new_account(&mut self, name: String) {
let (pair, seed) = Pair::generate();
let secret_seed = hex::encode(seed);
@ -97,12 +113,10 @@ impl Accounts {
let address = AccountId32::from(seed.clone())
.to_ss58check_with_version(Ss58AddressFormat::custom(1996));
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
format!("new wallet '{}' with public address {} created", &name, &address),
ActionLevel::Info,
));
}
self.log_event(
format!("new wallet '{}' with public address {} created",
&name, &address),
ActionLevel::Info);
self.send_balance_request(account_id, false);
self.wallet_keys.push(AccountInfo {
@ -120,29 +134,15 @@ impl Accounts {
if let Some(index) = self.table_state.selected() {
let old_name = self.wallet_keys[index].name.clone();
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
format!("wallet '{}' renamed to {}", &new_name, &old_name),
ActionLevel::Info,
));
}
self.log_event(format!("wallet '{}' renamed to {}",
&new_name, &old_name),
ActionLevel::Info);
self.wallet_keys[index].name = new_name;
self.save_to_file();
}
}
fn update_account_name(&mut self) {
if let Some(index) = self.table_state.selected() {
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::RenameAccount(
self.wallet_keys[index].name.clone()
));
}
}
}
fn swap_up(&mut self) {
if let Some(src_index) = self.table_state.selected() {
let dst_index = src_index.saturating_sub(1);
@ -173,13 +173,9 @@ impl Accounts {
self.previous_row();
self.save_to_file();
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
format!("wallet `{}` with public address {} removed",
self.log_event(format!("wallet `{}` with public address {} removed",
&wallet.name, &wallet.address),
ActionLevel::Warn,
));
}
ActionLevel::Warn);
}
}
}
@ -223,12 +219,9 @@ impl Accounts {
pair_signer,
});
}
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
format!("read {} wallets from disk", self.wallet_keys.len()),
ActionLevel::Info,
));
}
self.log_event(format!("read {} wallets from disk",
self.wallet_keys.len()),
ActionLevel::Info);
},
Err(_) => {
let (pair, seed) = match std::fs::read_to_string("/etc/ghost/wallet-key") {
@ -241,23 +234,17 @@ impl Accounts {
.try_into()
.expect("stored seed is valid length; qed");
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
"wallet read from the `/etc/ghost/wallet-key`".to_string(),
ActionLevel::Warn,
));
}
ActionLevel::Warn);
let pair = Pair::from_seed(&seed);
(pair, seed)
}
Err(_) => {
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
"no wallets found on disk, new wallet created".to_string(),
ActionLevel::Warn,
));
}
ActionLevel::Warn);
let (pair, seed) = Pair::generate();
(pair, seed)
}

View File

@ -75,8 +75,14 @@ impl AddressBook {
}
fn send_balance_request(&mut self, account_id: [u8; 32], remove: bool) {
if let Some(action_tx) = &self.network_tx {
let _ = action_tx.send(Action::BalanceRequest(account_id, remove));
if let Some(network_tx) = &self.network_tx {
let _ = network_tx.send(Action::BalanceRequest(account_id, remove));
}
}
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));
}
}
@ -108,12 +114,9 @@ impl AddressBook {
});
self.send_balance_request(account_id, false);
}
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
format!("read {} records from address book", self.address_book.len()),
ActionLevel::Info,
));
}
ActionLevel::Info)
},
Err(_) => {
let chad_boyz = vec![
@ -151,12 +154,9 @@ impl AddressBook {
});
self.send_balance_request(chad_account_id, false);
});
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
format!("address book is empty, filling it with giga chad boyz as default"),
ActionLevel::Warn,
));
}
ActionLevel::Warn)
}
};
self.scroll_state = self.scroll_state.content_length(self.address_book.len());
@ -166,12 +166,9 @@ impl AddressBook {
fn rename_record(&mut self, new_name: String) {
if let Some(index) = self.table_state.selected() {
let old_name = self.address_book[index].name.clone();
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
format!("record renamed from {} to {}", &old_name, &new_name),
ActionLevel::Info,
));
}
ActionLevel::Info);
self.address_book[index].name = new_name;
self.save_to_file();
}
@ -181,20 +178,14 @@ impl AddressBook {
match AccountId32::from_ss58check_with_version(&address) {
Ok((account_id, format)) => {
if format != Ss58AddressFormat::custom(1996) {
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
format!("provided public address for {} is not part of Casper/Ghost ecosystem", &address),
ActionLevel::Error,
));
}
ActionLevel::Error);
}
if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
self.log_event(
format!("account {} with address {} added to address book", &name, &address),
ActionLevel::Info,
));
}
ActionLevel::Info);
let seed_vec = account_id.to_raw_vec();
let mut account_id = [0u8; 32];
@ -211,16 +202,12 @@ impl AddressBook {
self.last_row();
self.send_balance_request(account_id, false);
},
Err(_) => if let Some(action_tx) = &self.action_tx {
let _ = action_tx.send(Action::WalletLog(
Err(_) => self.log_event(
format!("provided account address {} is invalid", &address),
ActionLevel::Error,
));
ActionLevel::Error),
}
}
}
fn update_address_book_record(&mut self) {
if let Some(index) = self.table_state.selected() {
if let Some(action_tx) = &self.action_tx {
@ -255,9 +242,12 @@ impl AddressBook {
fn delete_row(&mut self) {
if let Some(index) = self.table_state.selected() {
let _ = self.address_book.remove(index);
let record = self.address_book.remove(index);
self.previous_row();
self.save_to_file();
self.log_event(format!("record `{}` with public address {} removed",
&record.name, &record.address),
ActionLevel::Warn);
}
}