Skip to content

Commit

Permalink
feat: Added loading indicators for "accounts" group commands (near#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod authored Jun 21, 2024
1 parent f36b8de commit dc6b82c
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,49 +178,43 @@ impl SignerAccountId {
}
}

#[tracing::instrument(name = "Validation new account_id ...", skip_all)]
fn validate_new_account_id(
network_config: &crate::config::NetworkConfig,
account_id: &near_primitives::types::AccountId,
) -> crate::CliResult {
for _ in 0..3 {
let account_state =
tokio::runtime::Runtime::new()
.unwrap()
.block_on(crate::common::get_account_state(
network_config,
account_id,
near_primitives::types::BlockReference::latest(),
));
if let Err(near_jsonrpc_client::errors::JsonRpcError::TransportError(
near_jsonrpc_client::errors::RpcTransportError::SendError(_),
)) = account_state
{
eprintln!("Transport error.\nPlease wait. The next try to send this query is happening right now ...");
std::thread::sleep(std::time::Duration::from_millis(100))
} else {
match account_state {
Ok(_) => {
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!(
"\nAccount <{}> already exists in network <{}>. Therefore, it is not possible to create an account with this name.",
account_id,
network_config.network_name
));
}
Err(near_jsonrpc_client::errors::JsonRpcError::ServerError(
near_jsonrpc_client::errors::JsonRpcServerError::HandlerError(
near_jsonrpc_primitives::types::query::RpcQueryError::UnknownAccount {
..
},
),
)) => {
return Ok(());
}
Err(err) => {
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!(err.to_string()))
}
}
let account_state =
tokio::runtime::Runtime::new()
.unwrap()
.block_on(crate::common::get_account_state(
network_config,
account_id,
near_primitives::types::BlockReference::latest(),
));
match account_state {
Ok(_) => {
color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!(
"\nAccount <{}> already exists in network <{}>. Therefore, it is not possible to create an account with this name.",
account_id,
network_config.network_name
))
}
Err(near_jsonrpc_client::errors::JsonRpcError::ServerError(
near_jsonrpc_client::errors::JsonRpcServerError::HandlerError(
near_jsonrpc_primitives::types::query::RpcQueryError::UnknownAccount {
..
},
),
)) => {
eprintln!("\nServer error.\nIt is currently possible to continue creating an account offline.\nYou can sign and send the created transaction later.");
Ok(())
}
Err(near_jsonrpc_client::errors::JsonRpcError::TransportError(_)) => {
eprintln!("\nTransport error.\nIt is currently possible to continue creating an account offline.\nYou can sign and send the created transaction later.");
Ok(())
}
Err(err) => {
color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!(err.to_string()))
}
}
eprintln!("\nTransport error.\nIt is currently possible to continue creating an account offline.\nYou can sign and send the created transaction later.");
Ok(())
}
51 changes: 32 additions & 19 deletions src/commands/account/storage_management/view_storage_balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::common::{CallResultExt, JsonRpcClientExt};
use color_eyre::eyre::WrapErr;
use tracing_indicatif::span_ext::IndicatifSpanExt;

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

const STORAGE_COST_PER_BYTE: u128 = 10u128.pow(19);

Expand Down Expand Up @@ -30,24 +32,7 @@ impl AccountContext {
move |network_config, block_reference| {
let contract_account_id = (previous_context.get_contract_account_id)(network_config)?;

let storage_balance = network_config
.json_rpc_client()
.blocking_call_view_function(
&contract_account_id,
"storage_balance_of",
serde_json::to_vec(&serde_json::json!({
"account_id": account_id.to_string(),
}))?,
block_reference.clone(),
)
.wrap_err_with(|| {
format!("Failed to fetch query for view method: 'storage_balance_of' (contract <{}> on network <{}>)",
contract_account_id,
network_config.network_name
)
})?
.parse_result_from_json::<near_socialdb_client::StorageBalance>()
.wrap_err("Failed to parse return value of view function call for StorageBalance.")?;
let storage_balance = get_storage_balance(network_config, &contract_account_id, &account_id, block_reference)?;
eprintln!("storage balance for <{account_id}>:");
eprintln!(" {:<13} {:>10} ({} [{:>28} yoctoNEAR])",
"available:",
Expand Down Expand Up @@ -90,3 +75,31 @@ impl Account {
)
}
}

#[tracing::instrument(name = "Getting storage balance for", skip_all)]
fn get_storage_balance(
network_config: &crate::config::NetworkConfig,
contract_account_id: &near_primitives::types::AccountId,
account_id: &crate::types::account_id::AccountId,
block_reference: &near_primitives::types::BlockReference,
) -> color_eyre::eyre::Result<near_socialdb_client::StorageBalance> {
tracing::Span::current().pb_set_message(account_id.as_ref());
network_config
.json_rpc_client()
.blocking_call_view_function(
contract_account_id,
"storage_balance_of",
serde_json::to_vec(&serde_json::json!({
"account_id": account_id.to_string(),
}))?,
block_reference.clone(),
)
.wrap_err_with(|| {
format!("Failed to fetch query for view method: 'storage_balance_of' (contract <{}> on network <{}>)",
contract_account_id,
network_config.network_name
)
})?
.parse_result_from_json::<near_socialdb_client::StorageBalance>()
.wrap_err("Failed to parse return value of view function call for StorageBalance.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[interactive_clap(input_context = super::super::UpdateSocialProfileContext)]
#[interactive_clap(output_context = JsonArgsContext)]
pub struct JsonArgs {
/// Enter valid JSON arguments (e.g. {\"token_id\": \"42\"})":
/// Enter valid JSON arguments (e.g. {"name": "NEAR", "description": "NEAR is fun"}):
data: crate::types::json::Json,
#[interactive_clap(named_arg)]
/// Specify signer account ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum ProfileArgsType {
/// Interactive input of arguments
Manually(self::manually::Manually),
#[strum_discriminants(strum(
message = "json-args - Valid JSON arguments (e.g. {\"token_id\": \"42\"})"
message = "json-args - Valid JSON arguments (e.g. {\"name\": \"NEAR\", \"description\": \"NEAR is fun\"})"
))]
/// Valid JSON arguments (e.g. {"token_id": "42"})
JsonArgs(self::json_args::JsonArgs),
Expand Down
Loading

0 comments on commit dc6b82c

Please sign in to comment.