Skip to content

Commit

Permalink
refactored commands for the contract
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Feb 19, 2025
1 parent f71119d commit fa72ba2
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 33 deletions.
20 changes: 14 additions & 6 deletions src/commands/contract/call_function/as_read_only/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,24 @@ fn call_view_function(
)
})?;
call_result.print_logs();
eprintln!("Result:");
let mut info_str = String::new();
if call_result.result.is_empty() {
eprintln!("Empty result");
info_str.push_str("\nEmpty result");
} else if let Ok(json_result) = call_result.parse_result_from_json::<serde_json::Value>() {
println!("{}", serde_json::to_string_pretty(&json_result)?);
info_str.push_str(&format!(
"\n{}",
serde_json::to_string_pretty(&json_result)?
));
} else if let Ok(string_result) = String::from_utf8(call_result.result) {
println!("{string_result}");
info_str.push_str(&format!("\n{string_result}"));
} else {
eprintln!("The returned value is not printable (binary data)");
info_str.push_str("\nThe returned value is not printable (binary data)");
}
eprintln!("--------------");
info_str.push_str("\n------------------------------------");
tracing::info!(
parent: &tracing::Span::none(),
"--- Result -------------------------{}\n",
crate::common::indent_payload(&info_str)
);
Ok(())
}
6 changes: 5 additions & 1 deletion src/commands/contract/download_abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ fn download_contract_abi(
.wrap_err_with(|| format!("Failed to create file: {:?}", file_path))?
.write(&serde_json::to_vec_pretty(&abi_root)?)
.wrap_err_with(|| format!("Failed to write to file: {:?}", file_path))?;
eprintln!("\nThe file {:?} was downloaded successfully", file_path);
tracing::info!(
parent: &tracing::Span::none(),
"The file {:?} was downloaded successfully",
file_path
);
Ok(())
}
7 changes: 5 additions & 2 deletions src/commands/contract/download_wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ fn download_contract_code(
.wrap_err_with(|| format!("Failed to create file: {:?}", file_path))?
.write(&call_access_view.code)
.wrap_err_with(|| format!("Failed to write to file: {:?}", file_path))?;
eprintln!("\nThe file {:?} was downloaded successfully", file_path);

tracing::info!(
parent: &tracing::Span::none(),
"The file {:?} was downloaded successfully",
file_path
);
Ok(())
}
15 changes: 8 additions & 7 deletions src/commands/contract/view_storage/output_format/as_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ impl AsJsonContext {
if let near_jsonrpc_primitives::types::query::QueryResponseKind::ViewState(result) =
query_view_method_response.kind
{
eprintln!("Contract state (values):");
println!(
"{}",
serde_json::to_string_pretty(&result.values)?
tracing::info!(
parent: &tracing::Span::none(),
"Contract state (values):\n{}\n",
crate::common::indent_payload(&serde_json::to_string_pretty(&result.values)?)
);
eprintln!(
"\nContract state (proof):\n{:#?}\n",
&result.proof
tracing::info!(
parent: &tracing::Span::none(),
"Contract state (proof):\n{}\n",
crate::common::indent_payload(&format!("{:#?}", result.proof))
);
} else {
return Err(color_eyre::Report::msg("Error call result".to_string()));
Expand Down
20 changes: 13 additions & 7 deletions src/commands/contract/view_storage/output_format/as_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ impl AsTextContext {
if let near_jsonrpc_primitives::types::query::QueryResponseKind::ViewState(result) =
query_view_method_response.kind
{
eprintln!("Contract state (values):");
let mut info_str = String::new();
for value in &result.values {
eprintln!("key:\n{}", key_value_to_string(&value.key)?.green());
eprintln!("value:\n{}", key_value_to_string(&value.value)?.yellow());
eprintln!("--------------------------------");
info_str.push_str(&format!("\nkey: {}", key_value_to_string(&value.key)?.green()));
info_str.push_str(&format!("\nvalue: {}", key_value_to_string(&value.value)?.yellow()));
info_str.push_str("\n--------------------------------");
}
eprintln!(
"\nContract state (proof):\n{:#?}\n",
&result.proof
tracing::info!(
parent: &tracing::Span::none(),
"Contract state (values):{}\n",
crate::common::indent_payload(&info_str)
);
tracing::info!(
parent: &tracing::Span::none(),
"Contract state (proof):\n{}\n",
crate::common::indent_payload(&format!("{:#?}", result.proof))
);
} else {
return Err(color_eyre::Report::msg("Error call result".to_string()));
Expand Down
6 changes: 4 additions & 2 deletions src/commands/tokens/view_ft_balance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ impl ViewFtBalanceContext {
symbol
);

eprintln!(
"\n<{owner_account_id}> account has {fungible_token} (FT-contract: {ft_contract_account_id})"
tracing::info!(
parent: &tracing::Span::none(),
"{}",
format!("<{owner_account_id}> account has {fungible_token} (FT-contract: {ft_contract_account_id})")
);
Ok(())
}
Expand Down
8 changes: 6 additions & 2 deletions src/commands/tokens/view_nft_assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ impl ViewNftAssetsContext {
call_result.print_logs();
let serde_call_result: serde_json::Value = call_result.parse_result_from_json()?;

eprintln!("\n{} account has NFT tokens:", owner_account_id);
eprintln!("{}", serde_json::to_string_pretty(&serde_call_result)?);
tracing::info!(
parent: &tracing::Span::none(),
"{} account has NFT tokens:\n{}",
owner_account_id,
crate::common::indent_payload(&serde_json::to_string_pretty(&serde_call_result)?)
);
Ok(())
}
});
Expand Down
17 changes: 11 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ fn print_value_successful_transaction(
));
}
near_primitives::views::ActionView::DeployContract { code: _ } => {
eprintln!("Contract code has been successfully deployed.",);
info_str.push_str("Contract code has been successfully deployed.");
}
near_primitives::views::ActionView::FunctionCall {
method_name,
Expand Down Expand Up @@ -2654,14 +2654,19 @@ pub impl near_primitives::views::CallResult {
}

fn print_logs(&self) {
eprintln!("--------------");
let mut info_str = String::new();
if self.logs.is_empty() {
eprintln!("No logs")
info_str.push_str("\nNo logs")
} else {
eprintln!("Logs:");
eprintln!(" {}", self.logs.join("\n "));
info_str.push_str("\nLogs:");
info_str.push_str(&format!("\n {}", self.logs.join("\n ")));
}
eprintln!("--------------");
info_str.push_str("\n------------------------------------");
tracing::info!(
parent: &tracing::Span::none(),
"--- Logs ---------------------------{}\n",
crate::common::indent_payload(&info_str)
);
}
}

Expand Down

0 comments on commit fa72ba2

Please sign in to comment.