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: Improved usability of NEAR CLI in scripts #445

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a247125
start
Jan 28, 2025
d112fa0
started refactoring the output of transaction information
Feb 4, 2025
0700014
refactored print_unsigned_transaction()
Feb 5, 2025
802e0ef
refactored information about a successfully signed transaction
Feb 5, 2025
fa76c64
Merge branch 'main' into update-for-quiet-option
FroVolod Feb 5, 2025
ff4101d
Merge branch 'main' into update-for-quiet-option
FroVolod Feb 6, 2025
bd26485
refactored create_account
Feb 12, 2025
442ad89
print debug removed
Feb 12, 2025
bd08829
print removed
Feb 12, 2025
ab44ac6
refactored StakeDelegationCommand
Feb 13, 2025
f71119d
Merge branch 'main' into update-for-quiet-option
FroVolod Feb 15, 2025
fa72ba2
refactored commands for the contract
Feb 19, 2025
7170b9f
refactored print_value_successful_transaction()
Feb 21, 2025
2f37aa6
refactored save_access_key_to_keychain_or_save_to_legacy_keychain()
Feb 21, 2025
a381537
refactored print_unsigned_transaction()
Feb 22, 2025
b77b6f3
refactored print_unsigned_transaction()
Feb 22, 2025
a98a4c3
refactored import-account
Feb 24, 2025
65180f7
refactored login()
Feb 24, 2025
ba0311a
Merge branch 'main' into update-for-quiet-option
FroVolod Mar 1, 2025
02e46d2
refactored "Create an implicit-account"
Mar 1, 2025
204396b
refactored send_ft
Mar 1, 2025
37ba0ce
Added validation for "memo"
Mar 2, 2025
e091239
refactored send_ft, send_nft
Mar 2, 2025
d1d5b4d
send_nft: the fields "gas" and "deposit" are optional
Mar 2, 2025
631d1ad
refactored view_near_balance
Mar 2, 2025
733dd3b
refactored view_near_balance, view_ft_balance, view_nft_balance
Mar 3, 2025
4b92c12
refactored staking
Mar 3, 2025
bc96dfe
refactored print_transaction_status()
Mar 3, 2025
86df900
refactored print_transaction_status()
Mar 3, 2025
62129ca
fixed printing for logs
Mar 4, 2025
ea3367e
refactored view-storage for contract
Mar 4, 2025
619bcca
refactored manage-storage-deposit
Mar 4, 2025
0342bb4
refactored
Mar 4, 2025
111d111
refactored GUIDE
Mar 4, 2025
a6f633d
ubuntu-20.04 image updated to ubuntu-latest for code_style.yml
Mar 5, 2025
4ecd42d
fixed GUIDE
Mar 5, 2025
7aecd74
fixed GUIDE
Mar 6, 2025
7c6f7fd
refactored staking
Mar 10, 2025
ce70c1a
refactored "staking" commands
Mar 10, 2025
1c31fe0
moved logs to "teach-me"
Mar 11, 2025
23c77d6
Merge branch 'main' into update-for-quiet-option
FroVolod Mar 11, 2025
752a300
fixed
Mar 11, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/code_style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
jobs:
codestyle:
name: Code Style (fmt + clippy)
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand Down
1,509 changes: 909 additions & 600 deletions docs/GUIDE.en.md

Large diffs are not rendered by default.

