Skip to content

Commit

Permalink
fix: Handle delegated stake errors gracefully and display a warning m…
Browse files Browse the repository at this point in the history
…essage instead of failing the view-account-summary command completely (#382)

Before:
![photo_2024-08-09 18 14
51](https://github.com/user-attachments/assets/4bc837c7-a93c-4e0a-b081-e5a27b78a1d5)

Now:
![Screenshot 2024-08-09 at 20 25
46](https://github.com/user-attachments/assets/04da99ec-d8e3-43a8-9d51-17d2e34ca552)
  • Loading branch information
FroVolod authored Aug 11, 2024
1 parent 2533b50 commit 53b75da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
58 changes: 30 additions & 28 deletions src/commands/account/view_account_summary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,47 +109,49 @@ fn get_account_inquiry(
.ok()
});
let validators = if let Some(validators) = historically_delegated_validators {
validators
Ok(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,
)?
)
} else {
Default::default()
Ok(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
let delegated_stake: color_eyre::Result<
std::collections::BTreeMap<near_primitives::types::AccountId, near_token::NearToken>,
> = 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))
})
})
.try_collect(),
)?;
.buffer_unordered(concurrency)
.filter(|balance_result| {
futures::future::ready(if let Ok((_, balance)) = balance_result {
!balance.is_zero()
} else {
true
})
})
.try_collect(),
)?),
Err(err) => Err(err),
};

let optional_account_profile = get_account_profile(account_id, network_config, block_reference)
.ok()
Expand All @@ -159,7 +161,7 @@ fn get_account_inquiry(
&rpc_query_response.block_hash,
&rpc_query_response.block_height,
account_id,
&delegated_stake,
delegated_stake,
&account_view,
access_key_list.as_ref(),
optional_account_profile.as_ref(),
Expand Down
27 changes: 18 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ pub fn fetch_currently_active_staking_pools(
include_proof: false,
},
})
.context("Failed to fetch query ViewState for <poolv1.near> on the selected network")?;
.map_err(color_eyre::Report::msg)?;
if let near_jsonrpc_primitives::types::query::QueryResponseKind::ViewState(result) =
query_view_method_response.kind
{
Expand Down Expand Up @@ -1699,9 +1699,8 @@ 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: &std::collections::BTreeMap<
near_primitives::types::AccountId,
near_token::NearToken,
delegated_stake: color_eyre::Result<
std::collections::BTreeMap<near_primitives::types::AccountId, near_token::NearToken>,
>,
account_view: &near_primitives::views::AccountView,
access_key_list: Option<&near_primitives::views::AccessKeyList>,
Expand All @@ -1728,11 +1727,21 @@ pub fn display_account_info(
Fy->near_token::NearToken::from_yoctonear(account_view.locked)
]);

for (validator_id, stake) in delegated_stake {
table.add_row(prettytable::row![
Fg->format!("Delegated stake with <{validator_id}>"),
Fy->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->"Delegated stake",
Fr->err
]);
}
}

table.add_row(prettytable::row![
Expand Down

0 comments on commit 53b75da

Please sign in to comment.