From 9787041980f3aae324c37bf10ea07f11259fe97e Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 9 Aug 2024 18:04:26 +0300 Subject: [PATCH 1/3] start --- .../account/view_account_summary/mod.rs | 65 ++++++++++--------- src/common.rs | 31 +++++++-- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/commands/account/view_account_summary/mod.rs b/src/commands/account/view_account_summary/mod.rs index 3b33a2d0..061ece4b 100644 --- a/src/commands/account/view_account_summary/mod.rs +++ b/src/commands/account/view_account_summary/mod.rs @@ -109,57 +109,64 @@ fn get_account_inquiry( .ok() }); let validators = if let Some(validators) = historically_delegated_validators { - validators + Some(validators) } else if let Some(staking_pools_factory_account_id) = &network_config.staking_pools_factory_account_id { crate::common::fetch_currently_active_staking_pools( &json_rpc_client, staking_pools_factory_account_id, - )? + ) + .ok() } else { - Default::default() + Some(Default::default()) }; let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build()?; let concurrency = 10; - let delegated_stake: std::collections::BTreeMap< - near_primitives::types::AccountId, - near_token::NearToken, - > = runtime.block_on( - futures::stream::iter(validators) - .map(|validator_account_id| async { - let balance = get_delegated_staked_balance( - &json_rpc_client, - block_reference, - &validator_account_id, - account_id, - ) - .await?; - Ok::<_, color_eyre::eyre::Report>((validator_account_id, balance)) - }) - .buffer_unordered(concurrency) - .filter(|balance_result| { - futures::future::ready(if let Ok((_, balance)) = balance_result { - !balance.is_zero() - } else { - true - }) - }) - .try_collect(), - )?; + let delegated_stake: Option< + std::collections::BTreeMap, + > = if let Some(validators) = validators { + Some( + runtime.block_on( + futures::stream::iter(validators) + .map(|validator_account_id| async { + let balance = get_delegated_staked_balance( + &json_rpc_client, + block_reference, + &validator_account_id, + account_id, + ) + .await?; + Ok::<_, color_eyre::eyre::Report>((validator_account_id, balance)) + }) + .buffer_unordered(concurrency) + .filter(|balance_result| { + futures::future::ready(if let Ok((_, balance)) = balance_result { + !balance.is_zero() + } else { + true + }) + }) + .try_collect(), + )?, + ) + } else { + None + }; let optional_account_profile = get_account_profile(account_id, network_config, block_reference) .ok() .flatten(); crate::common::display_account_info( + network_config, &rpc_query_response.block_hash, &rpc_query_response.block_height, account_id, - &delegated_stake, + delegated_stake.as_ref(), &account_view, access_key_list.as_ref(), optional_account_profile.as_ref(), diff --git a/src/common.rs b/src/common.rs index c81e0645..be8b35fc 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1567,7 +1567,12 @@ pub fn fetch_currently_active_staking_pools( include_proof: false, }, }) - .context("Failed to fetch query ViewState for on the selected network")?; + .wrap_err_with(|| { + format!( + "Failed to fetch query ViewState for <{}> on the selected network", + staking_pools_factory_account_id + ) + })?; if let near_jsonrpc_primitives::types::query::QueryResponseKind::ViewState(result) = query_view_method_response.kind { @@ -1695,13 +1700,14 @@ async fn get_staking_pool_info( }) } +#[allow(clippy::too_many_arguments)] pub fn display_account_info( + network_config: &crate::config::NetworkConfig, viewed_at_block_hash: &CryptoHash, viewed_at_block_height: &near_primitives::types::BlockHeight, account_id: &near_primitives::types::AccountId, - delegated_stake: &std::collections::BTreeMap< - near_primitives::types::AccountId, - near_token::NearToken, + delegated_stake: Option< + &std::collections::BTreeMap, >, account_view: &near_primitives::views::AccountView, access_key_list: Option<&near_primitives::views::AccessKeyList>, @@ -1728,10 +1734,21 @@ pub fn display_account_info( Fy->near_token::NearToken::from_yoctonear(account_view.locked) ]); - for (validator_id, stake) in delegated_stake { + if let Some(delegated_stake) = delegated_stake { + for (validator_id, stake) in delegated_stake { + table.add_row(prettytable::row![ + Fg->format!("Delegated stake with <{validator_id}>"), + Fy->stake + ]); + } + } else { table.add_row(prettytable::row![ - Fg->format!("Delegated stake with <{validator_id}>"), - Fy->stake + Fg->"Delegated stake", + Fr->format!( + "Warning: Failed to fetch query ViewState for <{}> on network <{}>.", + network_config.staking_pools_factory_account_id.clone().unwrap_or("poolv1.near".parse().unwrap()), + network_config.network_name + ) ]); } From 000cf627cb74a5f1ed07468f7c270a8807800f1e Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 9 Aug 2024 20:57:58 +0300 Subject: [PATCH 2/3] refactored --- .../account/view_account_summary/mod.rs | 59 +++++++++---------- src/common.rs | 46 +++++++-------- 2 files changed, 51 insertions(+), 54 deletions(-) diff --git a/src/commands/account/view_account_summary/mod.rs b/src/commands/account/view_account_summary/mod.rs index 061ece4b..cb295ec8 100644 --- a/src/commands/account/view_account_summary/mod.rs +++ b/src/commands/account/view_account_summary/mod.rs @@ -109,7 +109,7 @@ fn get_account_inquiry( .ok() }); let validators = if let Some(validators) = historically_delegated_validators { - Some(validators) + Ok(validators) } else if let Some(staking_pools_factory_account_id) = &network_config.staking_pools_factory_account_id { @@ -117,44 +117,41 @@ fn get_account_inquiry( &json_rpc_client, staking_pools_factory_account_id, ) - .ok() } else { - Some(Default::default()) + Ok(Default::default()) }; let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build()?; let concurrency = 10; - let delegated_stake: Option< + let delegated_stake: Result< std::collections::BTreeMap, - > = if let Some(validators) = validators { - Some( - runtime.block_on( - futures::stream::iter(validators) - .map(|validator_account_id| async { - let balance = get_delegated_staked_balance( - &json_rpc_client, - block_reference, - &validator_account_id, - account_id, - ) - .await?; - Ok::<_, color_eyre::eyre::Report>((validator_account_id, balance)) - }) - .buffer_unordered(concurrency) - .filter(|balance_result| { - futures::future::ready(if let Ok((_, balance)) = balance_result { - !balance.is_zero() - } else { - true - }) + String, + > = match validators { + Ok(validators) => Ok(runtime.block_on( + futures::stream::iter(validators) + .map(|validator_account_id| async { + let balance = get_delegated_staked_balance( + &json_rpc_client, + block_reference, + &validator_account_id, + account_id, + ) + .await?; + Ok::<_, color_eyre::eyre::Report>((validator_account_id, balance)) + }) + .buffer_unordered(concurrency) + .filter(|balance_result| { + futures::future::ready(if let Ok((_, balance)) = balance_result { + !balance.is_zero() + } else { + true }) - .try_collect(), - )?, - ) - } else { - None + }) + .try_collect(), + )?), + Err(err) => Err(err), }; let optional_account_profile = get_account_profile(account_id, network_config, block_reference) @@ -166,7 +163,7 @@ fn get_account_inquiry( &rpc_query_response.block_hash, &rpc_query_response.block_height, account_id, - delegated_stake.as_ref(), + delegated_stake, &account_view, access_key_list.as_ref(), optional_account_profile.as_ref(), diff --git a/src/common.rs b/src/common.rs index be8b35fc..dc80f485 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1557,7 +1557,7 @@ pub fn fetch_historically_delegated_staking_pools( pub fn fetch_currently_active_staking_pools( json_rpc_client: &near_jsonrpc_client::JsonRpcClient, staking_pools_factory_account_id: &near_primitives::types::AccountId, -) -> color_eyre::Result> { +) -> Result, String> { let query_view_method_response = json_rpc_client .blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference: near_primitives::types::Finality::Final.into(), @@ -1567,12 +1567,7 @@ pub fn fetch_currently_active_staking_pools( include_proof: false, }, }) - .wrap_err_with(|| { - format!( - "Failed to fetch query ViewState for <{}> on the selected network", - staking_pools_factory_account_id - ) - })?; + .map_err(|err| err.to_string())?; if let near_jsonrpc_primitives::types::query::QueryResponseKind::ViewState(result) = query_view_method_response.kind { @@ -1582,7 +1577,7 @@ pub fn fetch_currently_active_staking_pools( .filter_map(|item| near_primitives::borsh::from_slice(&item.value).ok()) .collect()) } else { - Err(color_eyre::Report::msg("Error call result".to_string())) + Err("Error call result".to_string()) } } @@ -1706,8 +1701,9 @@ pub fn display_account_info( viewed_at_block_hash: &CryptoHash, viewed_at_block_height: &near_primitives::types::BlockHeight, account_id: &near_primitives::types::AccountId, - delegated_stake: Option< - &std::collections::BTreeMap, + delegated_stake: Result< + std::collections::BTreeMap, + String, >, account_view: &near_primitives::views::AccountView, access_key_list: Option<&near_primitives::views::AccessKeyList>, @@ -1734,22 +1730,26 @@ pub fn display_account_info( Fy->near_token::NearToken::from_yoctonear(account_view.locked) ]); - if let Some(delegated_stake) = delegated_stake { - for (validator_id, stake) in delegated_stake { + match delegated_stake { + Ok(delegated_stake) => { + for (validator_id, stake) in delegated_stake { + table.add_row(prettytable::row![ + Fg->format!("Delegated stake with <{validator_id}>"), + Fy->stake + ]); + } + } + Err(err) => { table.add_row(prettytable::row![ - Fg->format!("Delegated stake with <{validator_id}>"), - Fy->stake + Fg->"Delegated stake", + Fr->format!( + "Warning: Failed to fetch query ViewState for <{}> on network <{}>\n{}", + network_config.staking_pools_factory_account_id.clone().unwrap_or("poolv1.near".parse().unwrap()), + network_config.network_name, + err + ) ]); } - } else { - table.add_row(prettytable::row![ - Fg->"Delegated stake", - Fr->format!( - "Warning: Failed to fetch query ViewState for <{}> on network <{}>.", - network_config.staking_pools_factory_account_id.clone().unwrap_or("poolv1.near".parse().unwrap()), - network_config.network_name - ) - ]); } table.add_row(prettytable::row![ From ba08420e47c185cefa5c567337a50e3ddccfdbdb Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 10 Aug 2024 13:05:55 +0300 Subject: [PATCH 3/3] refactored display_account_info() --- .../account/view_account_summary/mod.rs | 4 +--- src/common.rs | 18 +++++------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/commands/account/view_account_summary/mod.rs b/src/commands/account/view_account_summary/mod.rs index cb295ec8..0f5fd8a6 100644 --- a/src/commands/account/view_account_summary/mod.rs +++ b/src/commands/account/view_account_summary/mod.rs @@ -125,9 +125,8 @@ fn get_account_inquiry( .enable_all() .build()?; let concurrency = 10; - let delegated_stake: Result< + let delegated_stake: color_eyre::Result< std::collections::BTreeMap, - String, > = match validators { Ok(validators) => Ok(runtime.block_on( futures::stream::iter(validators) @@ -159,7 +158,6 @@ fn get_account_inquiry( .flatten(); crate::common::display_account_info( - network_config, &rpc_query_response.block_hash, &rpc_query_response.block_height, account_id, diff --git a/src/common.rs b/src/common.rs index dc80f485..870829e9 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1557,7 +1557,7 @@ pub fn fetch_historically_delegated_staking_pools( pub fn fetch_currently_active_staking_pools( json_rpc_client: &near_jsonrpc_client::JsonRpcClient, staking_pools_factory_account_id: &near_primitives::types::AccountId, -) -> Result, String> { +) -> color_eyre::Result> { let query_view_method_response = json_rpc_client .blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference: near_primitives::types::Finality::Final.into(), @@ -1567,7 +1567,7 @@ pub fn fetch_currently_active_staking_pools( include_proof: false, }, }) - .map_err(|err| err.to_string())?; + .map_err(color_eyre::Report::msg)?; if let near_jsonrpc_primitives::types::query::QueryResponseKind::ViewState(result) = query_view_method_response.kind { @@ -1577,7 +1577,7 @@ pub fn fetch_currently_active_staking_pools( .filter_map(|item| near_primitives::borsh::from_slice(&item.value).ok()) .collect()) } else { - Err("Error call result".to_string()) + Err(color_eyre::Report::msg("Error call result".to_string())) } } @@ -1695,15 +1695,12 @@ async fn get_staking_pool_info( }) } -#[allow(clippy::too_many_arguments)] pub fn display_account_info( - network_config: &crate::config::NetworkConfig, viewed_at_block_hash: &CryptoHash, viewed_at_block_height: &near_primitives::types::BlockHeight, account_id: &near_primitives::types::AccountId, - delegated_stake: Result< + delegated_stake: color_eyre::Result< std::collections::BTreeMap, - String, >, account_view: &near_primitives::views::AccountView, access_key_list: Option<&near_primitives::views::AccessKeyList>, @@ -1742,12 +1739,7 @@ pub fn display_account_info( Err(err) => { table.add_row(prettytable::row![ Fg->"Delegated stake", - Fr->format!( - "Warning: Failed to fetch query ViewState for <{}> on network <{}>\n{}", - network_config.staking_pools_factory_account_id.clone().unwrap_or("poolv1.near".parse().unwrap()), - network_config.network_name, - err - ) + Fr->err ]); } }