1,505 changes: 906 additions & 599 deletions docs/GUIDE.ru.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ impl SaveWithUseAutoGenerationContext {
.wrap_err_with(|| format!("Failed to create file: {:?}", file_path))?
.write(buf.as_bytes())
.wrap_err_with(|| format!("Failed to write to file: {:?}", folder_path))?;
eprintln!("\nThe file {:?} was saved successfully", &file_path);

tracing::info!(
parent: &tracing::Span::none(),
"The file {:?} was saved successfully",
&file_path
);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ impl SaveWithLedgerContext {
.wrap_err_with(|| format!("Failed to create file: {:?}", file_path))?
.write(buf.as_bytes())
.wrap_err_with(|| format!("Failed to write to file: {:?}", file_path))?;
eprintln!("\nThe file {:?} was saved successfully", &file_path);

tracing::info!(
parent: &tracing::Span::none(),
"The file {:?} was saved successfully",
&file_path
);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ impl SaveWithSeedPhraseContext {
.wrap_err_with(|| format!("Failed to create file: {:?}", file_path))?
.write(buf.as_bytes())
.wrap_err_with(|| format!("Failed to write to file: {:?}", file_path))?;
eprintln!("\nThe file {:?} was saved successfully", &file_path);

tracing::info!(
parent: &tracing::Span::none(),
"The file {:?} was saved successfully",
&file_path
);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ fn validate_new_account_id(
..
},
),
)) => {
eprintln!("\nServer error.\nIt is currently possible to continue creating an account offline.\nYou can sign and send the created transaction later.");
Ok(())
}
)) => 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.");
tracing::warn!(
parent: &tracing::Span::none(),
"Transport error.{}",
crate::common::indent_payload(
"\nIt is currently possible to continue creating an account offline.\nYou can sign and send the created transaction later.\n"
)
);
Ok(())
}
Err(err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ fn print_account_creation_status(
credentials_home_dir: &std::path::Path,
storage_message: String,
) -> crate::CliResult {
eprintln!();
match result {
Ok(response) => {
tracing::info!(
Expand All @@ -133,7 +132,13 @@ fn print_account_creation_status(
crate::common::indent_payload(&format!("{:#?}", response))
);
if response.status() >= reqwest::StatusCode::BAD_REQUEST {
eprintln!("WARNING! The new account <{new_account_id}> could not be created successfully.\n{storage_message}\n");
tracing::warn!(
parent: &tracing::Span::none(),
"WARNING!{}",
crate::common::indent_payload(&format!(
"\nThe new account <{new_account_id}> could not be created successfully.\n{storage_message}\n"
))
);
return Err(color_eyre::Report::msg(format!(
"The faucet (helper service) server failed with status code <{}>",
response.status()
Expand All @@ -152,23 +157,46 @@ fn print_account_creation_status(
match account_creation_transaction.status {
near_primitives::views::FinalExecutionStatus::SuccessValue(ref value) => {
if value == b"false" {
eprintln!("WARNING! The new account <{new_account_id}> could not be created successfully.\n{storage_message}\n");
tracing::warn!(
parent: &tracing::Span::none(),
"WARNING!{}",
crate::common::indent_payload(&format!(
"\nThe new account <{new_account_id}> could not be created successfully.\n{storage_message}\n"
))
);
} else {
crate::common::update_used_account_list_as_signer(
credentials_home_dir,
new_account_id.as_ref(),
);
eprintln!("New account <{new_account_id}> created successfully.\n{storage_message}\n");
tracing::info!(
parent: &tracing::Span::none(),
"{}",
crate::common::indent_payload(&format!(
"\nNew account <{new_account_id}> created successfully.\n{storage_message}\n"
))
);
}
eprintln!("Transaction ID: {id}\nTo see the transaction in the transaction explorer, please open this url in your browser:\n{path}{id}\n",
id=account_creation_transaction.transaction_outcome.id,
path=network_config.explorer_transaction_url
tracing::info!(
parent: &tracing::Span::none(),
"\n{}",
crate::common::indent_payload(&format!(
"Transaction ID: {id}\nTo see the transaction in the transaction explorer, please open this url in your browser:\n{path}{id}\n",
id=account_creation_transaction.transaction_outcome.id,
path=network_config.explorer_transaction_url
))
);
}
near_primitives::views::FinalExecutionStatus::NotStarted
| near_primitives::views::FinalExecutionStatus::Started => unreachable!(),
near_primitives::views::FinalExecutionStatus::Failure(tx_execution_error) => {
eprintln!("WARNING! The new account <{new_account_id}> could not be created successfully.\n{storage_message}\n");
tracing::warn!(
parent: &tracing::Span::none(),
"WARNING!{}",
crate::common::indent_payload(&format!(
"\nThe new account <{new_account_id}> could not be created successfully.\n{storage_message}\n"
))
);
match tx_execution_error {
near_primitives::errors::TxExecutionError::ActionError(action_error) => {
return crate::common::convert_action_error_to_cli_result(
Expand All @@ -194,7 +222,13 @@ fn print_account_creation_status(
"JSON RPC Response:\n{}",
crate::common::indent_payload(&err.to_string())
);
eprintln!("WARNING! The new account <{new_account_id}> could not be created successfully.\n{storage_message}\n");
tracing::warn!(
parent: &tracing::Span::none(),
"WARNING!{}",
crate::common::indent_payload(&format!(
"\nThe new account <{new_account_id}> could not be created successfully.\n{storage_message}\n"
))
);
Err(color_eyre::Report::msg(err.to_string()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@ impl NetworkContext {
.wrap_err("Failed to get network config!")?
.clone();

eprintln!("\nYour transaction:");
eprintln!("{:<13} {}", "signer_id:", &network_config.network_name);
eprintln!("actions:");
eprintln!(
"{:>5} {:<20} {}",
let mut info_str: String = String::new();
info_str.push_str(&format!(
"\n{:<13} {}",
"signer_id:", &network_config.network_name
));
info_str.push_str("\nactions:");
info_str.push_str(&format!(
"\n{:>5} {:<20} {}",
"--", "create account:", &previous_context.new_account_id
);
eprintln!("{:>5} {:<20}", "--", "add access key:");
eprintln!(
"{:>18} {:<13} {}",
));
info_str.push_str(&format!("\n{:>5} {:<20}", "--", "add access key:"));
info_str.push_str(&format!(
"\n{:>18} {:<13} {}",
"", "public key:", &previous_context.public_key
));
info_str.push_str(&format!("\n{:>18} {:<13} FullAccess", "", "permission:"));
info_str.push('\n');

tracing::info!(
"Your transaction:{}",
crate::common::indent_payload(&info_str)
);
eprintln!("{:>18} {:<13} FullAccess", "", "permission:");
eprintln!();

Ok(Self {
new_account_id: previous_context.new_account_id,
Expand Down
19 changes: 15 additions & 4 deletions src/commands/account/import_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ pub fn login(

let account_id = loop {
let account_id_from_cli = input_account_id()?;
eprintln!();
if crate::common::verify_account_access_key(
account_id_from_cli.clone(),
public_key.clone(),
network_config.clone(),
)
.is_err()
{
eprintln!("{}", error_message);
tracing::warn!(
parent: &tracing::Span::none(),
"WARNING!{}",
crate::common::indent_payload(error_message)
);

#[derive(strum_macros::Display)]
enum ConfirmOptions {
Expand Down Expand Up @@ -126,7 +129,11 @@ fn save_access_key(
public_key_str,
account_id.as_ref(),
)?;
eprintln!("{}", storage_message);
tracing::info!(
parent: &tracing::Span::none(),
"\n{}",
crate::common::indent_payload(&storage_message)
);
return Ok(());
}

Expand All @@ -138,6 +145,10 @@ fn save_access_key(
account_id.as_ref(),
)
.wrap_err_with(|| format!("Failed to save a file with access key: {}", public_key_str))?;
eprintln!("{}", storage_message);
tracing::info!(
parent: &tracing::Span::none(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to repeat that parent: none()?

"\n{}",
crate::common::indent_payload(&storage_message)
);
Ok(())
}
10 changes: 7 additions & 3 deletions src/commands/account/storage_management/storage_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,13 @@ impl SignerAccountIdContext {
let contract_account_id = (previous_context.get_contract_account_id)(network_config)?;

if let near_primitives::views::FinalExecutionStatus::SuccessValue(_) = outcome_view.status {
eprintln!(
"<{signer_account_id}> has successfully added a deposit of {deposit} to <{receiver_account_id}> on contract <{contract_account_id}>.",
deposit = previous_context.deposit,
tracing::info!(
parent: &tracing::Span::none(),
"\n{}",
crate::common::indent_payload(&format!(
"<{signer_account_id}> has successfully added a deposit of {deposit} to <{receiver_account_id}> on contract <{contract_account_id}>.",
deposit = previous_context.deposit,
))
);
}
Ok(())
Expand Down
10 changes: 7 additions & 3 deletions src/commands/account/storage_management/storage_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ impl SignerAccountIdContext {
move |outcome_view, network_config| {
let contract_account_id = (previous_context.get_contract_account_id)(network_config)?;
if let near_primitives::views::FinalExecutionStatus::SuccessValue(_) = outcome_view.status {
eprintln!(
"<{signer_account_id}> has successfully withdraw {amount} from <{contract_account_id}>.",
amount = previous_context.amount,
tracing::info!(
parent: &tracing::Span::none(),
"\n{}",
crate::common::indent_payload(&format!(
"<{signer_account_id}> has successfully withdraw {amount} from <{contract_account_id}>.",
amount = previous_context.amount,
))
);
}
Ok(())
Expand Down
6 changes: 5 additions & 1 deletion src/commands/account/update_social_profile/sign_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ impl From<SignerContext> for crate::commands::ActionContext {
let on_after_sending_transaction_callback: crate::transaction_signature_options::OnAfterSendingTransactionCallback = Arc::new({
move |transaction_info, _network_config| {
if let near_primitives::views::FinalExecutionStatus::SuccessValue(_) = transaction_info.status {
eprintln!("\nProfile for {account_id} updated successfully");
tracing::info!(
parent: &tracing::Span::none(),
"Profile for {} updated successfully",
account_id
);
} else {
color_eyre::eyre::bail!("Failed to update profile!");
};
Expand Down
36 changes: 29 additions & 7 deletions src/commands/contract/call_function/as_read_only/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ impl FunctionContext {
let function_name = scope.function_name.clone();

move |network_config, block_reference| {
call_view_function(network_config, &account_id, &function_name, function_args.clone(), function_args_type.clone(), block_reference)
call_view_function(
network_config,
&account_id,
&function_name,
function_args.clone(),
function_args_type.clone(),
block_reference,
&previous_context.global_context.verbosity,
)
}
});

Expand Down Expand Up @@ -117,6 +125,7 @@ fn call_view_function(
function_args: String,
function_args_type: super::call_function_args_type::FunctionArgsType,
block_reference: &near_primitives::types::BlockReference,
verbosity: &crate::Verbosity,
) -> crate::CliResult {
let args = super::call_function_args_type::function_args(function_args, function_args_type)?;
let call_result = network_config
Expand All @@ -129,16 +138,29 @@ 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!("--------------");

if let crate::Verbosity::Quiet = verbosity {
println!("{}", info_str);
return Ok(());
};
info_str.push_str("\n------------------------------------");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

tracing::info!(
parent: &tracing::Span::none(),
"--- Result -------------------------{}",
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(())
}
Loading
Loading