diff --git a/src/commands/account.rs b/src/commands/account.rs index 1d0c452a65295d6052c0d1855140779cbddb1bf9..05eb7a68de4795b7bcf15af625862f5d99f9627a 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -72,7 +72,7 @@ struct BalanceView { pub async fn get_balance(data: Data) -> Result<(), GcliError> { let account_id = data.address(); let account_info = get_account_info(data.client(), &account_id).await?; - + match data.args.output_format { OutputFormat::Human => { if let Some(account_info) = account_info { @@ -88,13 +88,15 @@ pub async fn get_balance(data: Data) -> Result<(), GcliError> { let view = BalanceView { account_id: account_id.to_string(), free_balance: account_info.as_ref().map_or(0, |info| info.data.free), - formatted_balance: account_info.as_ref().map_or("0".to_string(), |info| data.format_balance(info.data.free)), + formatted_balance: account_info + .as_ref() + .map_or("0".to_string(), |info| data.format_balance(info.data.free)), exists: account_info.is_some(), }; println!("{}", serde_json::to_string(&view).map_err(|e| anyhow!(e))?); } } - + Ok(()) } diff --git a/src/commands/blockchain.rs b/src/commands/blockchain.rs index 0062071c52b9f9163d9e6c1af56675bc75fb3240..958f1dbdeb72ed453a9309f9e69132f1f92bc383 100644 --- a/src/commands/blockchain.rs +++ b/src/commands/blockchain.rs @@ -51,7 +51,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE Subcommand::CurrentBlock => { let finalized_number = fetch_finalized_number(&data).await?; let current_number = fetch_latest_number_and_hash(&data).await?.0; - + match data.args.output_format { OutputFormat::Human => { println!("on {}", data.cfg.duniter_endpoint); diff --git a/src/commands/oneshot.rs b/src/commands/oneshot.rs index 086fc9c370181c0962e77d38651c267a7f9e15e1..399be868fee70c970c35d3fd6d105b6464bceab8 100644 --- a/src/commands/oneshot.rs +++ b/src/commands/oneshot.rs @@ -76,7 +76,8 @@ struct OneshotBalanceView { /// get balance of oneshot account pub async fn oneshot_account_balance(data: &Data) -> Result<(), GcliError> { let account_id = data.address(); - let balance = data.client() + let balance = data + .client() .storage() .at_latest() .await? @@ -87,14 +88,10 @@ pub async fn oneshot_account_balance(data: &Data) -> Result<(), GcliError> { ) .await? .unwrap_or(0); - + match data.args.output_format { OutputFormat::Human => { - println!( - "balance of oneshot account {} is: {}", - account_id, - balance - ); + println!("balance of oneshot account {} is: {}", account_id, balance); } OutputFormat::Json => { let view = OneshotBalanceView { diff --git a/src/commands/runtime.rs b/src/commands/runtime.rs index 4045a109c4e99dee447895d76a440bead2f33375..87db078d786be34cddaf5d4325af049586634af7 100644 --- a/src/commands/runtime.rs +++ b/src/commands/runtime.rs @@ -168,60 +168,150 @@ pub async fn runtime_info(data: Data) -> Result<(), GcliError> { OutputFormat::Json => { // Create HashMaps for each section let mut identity = HashMap::new(); - identity.insert("confirm_period".to_string(), getu32(consts.identity().confirm_period())); - identity.insert("validation_period".to_string(), getu32(consts.identity().validation_period())); - identity.insert("autorevocation_period".to_string(), getu32(consts.identity().autorevocation_period())); - identity.insert("deletion_period".to_string(), getu32(consts.identity().deletion_period())); - identity.insert("change_owner_key_period".to_string(), getu32(consts.identity().change_owner_key_period())); - identity.insert("idty_creation_period".to_string(), getu32(consts.identity().idty_creation_period())); - + identity.insert( + "confirm_period".to_string(), + getu32(consts.identity().confirm_period()), + ); + identity.insert( + "validation_period".to_string(), + getu32(consts.identity().validation_period()), + ); + identity.insert( + "autorevocation_period".to_string(), + getu32(consts.identity().autorevocation_period()), + ); + identity.insert( + "deletion_period".to_string(), + getu32(consts.identity().deletion_period()), + ); + identity.insert( + "change_owner_key_period".to_string(), + getu32(consts.identity().change_owner_key_period()), + ); + identity.insert( + "idty_creation_period".to_string(), + getu32(consts.identity().idty_creation_period()), + ); + let mut certification = HashMap::new(); - certification.insert("cert_period".to_string(), getu32(consts.certification().cert_period())); - certification.insert("max_by_issuer".to_string(), getu32(consts.certification().max_by_issuer())); - certification.insert("min_received_cert_to_be_able_to_issue_cert".to_string(), - getu32(consts.certification().min_received_cert_to_be_able_to_issue_cert())); - certification.insert("validity_period".to_string(), getu32(consts.certification().validity_period())); - + certification.insert( + "cert_period".to_string(), + getu32(consts.certification().cert_period()), + ); + certification.insert( + "max_by_issuer".to_string(), + getu32(consts.certification().max_by_issuer()), + ); + certification.insert( + "min_received_cert_to_be_able_to_issue_cert".to_string(), + getu32( + consts + .certification() + .min_received_cert_to_be_able_to_issue_cert(), + ), + ); + certification.insert( + "validity_period".to_string(), + getu32(consts.certification().validity_period()), + ); + let mut wot = HashMap::new(); - wot.insert("first_issuable_on".to_string(), getu32(consts.wot().first_issuable_on())); - wot.insert("min_cert_for_membership".to_string(), getu32(consts.wot().min_cert_for_membership())); - wot.insert("min_cert_for_create_idty_right".to_string(), getu32(consts.wot().min_cert_for_create_idty_right())); - + wot.insert( + "first_issuable_on".to_string(), + getu32(consts.wot().first_issuable_on()), + ); + wot.insert( + "min_cert_for_membership".to_string(), + getu32(consts.wot().min_cert_for_membership()), + ); + wot.insert( + "min_cert_for_create_idty_right".to_string(), + getu32(consts.wot().min_cert_for_create_idty_right()), + ); + let mut membership = HashMap::new(); - membership.insert("membership_period".to_string(), getu32(consts.membership().membership_period())); - + membership.insert( + "membership_period".to_string(), + getu32(consts.membership().membership_period()), + ); + let mut smith_members = HashMap::new(); - smith_members.insert("max_by_issuer".to_string(), getu32(consts.smith_members().max_by_issuer())); - smith_members.insert("min_cert_for_membership".to_string(), getu32(consts.smith_members().min_cert_for_membership())); - smith_members.insert("smith_inactivity_max_duration".to_string(), getu32(consts.smith_members().smith_inactivity_max_duration())); - + smith_members.insert( + "max_by_issuer".to_string(), + getu32(consts.smith_members().max_by_issuer()), + ); + smith_members.insert( + "min_cert_for_membership".to_string(), + getu32(consts.smith_members().min_cert_for_membership()), + ); + smith_members.insert( + "smith_inactivity_max_duration".to_string(), + getu32(consts.smith_members().smith_inactivity_max_duration()), + ); + let mut distance = HashMap::new(); - distance.insert("max_referee_distance".to_string(), - serde_json::to_value(getu32(consts.distance().max_referee_distance())).map_err(|e| anyhow!(e))?); - distance.insert("min_accessible_referees".to_string(), - serde_json::to_value(format!("{:?}", getp(consts.distance().min_accessible_referees()))).map_err(|e| anyhow!(e))?); - distance.insert("evaluation_price".to_string(), - serde_json::to_value(getf(consts.distance().evaluation_price())).map_err(|e| anyhow!(e))?); - + distance.insert( + "max_referee_distance".to_string(), + serde_json::to_value(getu32(consts.distance().max_referee_distance())) + .map_err(|e| anyhow!(e))?, + ); + distance.insert( + "min_accessible_referees".to_string(), + serde_json::to_value(format!( + "{:?}", + getp(consts.distance().min_accessible_referees()) + )) + .map_err(|e| anyhow!(e))?, + ); + distance.insert( + "evaluation_price".to_string(), + serde_json::to_value(getf(consts.distance().evaluation_price())) + .map_err(|e| anyhow!(e))?, + ); + let mut currency = HashMap::new(); - currency.insert("existential_deposit".to_string(), getf(consts.balances().existential_deposit())); - + currency.insert( + "existential_deposit".to_string(), + getf(consts.balances().existential_deposit()), + ); + let mut provide_randomness = HashMap::new(); - provide_randomness.insert("max_requests".to_string(), - serde_json::to_value(getu32(consts.provide_randomness().max_requests())).map_err(|e| anyhow!(e))?); - provide_randomness.insert("request_price".to_string(), - serde_json::to_value(getf(consts.provide_randomness().request_price())).map_err(|e| anyhow!(e))?); - + provide_randomness.insert( + "max_requests".to_string(), + serde_json::to_value(getu32(consts.provide_randomness().max_requests())) + .map_err(|e| anyhow!(e))?, + ); + provide_randomness.insert( + "request_price".to_string(), + serde_json::to_value(getf(consts.provide_randomness().request_price())) + .map_err(|e| anyhow!(e))?, + ); + let mut universal_dividend = HashMap::new(); - universal_dividend.insert("max_past_reeval".to_string(), - serde_json::to_value(getu32(consts.universal_dividend().max_past_reeval())).map_err(|e| anyhow!(e))?); - universal_dividend.insert("square_money_growth_rate".to_string(), - serde_json::to_value(format!("{:?}", getp(consts.universal_dividend().square_money_growth_rate()))).map_err(|e| anyhow!(e))?); - universal_dividend.insert("ud_creation_period".to_string(), - serde_json::to_value(getu64(consts.universal_dividend().ud_creation_period())).map_err(|e| anyhow!(e))?); - universal_dividend.insert("ud_reeval_period".to_string(), - serde_json::to_value(getu64(consts.universal_dividend().ud_reeval_period())).map_err(|e| anyhow!(e))?); - + universal_dividend.insert( + "max_past_reeval".to_string(), + serde_json::to_value(getu32(consts.universal_dividend().max_past_reeval())) + .map_err(|e| anyhow!(e))?, + ); + universal_dividend.insert( + "square_money_growth_rate".to_string(), + serde_json::to_value(format!( + "{:?}", + getp(consts.universal_dividend().square_money_growth_rate()) + )) + .map_err(|e| anyhow!(e))?, + ); + universal_dividend.insert( + "ud_creation_period".to_string(), + serde_json::to_value(getu64(consts.universal_dividend().ud_creation_period())) + .map_err(|e| anyhow!(e))?, + ); + universal_dividend.insert( + "ud_reeval_period".to_string(), + serde_json::to_value(getu64(consts.universal_dividend().ud_reeval_period())) + .map_err(|e| anyhow!(e))?, + ); + // Create the view let view = RuntimeInfoView { identity, @@ -234,10 +324,13 @@ pub async fn runtime_info(data: Data) -> Result<(), GcliError> { provide_randomness, universal_dividend, }; - - println!("{}", serde_json::to_string_pretty(&view).map_err(|e| anyhow!(e))?); + + println!( + "{}", + serde_json::to_string_pretty(&view).map_err(|e| anyhow!(e))? + ); } } - + Ok(()) } diff --git a/src/commands/smith.rs b/src/commands/smith.rs index 4e6789d03a90742571f5aac10fa562e738345f60..ba35848b2d851f89a573caade2d116cdf453b44f 100644 --- a/src/commands/smith.rs +++ b/src/commands/smith.rs @@ -1,12 +1,12 @@ use crate::*; +use anyhow::anyhow; use commands::identity::try_get_idty_index_by_name; #[cfg(feature = "gdev")] use runtime::runtime_types::gdev_runtime::opaque::SessionKeys as RuntimeSessionKeys; +use serde::Serialize; use std::collections::HashMap; use std::ops::Deref; -use anyhow::anyhow; -use serde::Serialize; type SessionKeys = [u8; 128]; @@ -302,15 +302,16 @@ pub async fn online(data: &Data) -> Result<(), GcliError> { let mut online_view = Vec::new(); let mut incoming_view = Vec::new(); let mut outgoing_view = Vec::new(); - + let names = if let Some(indexer) = &data.indexer { let mut names_map = HashMap::<IdtyId, String>::new(); let all_authorities = [ online_authorities.clone(), incoming_authorities.clone(), outgoing_authorities.clone(), - ].concat(); - + ] + .concat(); + indexer .names_by_indexes(&all_authorities) .await @@ -318,39 +319,39 @@ pub async fn online(data: &Data) -> Result<(), GcliError> { .for_each(|e| { names_map.insert(e.0, e.1); }); - + names_map } else { HashMap::new() }; - + for id in online_authorities { online_view.push(SmithAuthorityView { id, name: names.get(&id).cloned(), }); } - + for id in incoming_authorities { incoming_view.push(SmithAuthorityView { id, name: names.get(&id).cloned(), }); } - + for id in outgoing_authorities { outgoing_view.push(SmithAuthorityView { id, name: names.get(&id).cloned(), }); } - + let view = SmithAuthoritiesView { online: online_view, incoming: incoming_view, outgoing: outgoing_view, }; - + println!("{}", serde_json::to_string(&view).map_err(|e| anyhow!(e))?); } } diff --git a/src/commands/vault.rs b/src/commands/vault.rs index ddce4a9219ad91c2469d55913f8133ac9919181b..a817acbca92c95100a1d38fc39d43b2ae6f93afe 100644 --- a/src/commands/vault.rs +++ b/src/commands/vault.rs @@ -225,15 +225,21 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE match data.args.output_format { OutputFormat::Human => { let table = display::compute_vault_accounts_table_with_g1v1( - &all_account_tree_node_hierarchies, - show_g1v1, - )?; + &all_account_tree_node_hierarchies, + show_g1v1, + )?; println!("available SS58 Addresses:"); println!("{table}"); } OutputFormat::Json => { - let json_view = display::compute_vault_accounts_json(&all_account_tree_node_hierarchies, show_g1v1); - println!("{}", serde_json::to_string(&json_view).map_err(|e| anyhow!(e))?); + let json_view = display::compute_vault_accounts_json( + &all_account_tree_node_hierarchies, + show_g1v1, + ); + println!( + "{}", + serde_json::to_string(&json_view).map_err(|e| anyhow!(e))? + ); } } } @@ -244,15 +250,21 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE match data.args.output_format { OutputFormat::Human => { let table = display::compute_vault_accounts_table_with_g1v1( - &base_account_tree_nodes, - show_g1v1, - )?; + &base_account_tree_nodes, + show_g1v1, + )?; println!("available <Base> SS58 Addresses:"); println!("{table}"); } OutputFormat::Json => { - let json_view = display::compute_vault_accounts_json(&base_account_tree_nodes, show_g1v1); - println!("{}", serde_json::to_string(&json_view).map_err(|e| anyhow!(e))?); + let json_view = display::compute_vault_accounts_json( + &base_account_tree_nodes, + show_g1v1, + ); + println!( + "{}", + serde_json::to_string(&json_view).map_err(|e| anyhow!(e))? + ); } } } @@ -269,9 +281,9 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE match data.args.output_format { OutputFormat::Human => { let table = display::compute_vault_accounts_table_with_g1v1( - &[base_account_tree_node.clone()], - show_g1v1, - )?; + &[base_account_tree_node.clone()], + show_g1v1, + )?; println!( "available SS58 Addresses linked to {}:", account_tree_node.borrow().account @@ -279,8 +291,14 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE println!("{table}"); } OutputFormat::Json => { - let json_view = display::compute_vault_accounts_json(&[base_account_tree_node], show_g1v1); - println!("{}", serde_json::to_string(&json_view).map_err(|e| anyhow!(e))?); + let json_view = display::compute_vault_accounts_json( + &[base_account_tree_node], + show_g1v1, + ); + println!( + "{}", + serde_json::to_string(&json_view).map_err(|e| anyhow!(e))? + ); } } } @@ -295,7 +313,10 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE println!("{table}"); } OutputFormat::Json => { - println!("{}", serde_json::to_string(&vault_key_addresses).map_err(|e| anyhow!(e))?); + println!( + "{}", + serde_json::to_string(&vault_key_addresses).map_err(|e| anyhow!(e))? + ); } } } @@ -313,7 +334,10 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE //FIXME Probably not the display we would expect "address": account.to_string() }); - println!("{}", serde_json::to_string(&json_view).map_err(|e| anyhow!(e))?); + println!( + "{}", + serde_json::to_string(&json_view).map_err(|e| anyhow!(e))? + ); } } @@ -612,7 +636,10 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE let json_view = serde_json::json!({ "substrate_uri": account_to_derive_secret_suri }); - println!("{}", serde_json::to_string(&json_view).map_err(|e| anyhow!(e))?); + println!( + "{}", + serde_json::to_string(&json_view).map_err(|e| anyhow!(e))? + ); } } } @@ -693,7 +720,10 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE let json_view = serde_json::json!({ "data_dir": data_dir }); - println!("{}", serde_json::to_string(&json_view).map_err(|e| anyhow!(e))?); + println!( + "{}", + serde_json::to_string(&json_view).map_err(|e| anyhow!(e))? + ); } } } diff --git a/src/conf.rs b/src/conf.rs index 09c83f4cd2f4919c8e094ddf8bbfeffdbf66c29d..1577faddf9720c5b6379395220f0562197dca88a 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,8 +1,8 @@ use crate::entities::vault_account; use crate::entities::vault_account::DbAccountId; use crate::*; -use serde::{Deserialize, Serialize}; use anyhow::anyhow; +use serde::{Deserialize, Serialize}; const APP_NAME: &str = "gcli"; @@ -93,45 +93,43 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE confy::get_configuration_file_path(APP_NAME, None)?.display() ); } - Subcommand::Show => { - match data.args.output_format { - OutputFormat::Human => { - println!("{}", data.cfg); - if let Some(ref account_id) = data.cfg.address { - if let Some(account) = vault_account::find_by_id( - data.connect_db(), - &DbAccountId::from(account_id.clone()), - ) - .await? - { - println!("(Vault: {})", account); - } + Subcommand::Show => match data.args.output_format { + OutputFormat::Human => { + println!("{}", data.cfg); + if let Some(ref account_id) = data.cfg.address { + if let Some(account) = vault_account::find_by_id( + data.connect_db(), + &DbAccountId::from(account_id.clone()), + ) + .await? + { + println!("(Vault: {})", account); } } - OutputFormat::Json => { - let mut vault_account_str = None; - if let Some(ref account_id) = data.cfg.address { - if let Some(account) = vault_account::find_by_id( - data.connect_db(), - &DbAccountId::from(account_id.clone()), - ) - .await? - { - vault_account_str = Some(account.to_string()); - } + } + OutputFormat::Json => { + let mut vault_account_str = None; + if let Some(ref account_id) = data.cfg.address { + if let Some(account) = vault_account::find_by_id( + data.connect_db(), + &DbAccountId::from(account_id.clone()), + ) + .await? + { + vault_account_str = Some(account.to_string()); } - - let view = ConfigView { - duniter_endpoint: data.cfg.duniter_endpoint.clone(), - indexer_endpoint: data.cfg.indexer_endpoint.clone(), - address: data.cfg.address.as_ref().map(|a| a.to_string()), - vault_account: vault_account_str, - }; - - println!("{}", serde_json::to_string(&view).map_err(|e| anyhow!(e))?); } + + let view = ConfigView { + duniter_endpoint: data.cfg.duniter_endpoint.clone(), + indexer_endpoint: data.cfg.indexer_endpoint.clone(), + address: data.cfg.address.as_ref().map(|a| a.to_string()), + vault_account: vault_account_str, + }; + + println!("{}", serde_json::to_string(&view).map_err(|e| anyhow!(e))?); } - } + }, Subcommand::Save => { save(&data.cfg); }