Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Left a message that information about the delegated stake could not be obtained, instead of failing the operation completely #382

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading