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

feat: Added load indicators to wait for the view-account-summary command #349

Merged
merged 3 commits into from
Jun 18, 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
8 changes: 8 additions & 0 deletions src/commands/account/export_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub fn get_account_key_pair_from_keychain(
account_key_pair.wrap_err("Error reading data")
}

#[tracing::instrument(
name = "Receiving the account key pair from the keychain ...",
skip_all
)]
pub fn get_password_from_keychain(
network_config: &crate::config::NetworkConfig,
account_id: &near_primitives::types::AccountId,
Expand Down Expand Up @@ -147,6 +151,10 @@ fn get_account_key_pair_data_path(
)
}

#[tracing::instrument(
name = "Receiving the account key pair from a legacy keychain ...",
skip_all
)]
pub fn get_account_properties_data_path(
network_config: &crate::config::NetworkConfig,
account_id: &near_primitives::types::AccountId,
Expand Down
68 changes: 41 additions & 27 deletions src/commands/account/view_account_summary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use color_eyre::eyre::Context;
use futures::{StreamExt, TryStreamExt};
use tracing_indicatif::span_ext::IndicatifSpanExt;

use crate::common::{CallResultExt, JsonRpcClientExt, RpcQueryResponseExt};

Expand Down Expand Up @@ -97,33 +98,7 @@ impl ViewAccountSummaryContext {
.try_collect(),
)?;

let optional_account_profile =
if let Ok(contract_account_id) = network_config.get_near_social_account_id_from_network() {
let mut social_db = network_config
.json_rpc_client()
.blocking_call_view_function(
&contract_account_id,
"get",
serde_json::to_vec(&serde_json::json!({
"keys": vec![format!("{account_id}/profile/**")],
}))?,
block_reference.clone(),
)
.wrap_err_with(|| {
format!("Failed to fetch query for view method: 'get {account_id}/profile/**' (contract <{}> on network <{}>)",
contract_account_id,
network_config.network_name
)
})?
.parse_result_from_json::<near_socialdb_client::types::socialdb_types::SocialDb>()
.wrap_err_with(|| {
format!("Failed to parse view function call return value for {account_id}/profile.")
})?;

social_db.accounts.remove(&account_id)
} else {
None
};
let optional_account_profile = get_account_profile(&account_id, network_config, block_reference).ok().flatten();

crate::common::display_account_info(
&rpc_query_response.block_hash,
Expand Down Expand Up @@ -163,12 +138,17 @@ impl ViewAccountSummary {
}
}

#[tracing::instrument(
name = "Receiving the delegated staked balance from validator",
skip_all
)]
async fn get_delegated_staked_balance(
json_rpc_client: &near_jsonrpc_client::JsonRpcClient,
block_reference: &near_primitives::types::BlockReference,
staking_pool_account_id: &near_primitives::types::AccountId,
account_id: &near_primitives::types::AccountId,
) -> color_eyre::eyre::Result<near_token::NearToken> {
tracing::Span::current().pb_set_message(staking_pool_account_id.as_str());
let account_staked_balance_response = json_rpc_client
.call(near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference: block_reference.clone(),
Expand Down Expand Up @@ -202,3 +182,37 @@ async fn get_delegated_staked_balance(
Err(err) => Err(err.into()),
}
}

#[tracing::instrument(name = "Getting an account profile ...", skip_all)]
fn get_account_profile(
account_id: &near_primitives::types::AccountId,
network_config: &crate::config::NetworkConfig,
block_reference: &near_primitives::types::BlockReference,
) -> color_eyre::Result<Option<near_socialdb_client::types::socialdb_types::AccountProfile>> {
if let Ok(contract_account_id) = network_config.get_near_social_account_id_from_network() {
let mut social_db = network_config
.json_rpc_client()
.blocking_call_view_function(
&contract_account_id,
"get",
serde_json::to_vec(&serde_json::json!({
"keys": vec![format!("{account_id}/profile/**")],
}))?,
block_reference.clone(),
)
.wrap_err_with(|| {
format!("Failed to fetch query for view method: 'get {account_id}/profile/**' (contract <{}> on network <{}>)",
contract_account_id,
network_config.network_name
)
})?
.parse_result_from_json::<near_socialdb_client::types::socialdb_types::SocialDb>()
.wrap_err_with(|| {
format!("Failed to parse view function call return value for {account_id}/profile.")
})?;

Ok(social_db.accounts.remove(account_id))
} else {
Ok(None)
}
}
74 changes: 72 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ pub fn display_account_info(
access_key_list: Option<&near_primitives::views::AccessKeyList>,
optional_account_profile: Option<&near_socialdb_client::types::socialdb_types::AccountProfile>,
) {
eprintln!();
let mut table: Table = Table::new();
table.set_format(*prettytable::format::consts::FORMAT_NO_COLSEP);

Expand Down Expand Up @@ -1896,6 +1897,13 @@ fn profile_table(
Fy->account_id,
format!("At block #{}\n({})", viewed_at_block_height, viewed_at_block_hash)
]);
table.add_row(prettytable::row![
Fd->"NEAR Social profile unavailable",
Fd->format!("The profile can be edited at {}\nor using the cli command: {}\n(https://github.com/bos-cli-rs/bos-cli-rs)",
"https://near.social".blue(),
"bos social-db manage-profile".blue()
)
]);
}
}

Expand Down Expand Up @@ -1994,8 +2002,60 @@ pub fn input_network_name(
}
}

#[easy_ext::ext(JsonRpcClientExt)]
pub impl near_jsonrpc_client::JsonRpcClient {
pub trait JsonRpcClientExt {
fn blocking_call<M>(
&self,
method: M,
) -> near_jsonrpc_client::MethodCallResult<M::Response, M::Error>
where
M: near_jsonrpc_client::methods::RpcMethod;

/// A helper function to make a view-funcation call using JSON encoding for the function
/// arguments and function return value.
fn blocking_call_view_function(
&self,
account_id: &near_primitives::types::AccountId,
method_name: &str,
args: Vec<u8>,
block_reference: near_primitives::types::BlockReference,
) -> Result<near_primitives::views::CallResult, color_eyre::eyre::Error>;

fn blocking_call_view_access_key(
&self,
account_id: &near_primitives::types::AccountId,
public_key: &near_crypto::PublicKey,
block_reference: near_primitives::types::BlockReference,
) -> Result<
near_jsonrpc_primitives::types::query::RpcQueryResponse,
near_jsonrpc_client::errors::JsonRpcError<
near_jsonrpc_primitives::types::query::RpcQueryError,
>,
>;

fn blocking_call_view_access_key_list(
&self,
account_id: &near_primitives::types::AccountId,
block_reference: near_primitives::types::BlockReference,
) -> Result<
near_jsonrpc_primitives::types::query::RpcQueryResponse,
near_jsonrpc_client::errors::JsonRpcError<
near_jsonrpc_primitives::types::query::RpcQueryError,
>,
>;

fn blocking_call_view_account(
&self,
account_id: &near_primitives::types::AccountId,
block_reference: near_primitives::types::BlockReference,
) -> Result<
near_jsonrpc_primitives::types::query::RpcQueryResponse,
near_jsonrpc_client::errors::JsonRpcError<
near_jsonrpc_primitives::types::query::RpcQueryError,
>,
>;
}

impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
fn blocking_call<M>(
&self,
method: M,
Expand All @@ -2010,13 +2070,17 @@ pub impl near_jsonrpc_client::JsonRpcClient {

/// A helper function to make a view-funcation call using JSON encoding for the function
/// arguments and function return value.
#[tracing::instrument(name = "Getting the result of executing", skip_all)]
fn blocking_call_view_function(
&self,
account_id: &near_primitives::types::AccountId,
method_name: &str,
args: Vec<u8>,
block_reference: near_primitives::types::BlockReference,
) -> Result<near_primitives::views::CallResult, color_eyre::eyre::Error> {
tracing::Span::current().pb_set_message(&format!(
"the '{method_name}' method of the <{account_id}> contract ..."
));
let query_view_method_response = self
.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
Expand All @@ -2030,6 +2094,7 @@ pub impl near_jsonrpc_client::JsonRpcClient {
query_view_method_response.call_result()
}

#[tracing::instrument(name = "Getting access key information:", skip_all)]
fn blocking_call_view_access_key(
&self,
account_id: &near_primitives::types::AccountId,
Expand All @@ -2041,6 +2106,7 @@ pub impl near_jsonrpc_client::JsonRpcClient {
near_jsonrpc_primitives::types::query::RpcQueryError,
>,
> {
tracing::Span::current().pb_set_message(&format!("{public_key} ..."));
self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::ViewAccessKey {
Expand All @@ -2050,6 +2116,7 @@ pub impl near_jsonrpc_client::JsonRpcClient {
})
}

#[tracing::instrument(name = "Getting a list of", skip_all)]
fn blocking_call_view_access_key_list(
&self,
account_id: &near_primitives::types::AccountId,
Expand All @@ -2060,6 +2127,7 @@ pub impl near_jsonrpc_client::JsonRpcClient {
near_jsonrpc_primitives::types::query::RpcQueryError,
>,
> {
tracing::Span::current().pb_set_message(&format!("{account_id} access keys ..."));
self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::ViewAccessKeyList {
Expand All @@ -2068,6 +2136,7 @@ pub impl near_jsonrpc_client::JsonRpcClient {
})
}

#[tracing::instrument(name = "Getting information about", skip_all)]
fn blocking_call_view_account(
&self,
account_id: &near_primitives::types::AccountId,
Expand All @@ -2078,6 +2147,7 @@ pub impl near_jsonrpc_client::JsonRpcClient {
near_jsonrpc_primitives::types::query::RpcQueryError,
>,
> {
tracing::Span::current().pb_set_message(&format!("{account_id} ..."));
self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest {
block_reference,
request: near_primitives::views::QueryRequest::ViewAccount {
Expand Down
3 changes: 3 additions & 0 deletions src/network_view_at_block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct Now;
pub struct NowContext;

impl NowContext {
#[tracing::instrument(name = "Receiving an inquiry about your account ...", skip_all)]
pub fn from_previous_context(
previous_context: NetworkViewAtBlockArgsContext,
_scope: &<Now as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
Expand All @@ -114,6 +115,7 @@ pub struct AtBlockHeight {
pub struct AtBlockHeightContext;

impl AtBlockHeightContext {
#[tracing::instrument(name = "Receiving an inquiry about your account ...", skip_all)]
pub fn from_previous_context(
previous_context: NetworkViewAtBlockArgsContext,
scope: &<AtBlockHeight as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
Expand All @@ -140,6 +142,7 @@ pub struct BlockIdHash {
pub struct BlockIdHashContext;

impl BlockIdHashContext {
#[tracing::instrument(name = "Receiving an inquiry about your account ...", skip_all)]
pub fn from_previous_context(
previous_context: NetworkViewAtBlockArgsContext,
scope: &<BlockIdHash as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
Expand Down
10 changes: 3 additions & 7 deletions src/transaction_signature_options/sign_with_keychain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use color_eyre::eyre::{ContextCompat, WrapErr};
use color_eyre::owo_colors::OwoColorize;
use inquire::CustomType;
use tracing_indicatif::span_ext::IndicatifSpanExt;

Expand Down Expand Up @@ -136,10 +135,7 @@ impl SignKeychainContext {
Some(password) => password,
None => {
// no access keys found, try the legacy keychain
warning_message(format!(
"{}",
"no access keys found in keychain, trying legacy keychain".red()
));
warning_message("no access keys found in keychain, trying legacy keychain");
return from_legacy_keychain(previous_context, scope);
}
}
Expand Down Expand Up @@ -224,8 +220,8 @@ impl SignKeychainContext {
}

#[tracing::instrument(name = "Warning:", skip_all)]
fn warning_message(instrument_message: String) {
tracing::Span::current().pb_set_message(&instrument_message);
fn warning_message(instrument_message: &str) {
tracing::Span::current().pb_set_message(instrument_message);
std::thread::sleep(std::time::Duration::from_secs(1));
}

Expand Down
Loading