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

View File

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