From f16c4444e813bd4662f7d166299118a6806d32de Mon Sep 17 00:00:00 2001 From: FroVolod Date: Tue, 2 Jul 2024 18:19:09 +0300 Subject: [PATCH 01/13] start --- src/lib.rs | 1 + src/main.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0b5cc6cdd..ad273c6b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,4 +16,5 @@ pub mod utils_command; pub struct GlobalContext { pub config: crate::config::Config, pub offline: bool, + pub teach_me: bool, } diff --git a/src/main.rs b/src/main.rs index 504848730..1b7b7c18f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ -#![allow(clippy::enum_variant_names, clippy::large_enum_variant)] +#![allow( + clippy::enum_variant_names, + clippy::large_enum_variant, + clippy::too_many_arguments +)] use clap::Parser; #[cfg(feature = "self-update")] use color_eyre::eyre::WrapErr; @@ -33,6 +37,9 @@ struct Cmd { /// Offline mode #[interactive_clap(long)] offline: bool, + /// TEACH-ME mode + #[interactive_clap(long)] + teach_me: bool, #[interactive_clap(subcommand)] top_level: crate::commands::TopLevelCommand, } @@ -48,6 +55,7 @@ impl CmdContext { Ok(Self(crate::GlobalContext { config: previous_context.0, offline: scope.offline, + teach_me: scope.teach_me, })) } } @@ -206,6 +214,7 @@ fn main() -> crate::common::CliResult { ); let self_update_cli_cmd = CliCmd { offline: false, + teach_me: false, top_level: Some(crate::commands::CliTopLevelCommand::Extensions( crate::commands::extensions::CliExtensionsCommands { From b1609e604b96cb227658f9581fdaf5b41f6273ff Mon Sep 17 00:00:00 2001 From: FroVolod Date: Tue, 2 Jul 2024 18:41:15 +0300 Subject: [PATCH 02/13] added TEACH-ME to blocking_call_view_function() --- .../view_storage_balance.rs | 11 ++++++- .../account/update_social_profile/sign_as.rs | 19 ++++++++++-- .../account/view_account_summary/mod.rs | 16 +++++++--- .../call_function/as_read_only/mod.rs | 19 ++++++++++-- src/commands/staking/delegate/view_balance.rs | 21 +++++++++++++- src/commands/tokens/send_ft/amount_ft.rs | 5 ++++ src/commands/tokens/view_ft_balance/mod.rs | 12 +++++++- src/commands/tokens/view_nft_assets/mod.rs | 11 ++++++- src/common.rs | 29 ++++++++++++++----- src/types/ft_properties.rs | 2 ++ 10 files changed, 124 insertions(+), 21 deletions(-) diff --git a/src/commands/account/storage_management/view_storage_balance.rs b/src/commands/account/storage_management/view_storage_balance.rs index f490ca728..2cb93242d 100644 --- a/src/commands/account/storage_management/view_storage_balance.rs +++ b/src/commands/account/storage_management/view_storage_balance.rs @@ -32,7 +32,14 @@ impl AccountContext { move |network_config, block_reference| { let contract_account_id = (previous_context.get_contract_account_id)(network_config)?; - let storage_balance = get_storage_balance(network_config, &contract_account_id, &account_id, block_reference)?; + let storage_balance = + get_storage_balance( + previous_context.global_context.teach_me, + network_config, + &contract_account_id, + &account_id, + block_reference + )?; eprintln!("storage balance for <{account_id}>:"); eprintln!(" {:<13} {:>10} ({} [{:>28} yoctoNEAR])", "available:", @@ -78,6 +85,7 @@ impl Account { #[tracing::instrument(name = "Getting storage balance for", skip_all)] fn get_storage_balance( + teach_me: bool, network_config: &crate::config::NetworkConfig, contract_account_id: &near_primitives::types::AccountId, account_id: &crate::types::account_id::AccountId, @@ -87,6 +95,7 @@ fn get_storage_balance( network_config .json_rpc_client() .blocking_call_view_function( + teach_me, contract_account_id, "storage_balance_of", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/account/update_social_profile/sign_as.rs b/src/commands/account/update_social_profile/sign_as.rs index c3b8645ea..6647f855d 100644 --- a/src/commands/account/update_social_profile/sign_as.rs +++ b/src/commands/account/update_social_profile/sign_as.rs @@ -49,7 +49,13 @@ impl From for crate::commands::ActionContext { let get_prepopulated_transaction_after_getting_network_callback: crate::commands::GetPrepopulatedTransactionAfterGettingNetworkCallback = Arc::new({ move |network_config| { - get_prepopulated_transaction(network_config, &account_id, &signer_id, &data) + get_prepopulated_transaction( + item.global_context.teach_me, + network_config, + &account_id, + &signer_id, + &data + ) } }); @@ -149,6 +155,7 @@ impl Signer { skip_all )] fn get_prepopulated_transaction( + teach_me: bool, network_config: &crate::config::NetworkConfig, account_id: &near_primitives::types::AccountId, signer_id: &near_primitives::types::AccountId, @@ -162,8 +169,12 @@ fn get_prepopulated_transaction( }; let local_profile: serde_json::Value = serde_json::from_slice(data)?; - let remote_profile = - get_remote_profile(network_config, &contract_account_id, account_id.clone())?; + let remote_profile = get_remote_profile( + teach_me, + network_config, + &contract_account_id, + account_id.clone(), + )?; let deposit = required_deposit( &network_config.json_rpc_client(), @@ -243,6 +254,7 @@ fn get_deposit( #[tracing::instrument(name = "Getting data about a remote profile ...", skip_all)] fn get_remote_profile( + teach_me: bool, network_config: &crate::config::NetworkConfig, near_social_account_id: &near_primitives::types::AccountId, account_id: near_primitives::types::AccountId, @@ -250,6 +262,7 @@ fn get_remote_profile( match network_config .json_rpc_client() .blocking_call_view_function( + teach_me, near_social_account_id, "get", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/account/view_account_summary/mod.rs b/src/commands/account/view_account_summary/mod.rs index 3b33a2d04..2f5132b06 100644 --- a/src/commands/account/view_account_summary/mod.rs +++ b/src/commands/account/view_account_summary/mod.rs @@ -28,7 +28,11 @@ impl ViewAccountSummaryContext { let account_id: near_primitives::types::AccountId = scope.account_id.clone().into(); move |network_config, block_reference| { - get_account_inquiry(&account_id, network_config, block_reference) + get_account_inquiry( + previous_context.teach_me, + &account_id, network_config, + block_reference + ) } }); Ok(Self(crate::network_view_at_block::ArgsForViewContext { @@ -58,6 +62,7 @@ impl ViewAccountSummary { #[tracing::instrument(name = "Receiving an inquiry about your account ...", skip_all)] fn get_account_inquiry( + teach_me: bool, account_id: &near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, @@ -151,9 +156,10 @@ fn get_account_inquiry( .try_collect(), )?; - let optional_account_profile = get_account_profile(account_id, network_config, block_reference) - .ok() - .flatten(); + let optional_account_profile = + get_account_profile(teach_me, account_id, network_config, block_reference) + .ok() + .flatten(); crate::common::display_account_info( &rpc_query_response.block_hash, @@ -215,6 +221,7 @@ async fn get_delegated_staked_balance( #[tracing::instrument(name = "Getting an account profile ...", skip_all)] fn get_account_profile( + teach_me: bool, account_id: &near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, @@ -223,6 +230,7 @@ fn get_account_profile( let mut social_db = network_config .json_rpc_client() .blocking_call_view_function( + teach_me, &contract_account_id, "get", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/contract/call_function/as_read_only/mod.rs b/src/commands/contract/call_function/as_read_only/mod.rs index bc2bc4e15..ed1cd87b0 100644 --- a/src/commands/contract/call_function/as_read_only/mod.rs +++ b/src/commands/contract/call_function/as_read_only/mod.rs @@ -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( + previous_context.global_context.teach_me, + network_config, + &account_id, + &function_name, + function_args.clone(), + function_args_type.clone(), + block_reference + ) } }); @@ -111,6 +119,7 @@ impl Function { #[tracing::instrument(name = "Getting a response to a view method ...", skip_all)] fn call_view_function( + teach_me: bool, network_config: &crate::config::NetworkConfig, account_id: &near_primitives::types::AccountId, function_name: &str, @@ -121,7 +130,13 @@ fn call_view_function( let args = super::call_function_args_type::function_args(function_args, function_args_type)?; let call_result = network_config .json_rpc_client() - .blocking_call_view_function(account_id, function_name, args, block_reference.clone()) + .blocking_call_view_function( + teach_me, + account_id, + function_name, + args, + block_reference.clone(), + ) .wrap_err_with(|| { format!( "Failed to fetch query for view method: '{}' (contract <{}> on network <{}>)", diff --git a/src/commands/staking/delegate/view_balance.rs b/src/commands/staking/delegate/view_balance.rs index 2a4a34adb..de241ad78 100644 --- a/src/commands/staking/delegate/view_balance.rs +++ b/src/commands/staking/delegate/view_balance.rs @@ -30,7 +30,13 @@ impl ViewBalanceContext { let on_after_getting_block_reference_callback: crate::network_view_at_block::OnAfterGettingBlockReferenceCallback = std::sync::Arc::new({ move |network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference| { - calculation_delegated_stake_balance(&account_id, &validator_account_id, network_config, block_reference) + calculation_delegated_stake_balance( + previous_context.global_context.teach_me, + &account_id, + &validator_account_id, + network_config, + block_reference + ) } }); Ok(Self(crate::network_view_at_block::ArgsForViewContext { @@ -60,24 +66,28 @@ impl ViewBalance { skip_all )] fn calculation_delegated_stake_balance( + teach_me: bool, account_id: &near_primitives::types::AccountId, validator_account_id: &near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, ) -> crate::CliResult { let user_staked_balance: u128 = get_user_staked_balance( + teach_me, network_config, block_reference, validator_account_id, account_id, )?; let user_unstaked_balance: u128 = get_user_unstaked_balance( + teach_me, network_config, block_reference, validator_account_id, account_id, )?; let user_total_balance: u128 = get_user_total_balance( + teach_me, network_config, block_reference, validator_account_id, @@ -85,6 +95,7 @@ fn calculation_delegated_stake_balance( )?; let withdrawal_availability_message = match is_account_unstaked_balance_available_for_withdrawal( + teach_me, network_config, validator_account_id, account_id, @@ -115,6 +126,7 @@ fn calculation_delegated_stake_balance( #[tracing::instrument(name = "Getting the staked balance for the user ...", skip_all)] pub fn get_user_staked_balance( + teach_me: bool, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, validator_account_id: &near_primitives::types::AccountId, @@ -123,6 +135,7 @@ pub fn get_user_staked_balance( Ok(network_config .json_rpc_client() .blocking_call_view_function( + teach_me, validator_account_id, "get_account_staked_balance", serde_json::to_vec(&serde_json::json!({ @@ -143,6 +156,7 @@ pub fn get_user_staked_balance( #[tracing::instrument(name = "Getting the unstaked balance for the user ...", skip_all)] pub fn get_user_unstaked_balance( + teach_me: bool, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, validator_account_id: &near_primitives::types::AccountId, @@ -151,6 +165,7 @@ pub fn get_user_unstaked_balance( Ok(network_config .json_rpc_client() .blocking_call_view_function( + teach_me, validator_account_id, "get_account_unstaked_balance", serde_json::to_vec(&serde_json::json!({ @@ -171,6 +186,7 @@ pub fn get_user_unstaked_balance( #[tracing::instrument(name = "Getting the total balance for the user ...", skip_all)] pub fn get_user_total_balance( + teach_me: bool, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, validator_account_id: &near_primitives::types::AccountId, @@ -179,6 +195,7 @@ pub fn get_user_total_balance( Ok(network_config .json_rpc_client() .blocking_call_view_function( + teach_me, validator_account_id, "get_account_total_balance", serde_json::to_vec(&serde_json::json!({ @@ -202,6 +219,7 @@ pub fn get_user_total_balance( skip_all )] pub fn is_account_unstaked_balance_available_for_withdrawal( + teach_me: bool, network_config: &crate::config::NetworkConfig, validator_account_id: &near_primitives::types::AccountId, account_id: &near_primitives::types::AccountId, @@ -209,6 +227,7 @@ pub fn is_account_unstaked_balance_available_for_withdrawal( network_config .json_rpc_client() .blocking_call_view_function( + teach_me, validator_account_id, "is_account_unstaked_balance_available", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/tokens/send_ft/amount_ft.rs b/src/commands/tokens/send_ft/amount_ft.rs index 6257470f1..b0712f57c 100644 --- a/src/commands/tokens/send_ft/amount_ft.rs +++ b/src/commands/tokens/send_ft/amount_ft.rs @@ -42,6 +42,7 @@ impl AmountFtContext { ) })?; let ft_metadata = crate::types::ft_properties::params_ft_metadata( + previous_context.global_context.teach_me, previous_context.ft_contract_account_id.clone(), &network_config, near_primitives::types::Finality::Final.into(), @@ -73,6 +74,7 @@ impl AmountFt { })?; let ft_metadata = crate::types::ft_properties::params_ft_metadata( + context.global_context.teach_me, context.ft_contract_account_id.clone(), &network_config, near_primitives::types::Finality::Final.into(), @@ -190,6 +192,7 @@ impl DepositContext { move |network_config| { get_prepopulated_transaction( + previous_context.global_context.teach_me, network_config, &ft_contract_account_id, &receiver_account_id, @@ -260,6 +263,7 @@ impl Deposit { skip_all )] fn get_prepopulated_transaction( + teach_me: bool, network_config: &crate::config::NetworkConfig, ft_contract_account_id: &near_primitives::types::AccountId, receiver_account_id: &near_primitives::types::AccountId, @@ -285,6 +289,7 @@ fn get_prepopulated_transaction( let call_result = network_config .json_rpc_client() .blocking_call_view_function( + teach_me, ft_contract_account_id, "storage_balance_of", args.clone(), diff --git a/src/commands/tokens/view_ft_balance/mod.rs b/src/commands/tokens/view_ft_balance/mod.rs index ec7e3a677..339ae3eee 100644 --- a/src/commands/tokens/view_ft_balance/mod.rs +++ b/src/commands/tokens/view_ft_balance/mod.rs @@ -31,6 +31,7 @@ impl ViewFtBalanceContext { move |network_config, block_reference| { let crate::types::ft_properties::FtMetadata { decimals, symbol } = crate::types::ft_properties::params_ft_metadata( + previous_context.global_context.teach_me, ft_contract_account_id.clone(), network_config, block_reference.clone(), @@ -38,7 +39,14 @@ impl ViewFtBalanceContext { let args = serde_json::to_vec(&json!({ "account_id": owner_account_id.to_string(), }))?; - let call_result = get_ft_balance(network_config, &ft_contract_account_id, args, block_reference.clone())?; + let call_result = + get_ft_balance( + previous_context.global_context.teach_me, + network_config, + &ft_contract_account_id, + args, + block_reference.clone() + )?; call_result.print_logs(); let amount: String = call_result.parse_result_from_json()?; let fungible_token = crate::types::ft_properties::FungibleToken::from_params_ft( @@ -83,6 +91,7 @@ impl ViewFtBalance { #[tracing::instrument(name = "Getting FT balance ...", skip_all)] fn get_ft_balance( + teach_me: bool, network_config: &crate::config::NetworkConfig, ft_contract_account_id: &near_primitives::types::AccountId, args: Vec, @@ -91,6 +100,7 @@ fn get_ft_balance( network_config .json_rpc_client() .blocking_call_view_function( + teach_me, ft_contract_account_id, "ft_balance_of", args, diff --git a/src/commands/tokens/view_nft_assets/mod.rs b/src/commands/tokens/view_nft_assets/mod.rs index b4aaf2e5d..3791f7f97 100644 --- a/src/commands/tokens/view_nft_assets/mod.rs +++ b/src/commands/tokens/view_nft_assets/mod.rs @@ -33,7 +33,14 @@ impl ViewNftAssetsContext { let args = serde_json::to_vec(&json!({ "account_id": owner_account_id.to_string(), }))?; - let call_result = get_nft_balance(network_config, &nft_contract_account_id, args, block_reference.clone())?; + let call_result = + get_nft_balance( + previous_context.global_context.teach_me, + network_config, + &nft_contract_account_id, + args, + block_reference.clone() + )?; call_result.print_logs(); let serde_call_result: serde_json::Value = call_result.parse_result_from_json()?; @@ -72,6 +79,7 @@ impl ViewNftAssets { #[tracing::instrument(name = "Getting NFT balance ...", skip_all)] fn get_nft_balance( + teach_me: bool, network_config: &crate::config::NetworkConfig, nft_contract_account_id: &near_primitives::types::AccountId, args: Vec, @@ -80,6 +88,7 @@ fn get_nft_balance( network_config .json_rpc_client() .blocking_call_view_function( + teach_me, nft_contract_account_id, "nft_tokens_for_owner", args, diff --git a/src/common.rs b/src/common.rs index 323af4472..41ca52d71 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2011,6 +2011,7 @@ pub trait JsonRpcClientExt { /// arguments and function return value. fn blocking_call_view_function( &self, + teach_me: bool, account_id: &near_primitives::types::AccountId, method_name: &str, args: Vec, @@ -2070,6 +2071,7 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { #[tracing::instrument(name = "Getting the result of executing", skip_all)] fn blocking_call_view_function( &self, + teach_me: bool, account_id: &near_primitives::types::AccountId, method_name: &str, args: Vec, @@ -2078,16 +2080,27 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::Span::current().pb_set_message(&format!( "the '{method_name}' method of the <{account_id}> contract ..." )); + let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { + block_reference, + request: near_primitives::views::QueryRequest::CallFunction { + account_id: account_id.clone(), + method_name: method_name.to_owned(), + args: near_primitives::types::FunctionArgs::from(args), + }, + }; + let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request)?; + let query_view_method_response = self - .blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest { - block_reference, - request: near_primitives::views::QueryRequest::CallFunction { - account_id: account_id.clone(), - method_name: method_name.to_owned(), - args: near_primitives::types::FunctionArgs::from(args), - }, - }) + .blocking_call(query_view_method_request) .wrap_err("Failed to make a view-function call")?; + + let response_payload = serde_json::to_value(&query_view_method_response)?; + + if teach_me { + eprintln!("\nJSON-BODY:\n{:#}", request_payload); + eprintln!("RESPONSE:\n{}", response_payload); + } + query_view_method_response.call_result() } diff --git a/src/types/ft_properties.rs b/src/types/ft_properties.rs index 8c6c5198b..6683af35b 100644 --- a/src/types/ft_properties.rs +++ b/src/types/ft_properties.rs @@ -153,6 +153,7 @@ pub struct FtMetadata { #[tracing::instrument(name = "Getting FT metadata ...", skip_all)] pub fn params_ft_metadata( + teach_me: bool, ft_contract_account_id: near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: near_primitives::types::BlockReference, @@ -160,6 +161,7 @@ pub fn params_ft_metadata( let ft_metadata: FtMetadata = network_config .json_rpc_client() .blocking_call_view_function( + teach_me, &ft_contract_account_id, "ft_metadata", vec![], From 096f7acc04930f560c0038013d931d49c75026af Mon Sep 17 00:00:00 2001 From: FroVolod Date: Thu, 4 Jul 2024 09:24:08 +0300 Subject: [PATCH 03/13] clippy --- src/commands/tokens/send_ft/amount_ft.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/commands/tokens/send_ft/amount_ft.rs b/src/commands/tokens/send_ft/amount_ft.rs index b0712f57c..cccc022de 100644 --- a/src/commands/tokens/send_ft/amount_ft.rs +++ b/src/commands/tokens/send_ft/amount_ft.rs @@ -1,3 +1,5 @@ +#![allow(clippy::too_many_arguments)] + use color_eyre::eyre::{Context, ContextCompat}; use inquire::CustomType; use serde_json::{json, Value}; From 5fbd60b7b8c255d8efc258bb8bd1304e57b275b2 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sun, 14 Jul 2024 11:24:56 +0300 Subject: [PATCH 04/13] draft --- src/common.rs | 29 +- src/main.rs | 25 +- .../sign_with_legacy_keychain/mod.rs | 608 ++++++++++++------ 3 files changed, 454 insertions(+), 208 deletions(-) diff --git a/src/common.rs b/src/common.rs index 41ca52d71..012463eee 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2117,13 +2117,38 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { >, > { tracing::Span::current().pb_set_message(&format!("{public_key} ...")); - self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest { + + let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, request: near_primitives::views::QueryRequest::ViewAccessKey { account_id: account_id.clone(), public_key: public_key.clone(), }, - }) + }; + let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request).map_err(|err| { + near_jsonrpc_client::errors::JsonRpcError::TransportError(near_jsonrpc_client::errors::RpcTransportError::SendError( + near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(err), + )) + })?; + + let query_view_method_response = self.blocking_call(query_view_method_request)?; + + let response_payload = serde_json::to_value(&query_view_method_response).map_err(|err| { + near_jsonrpc_client::errors::JsonRpcError::TransportError(near_jsonrpc_client::errors::RpcTransportError::SendError( + near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(err.into()), + )) + })?; + + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting access key information for public_key: {}\nHTTP POST {}\nJSON-BODY:\n{:#}\nRESPONSE:\n{:#}", + public_key, + self.server_addr(), + request_payload, + response_payload + ); + + Ok(query_view_method_response) } #[tracing::instrument(name = "Getting a list of", skip_all)] diff --git a/src/main.rs b/src/main.rs index 6804b1221..1c4d782cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,15 +92,6 @@ fn main() -> crate::common::CliResult { ]), ) .with_span_child_prefix_symbol("↳ "); - tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer())) - .with(indicatif_layer) - .with( - EnvFilter::from_default_env() - .add_directive(tracing::Level::WARN.into()) - .add_directive("near_cli_rs=info".parse()?), - ) - .init(); #[cfg(feature = "self-update")] let handle = std::thread::spawn(|| -> color_eyre::eyre::Result { @@ -161,6 +152,22 @@ fn main() -> crate::common::CliResult { error.exit(); } }; + let env_filter = if cli.teach_me { + EnvFilter::from_default_env() + .add_directive(tracing::Level::WARN.into()) + .add_directive("near_teach_me=info".parse()?) + } else { + EnvFilter::from_default_env() + .add_directive(tracing::Level::WARN.into()) + .add_directive("near_cli_rs=info".parse()?) + }; + + + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer())) + .with(indicatif_layer) + .with(env_filter) + .init(); let cli_cmd = match ::from_cli(Some(cli), (config,)) { interactive_clap::ResultFromCli::Ok(cli_cmd) diff --git a/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs b/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs index fcfe7d601..62aa0278a 100644 --- a/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs +++ b/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs @@ -44,214 +44,214 @@ pub struct SignLegacyKeychainContext { } impl SignLegacyKeychainContext { - #[tracing::instrument( - name = "Signing the transaction with a key saved in legacy keychain ...", - skip_all - )] pub fn from_previous_context( previous_context: crate::commands::TransactionContext, scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { - let network_config = previous_context.network_config.clone(); + // let network_config = previous_context.network_config.clone(); - let file_name = format!( - "{}.json", - &previous_context.prepopulated_transaction.signer_id - ); - let mut path = - std::path::PathBuf::from(&previous_context.global_context.config.credentials_home_dir); + // let file_name = format!( + // "{}.json", + // &previous_context.prepopulated_transaction.signer_id + // ); + // let mut path = + // std::path::PathBuf::from(&previous_context.global_context.config.credentials_home_dir); - let data_path: std::path::PathBuf = { - let dir_name = network_config.network_name.clone(); - path.push(&dir_name); + // { + // let dir_name = network_config.network_name.clone(); + // path.push(&dir_name); - if previous_context.global_context.offline { - path.push( - previous_context - .prepopulated_transaction - .signer_id - .to_string(), - ); - path.push(&format!( - "{}.json", - scope - .signer_public_key - .clone() - .wrap_err( - "Signer public key is required to sign a transaction in offline mode" - )? - .to_string() - .replace(':', "_") - )); - path - } else { - path.push(file_name); - if path.exists() { - path - } else { - let access_key_list = network_config - .json_rpc_client() - .blocking_call_view_access_key_list( - &previous_context.prepopulated_transaction.signer_id, - near_primitives::types::Finality::Final.into(), - ) - .wrap_err_with(|| { - format!( - "Failed to fetch access KeyList for {}", - previous_context.prepopulated_transaction.signer_id - ) - })? - .access_key_list_view()?; - let mut path = std::path::PathBuf::from( - &previous_context.global_context.config.credentials_home_dir, - ); - path.push(dir_name); - path.push( - &previous_context - .prepopulated_transaction - .signer_id - .to_string(), - ); - let mut data_path = std::path::PathBuf::new(); - 'outer: for access_key in access_key_list.keys { - let account_public_key = access_key.public_key.to_string(); - let is_full_access_key: bool = match &access_key.access_key.permission { - near_primitives::views::AccessKeyPermissionView::FullAccess => true, - near_primitives::views::AccessKeyPermissionView::FunctionCall { - allowance: _, - receiver_id: _, - method_names: _, - } => false, - }; - let dir = path - .read_dir() - .wrap_err("There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain.")?; - for entry in dir { - if let Ok(entry) = entry { - if entry - .path() - .file_stem() - .unwrap() - .to_str() - .unwrap() - .contains(account_public_key.rsplit(':').next().unwrap()) - && is_full_access_key - { - data_path.push(entry.path()); - break 'outer; - } - } else { - return Err(color_eyre::Report::msg( - "There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain." - )); - }; - } - } - data_path - } - } - }; - let data = std::fs::read_to_string(&data_path).wrap_err_with(|| { - format!( - "Access key file for account <{}> on network <{}> not found!", - previous_context.prepopulated_transaction.signer_id, network_config.network_name - ) - })?; - let account_json: super::AccountKeyPair = serde_json::from_str(&data) - .wrap_err_with(|| format!("Error reading data from file: {:?}", &data_path))?; - - let (nonce, block_hash, block_height) = if previous_context.global_context.offline { - ( - scope - .nonce - .wrap_err("Nonce is required to sign a transaction in offline mode")?, - scope - .block_hash - .wrap_err("Block Hash is required to sign a transaction in offline mode")? - .0, - scope - .block_height - .wrap_err("Block Height is required to sign a transaction in offline mode")?, - ) - } else { - let rpc_query_response = network_config - .json_rpc_client() - .blocking_call_view_access_key( - &previous_context.prepopulated_transaction.signer_id, - &account_json.public_key, - near_primitives::types::BlockReference::latest() - ) - .wrap_err( - "Cannot sign a transaction due to an error while fetching the most recent nonce value", - )?; - ( - rpc_query_response - .access_key_view() - .wrap_err("Error current_nonce")? - .nonce - + 1, - rpc_query_response.block_hash, - rpc_query_response.block_height, - ) - }; - - let mut unsigned_transaction = near_primitives::transaction::Transaction { - public_key: account_json.public_key.clone(), - block_hash, - nonce, - signer_id: previous_context.prepopulated_transaction.signer_id.clone(), - receiver_id: previous_context.prepopulated_transaction.receiver_id, - actions: previous_context.prepopulated_transaction.actions, - }; - - (previous_context.on_before_signing_callback)(&mut unsigned_transaction, &network_config)?; - - if network_config.meta_transaction_relayer_url.is_some() { - let max_block_height = block_height - + scope - .meta_transaction_valid_for - .unwrap_or(super::META_TRANSACTION_VALID_FOR_DEFAULT); - - let signed_delegate_action = super::get_signed_delegate_action( - unsigned_transaction, - &account_json.public_key, - account_json.private_key, - max_block_height, - ); - - return Ok(Self { - network_config: previous_context.network_config, - global_context: previous_context.global_context, - signed_transaction_or_signed_delegate_action: signed_delegate_action.into(), - on_before_sending_transaction_callback: previous_context - .on_before_sending_transaction_callback, - on_after_sending_transaction_callback: previous_context - .on_after_sending_transaction_callback, - }); - } + // if previous_context.global_context.offline { + // path.push( + // previous_context + // .prepopulated_transaction + // .signer_id + // .to_string(), + // ); + // path.push(&format!( + // "{}.json", + // scope + // .signer_public_key + // .clone() + // .wrap_err( + // "Signer public key is required to sign a transaction in offline mode" + // )? + // .to_string() + // .replace(':', "_") + // )); + // path + // } else { + // path.push(file_name); + // if path.exists() { + // path + // } else { + // println!("----------------"); + // let access_key_list = network_config + // .json_rpc_client() + // .blocking_call_view_access_key_list( + // &previous_context.prepopulated_transaction.signer_id, + // near_primitives::types::Finality::Final.into(), + // ) + // .wrap_err_with(|| { + // format!( + // "Failed to fetch access KeyList for {}", + // previous_context.prepopulated_transaction.signer_id + // ) + // })? + // .access_key_list_view()?; + // let mut path = std::path::PathBuf::from( + // &previous_context.global_context.config.credentials_home_dir, + // ); + // path.push(dir_name); + // path.push( + // &previous_context + // .prepopulated_transaction + // .signer_id + // .to_string(), + // ); + // let mut data_path = std::path::PathBuf::new(); + // 'outer: for access_key in access_key_list.keys { + // let account_public_key = access_key.public_key.to_string(); + // let is_full_access_key: bool = match &access_key.access_key.permission { + // near_primitives::views::AccessKeyPermissionView::FullAccess => true, + // near_primitives::views::AccessKeyPermissionView::FunctionCall { + // allowance: _, + // receiver_id: _, + // method_names: _, + // } => false, + // }; + // let dir = path + // .read_dir() + // .wrap_err("There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain.")?; + // for entry in dir { + // if let Ok(entry) = entry { + // if entry + // .path() + // .file_stem() + // .unwrap() + // .to_str() + // .unwrap() + // .contains(account_public_key.rsplit(':').next().unwrap()) + // && is_full_access_key + // { + // data_path.push(entry.path()); + // break 'outer; + // } + // } else { + // return Err(color_eyre::Report::msg( + // "There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain." + // )); + // }; + // } + // } + // data_path + // } + // } + // }; + // let data = std::fs::read_to_string(&data_path).wrap_err_with(|| { + // format!( + // "Access key file for account <{}> on network <{}> not found!", + // previous_context.prepopulated_transaction.signer_id, network_config.network_name + // ) + // })?; + // let account_json: super::AccountKeyPair = serde_json::from_str(&data) + // .wrap_err_with(|| format!("Error reading data from file: {:?}", &data_path))?; - let signature = account_json - .private_key - .sign(unsigned_transaction.get_hash_and_size().0.as_ref()); + // let (nonce, block_hash, block_height) = if previous_context.global_context.offline { + // ( + // scope + // .nonce + // .wrap_err("Nonce is required to sign a transaction in offline mode")?, + // scope + // .block_hash + // .wrap_err("Block Hash is required to sign a transaction in offline mode")? + // .0, + // scope + // .block_height + // .wrap_err("Block Height is required to sign a transaction in offline mode")?, + // ) + // } else { + // println!("+++++++++++++++++"); + // let rpc_query_response = network_config + // .json_rpc_client() + // .blocking_call_view_access_key( + // previous_context.global_context.teach_me, + // &previous_context.prepopulated_transaction.signer_id, + // &account_json.public_key, + // near_primitives::types::BlockReference::latest() + // ) + // .wrap_err( + // "Cannot sign a transaction due to an error while fetching the most recent nonce value", + // )?; + // ( + // rpc_query_response + // .access_key_view() + // .wrap_err("Error current_nonce")? + // .nonce + // + 1, + // rpc_query_response.block_hash, + // rpc_query_response.block_height, + // ) + // }; - let signed_transaction = near_primitives::transaction::SignedTransaction::new( - signature.clone(), - unsigned_transaction, - ); + // let mut unsigned_transaction = near_primitives::transaction::Transaction { + // public_key: account_json.public_key.clone(), + // block_hash, + // nonce, + // signer_id: previous_context.prepopulated_transaction.signer_id.clone(), + // receiver_id: previous_context.prepopulated_transaction.receiver_id, + // actions: previous_context.prepopulated_transaction.actions, + // }; - eprintln!("\nYour transaction was signed successfully."); - eprintln!("Public key: {}", account_json.public_key); - eprintln!("Signature: {}", signature); + // (previous_context.on_before_signing_callback)(&mut unsigned_transaction, &network_config)?; - Ok(Self { - network_config: previous_context.network_config, - global_context: previous_context.global_context, - signed_transaction_or_signed_delegate_action: signed_transaction.into(), - on_before_sending_transaction_callback: previous_context - .on_before_sending_transaction_callback, - on_after_sending_transaction_callback: previous_context - .on_after_sending_transaction_callback, - }) + // if network_config.meta_transaction_relayer_url.is_some() { + // let max_block_height = block_height + // + scope + // .meta_transaction_valid_for + // .unwrap_or(super::META_TRANSACTION_VALID_FOR_DEFAULT); + + // let signed_delegate_action = super::get_signed_delegate_action( + // unsigned_transaction, + // &account_json.public_key, + // account_json.private_key, + // max_block_height, + // ); + + // return Ok(Self { + // network_config: previous_context.network_config, + // global_context: previous_context.global_context, + // signed_transaction_or_signed_delegate_action: signed_delegate_action.into(), + // on_before_sending_transaction_callback: previous_context + // .on_before_sending_transaction_callback, + // on_after_sending_transaction_callback: previous_context + // .on_after_sending_transaction_callback, + // }); + // } + + // let signature = account_json + // .private_key + // .sign(unsigned_transaction.get_hash_and_size().0.as_ref()); + + // let signed_transaction = near_primitives::transaction::SignedTransaction::new( + // signature.clone(), + // unsigned_transaction, + // ); + + // eprintln!("\nYour transaction was signed successfully."); + // eprintln!("Public key: {}", account_json.public_key); + // eprintln!("Signature: {}", signature); + + // Ok(Self { + // network_config: previous_context.network_config, + // global_context: previous_context.global_context, + // signed_transaction_or_signed_delegate_action: signed_transaction.into(), + // on_before_sending_transaction_callback: previous_context + // .on_before_sending_transaction_callback, + // on_after_sending_transaction_callback: previous_context + // .on_after_sending_transaction_callback, + // }) + qwe(previous_context, scope) } } @@ -340,3 +340,217 @@ impl SignLegacyKeychain { Ok(None) } } + +#[tracing::instrument( + name = "Signing the transaction with a key saved in legacy keychain ...", + skip_all, + // level = "debug" +)] +fn qwe( + previous_context: crate::commands::TransactionContext, + scope: &::InteractiveClapContextScope, +) -> color_eyre::eyre::Result { + let network_config = previous_context.network_config.clone(); + + let file_name = format!( + "{}.json", + &previous_context.prepopulated_transaction.signer_id + ); + let mut path = + std::path::PathBuf::from(&previous_context.global_context.config.credentials_home_dir); + + let data_path: std::path::PathBuf = get_data_path( + &network_config, + &previous_context.prepopulated_transaction.signer_id, + scope.signer_public_key.clone(), + &previous_context.global_context.config.credentials_home_dir, + previous_context.global_context.offline, + )?; + + let data = std::fs::read_to_string(&data_path).wrap_err_with(|| { + format!( + "Access key file for account <{}> on network <{}> not found!", + previous_context.prepopulated_transaction.signer_id, network_config.network_name + ) + })?; + let account_json: super::AccountKeyPair = serde_json::from_str(&data) + .wrap_err_with(|| format!("Error reading data from file: {:?}", &data_path))?; + + let (nonce, block_hash, block_height) = if previous_context.global_context.offline { + ( + scope + .nonce + .wrap_err("Nonce is required to sign a transaction in offline mode")?, + scope + .block_hash + .wrap_err("Block Hash is required to sign a transaction in offline mode")? + .0, + scope + .block_height + .wrap_err("Block Height is required to sign a transaction in offline mode")?, + ) + } else { + let rpc_query_response = network_config + .json_rpc_client() + .blocking_call_view_access_key( + &previous_context.prepopulated_transaction.signer_id, + &account_json.public_key, + near_primitives::types::BlockReference::latest() + ) + .wrap_err( + "Cannot sign a transaction due to an error while fetching the most recent nonce value", + )?; + ( + rpc_query_response + .access_key_view() + .wrap_err("Error current_nonce")? + .nonce + + 1, + rpc_query_response.block_hash, + rpc_query_response.block_height, + ) + }; + + let mut unsigned_transaction = near_primitives::transaction::Transaction { + public_key: account_json.public_key.clone(), + block_hash, + nonce, + signer_id: previous_context.prepopulated_transaction.signer_id.clone(), + receiver_id: previous_context.prepopulated_transaction.receiver_id, + actions: previous_context.prepopulated_transaction.actions, + }; + + (previous_context.on_before_signing_callback)(&mut unsigned_transaction, &network_config)?; + + if network_config.meta_transaction_relayer_url.is_some() { + let max_block_height = block_height + + scope + .meta_transaction_valid_for + .unwrap_or(super::META_TRANSACTION_VALID_FOR_DEFAULT); + + let signed_delegate_action = super::get_signed_delegate_action( + unsigned_transaction, + &account_json.public_key, + account_json.private_key, + max_block_height, + ); + + return Ok(SignLegacyKeychainContext { + network_config: previous_context.network_config, + global_context: previous_context.global_context, + signed_transaction_or_signed_delegate_action: signed_delegate_action.into(), + on_before_sending_transaction_callback: previous_context + .on_before_sending_transaction_callback, + on_after_sending_transaction_callback: previous_context + .on_after_sending_transaction_callback, + }); + } + + let signature = account_json + .private_key + .sign(unsigned_transaction.get_hash_and_size().0.as_ref()); + + let signed_transaction = near_primitives::transaction::SignedTransaction::new( + signature.clone(), + unsigned_transaction, + ); + + eprintln!("\nYour transaction was signed successfully."); + eprintln!("Public key: {}", account_json.public_key); + eprintln!("Signature: {}", signature); + + Ok(SignLegacyKeychainContext { + network_config: previous_context.network_config, + global_context: previous_context.global_context, + signed_transaction_or_signed_delegate_action: signed_transaction.into(), + on_before_sending_transaction_callback: previous_context + .on_before_sending_transaction_callback, + on_after_sending_transaction_callback: previous_context + .on_after_sending_transaction_callback, + }) +} + +#[tracing::instrument( + // name = "Signing the transaction with a key saved in legacy keychain ...", + skip_all +)] +fn get_data_path( + network_config: &crate::config::NetworkConfig, + signer_id: &near_primitives::types::AccountId, + signer_public_key: Option, + credentials_home_dir: &std::path::PathBuf, + offline: bool, +) -> color_eyre::eyre::Result { + let file_name = format!("{signer_id}.json"); + let mut path = std::path::PathBuf::from(credentials_home_dir); + + let dir_name: &str = &network_config.network_name; + path.push(dir_name); + + if offline { + path.push(signer_id.as_str()); + path.push(&format!( + "{}.json", + signer_public_key + .clone() + .wrap_err("Signer public key is required to sign a transaction in offline mode")? + .to_string() + .replace(':', "_") + )); + Ok(path) + } else { + path.push(file_name); + if path.exists() { + Ok(path) + } else { + println!("----------------"); + let access_key_list = network_config + .json_rpc_client() + .blocking_call_view_access_key_list( + signer_id, + near_primitives::types::Finality::Final.into(), + ) + .wrap_err_with(|| format!("Failed to fetch access KeyList for {}", signer_id))? + .access_key_list_view()?; + let mut path = std::path::PathBuf::from(&credentials_home_dir); + path.push(dir_name); + path.push(signer_id.to_string()); + let mut data_path = std::path::PathBuf::new(); + 'outer: for access_key in access_key_list.keys { + let account_public_key = access_key.public_key.to_string(); + let is_full_access_key: bool = match &access_key.access_key.permission { + near_primitives::views::AccessKeyPermissionView::FullAccess => true, + near_primitives::views::AccessKeyPermissionView::FunctionCall { + allowance: _, + receiver_id: _, + method_names: _, + } => false, + }; + let dir = path + .read_dir() + .wrap_err("There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain.")?; + for entry in dir { + if let Ok(entry) = entry { + if entry + .path() + .file_stem() + .unwrap() + .to_str() + .unwrap() + .contains(account_public_key.rsplit(':').next().unwrap()) + && is_full_access_key + { + data_path.push(entry.path()); + break 'outer; + } + } else { + return Err(color_eyre::Report::msg( + "There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain." + )); + }; + } + } + Ok(data_path) + } + } +} From 8bf09500976e7b5e50d29a5f8528657a78a45284 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Thu, 18 Jul 2024 11:37:53 +0300 Subject: [PATCH 05/13] refactored tracing_subscriber::registry() --- src/main.rs | 61 +- .../sign_with_legacy_keychain/mod.rs | 608 ++++++------------ 2 files changed, 228 insertions(+), 441 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1c4d782cb..75e3b776f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,24 +75,6 @@ fn main() -> crate::common::CliResult { color_eyre::install()?; - let indicatif_layer = IndicatifLayer::new() - .with_progress_style( - ProgressStyle::with_template( - "{spinner:.blue}{span_child_prefix} {span_name} {msg} {span_fields}", - ) - .unwrap() - .tick_strings(&[ - "▹▹▹▹▹", - "▸▹▹▹▹", - "▹▸▹▹▹", - "▹▹▸▹▹", - "▹▹▹▸▹", - "▹▹▹▹▸", - "▪▪▪▪▪", - ]), - ) - .with_span_child_prefix_symbol("↳ "); - #[cfg(feature = "self-update")] let handle = std::thread::spawn(|| -> color_eyre::eyre::Result { crate::commands::extensions::self_update::get_latest_version() @@ -152,23 +134,42 @@ fn main() -> crate::common::CliResult { error.exit(); } }; - let env_filter = if cli.teach_me { - EnvFilter::from_default_env() + if cli.teach_me { + let env_filter = EnvFilter::from_default_env() .add_directive(tracing::Level::WARN.into()) - .add_directive("near_teach_me=info".parse()?) + .add_directive("near=info".parse()?); + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + .with(env_filter) + .init(); } else { - EnvFilter::from_default_env() + let indicatif_layer = IndicatifLayer::new() + .with_progress_style( + ProgressStyle::with_template( + "{spinner:.blue}{span_child_prefix} {span_name} {msg} {span_fields}", + ) + .unwrap() + .tick_strings(&[ + "▹▹▹▹▹", + "▸▹▹▹▹", + "▹▸▹▹▹", + "▹▹▸▹▹", + "▹▹▹▸▹", + "▹▹▹▹▸", + "▪▪▪▪▪", + ]), + ) + .with_span_child_prefix_symbol("↳ "); + let env_filter = EnvFilter::from_default_env() .add_directive(tracing::Level::WARN.into()) - .add_directive("near_cli_rs=info".parse()?) + .add_directive("near_cli_rs=info".parse()?); + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer())) + .with(indicatif_layer) + .with(env_filter) + .init(); }; - - tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer())) - .with(indicatif_layer) - .with(env_filter) - .init(); - let cli_cmd = match ::from_cli(Some(cli), (config,)) { interactive_clap::ResultFromCli::Ok(cli_cmd) | interactive_clap::ResultFromCli::Cancel(Some(cli_cmd)) => { diff --git a/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs b/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs index 62aa0278a..fcfe7d601 100644 --- a/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs +++ b/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs @@ -44,214 +44,214 @@ pub struct SignLegacyKeychainContext { } impl SignLegacyKeychainContext { + #[tracing::instrument( + name = "Signing the transaction with a key saved in legacy keychain ...", + skip_all + )] pub fn from_previous_context( previous_context: crate::commands::TransactionContext, scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { - // let network_config = previous_context.network_config.clone(); + let network_config = previous_context.network_config.clone(); - // let file_name = format!( - // "{}.json", - // &previous_context.prepopulated_transaction.signer_id - // ); - // let mut path = - // std::path::PathBuf::from(&previous_context.global_context.config.credentials_home_dir); - - // { - // let dir_name = network_config.network_name.clone(); - // path.push(&dir_name); - - // if previous_context.global_context.offline { - // path.push( - // previous_context - // .prepopulated_transaction - // .signer_id - // .to_string(), - // ); - // path.push(&format!( - // "{}.json", - // scope - // .signer_public_key - // .clone() - // .wrap_err( - // "Signer public key is required to sign a transaction in offline mode" - // )? - // .to_string() - // .replace(':', "_") - // )); - // path - // } else { - // path.push(file_name); - // if path.exists() { - // path - // } else { - // println!("----------------"); - // let access_key_list = network_config - // .json_rpc_client() - // .blocking_call_view_access_key_list( - // &previous_context.prepopulated_transaction.signer_id, - // near_primitives::types::Finality::Final.into(), - // ) - // .wrap_err_with(|| { - // format!( - // "Failed to fetch access KeyList for {}", - // previous_context.prepopulated_transaction.signer_id - // ) - // })? - // .access_key_list_view()?; - // let mut path = std::path::PathBuf::from( - // &previous_context.global_context.config.credentials_home_dir, - // ); - // path.push(dir_name); - // path.push( - // &previous_context - // .prepopulated_transaction - // .signer_id - // .to_string(), - // ); - // let mut data_path = std::path::PathBuf::new(); - // 'outer: for access_key in access_key_list.keys { - // let account_public_key = access_key.public_key.to_string(); - // let is_full_access_key: bool = match &access_key.access_key.permission { - // near_primitives::views::AccessKeyPermissionView::FullAccess => true, - // near_primitives::views::AccessKeyPermissionView::FunctionCall { - // allowance: _, - // receiver_id: _, - // method_names: _, - // } => false, - // }; - // let dir = path - // .read_dir() - // .wrap_err("There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain.")?; - // for entry in dir { - // if let Ok(entry) = entry { - // if entry - // .path() - // .file_stem() - // .unwrap() - // .to_str() - // .unwrap() - // .contains(account_public_key.rsplit(':').next().unwrap()) - // && is_full_access_key - // { - // data_path.push(entry.path()); - // break 'outer; - // } - // } else { - // return Err(color_eyre::Report::msg( - // "There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain." - // )); - // }; - // } - // } - // data_path - // } - // } - // }; - // let data = std::fs::read_to_string(&data_path).wrap_err_with(|| { - // format!( - // "Access key file for account <{}> on network <{}> not found!", - // previous_context.prepopulated_transaction.signer_id, network_config.network_name - // ) - // })?; - // let account_json: super::AccountKeyPair = serde_json::from_str(&data) - // .wrap_err_with(|| format!("Error reading data from file: {:?}", &data_path))?; - - // let (nonce, block_hash, block_height) = if previous_context.global_context.offline { - // ( - // scope - // .nonce - // .wrap_err("Nonce is required to sign a transaction in offline mode")?, - // scope - // .block_hash - // .wrap_err("Block Hash is required to sign a transaction in offline mode")? - // .0, - // scope - // .block_height - // .wrap_err("Block Height is required to sign a transaction in offline mode")?, - // ) - // } else { - // println!("+++++++++++++++++"); - // let rpc_query_response = network_config - // .json_rpc_client() - // .blocking_call_view_access_key( - // previous_context.global_context.teach_me, - // &previous_context.prepopulated_transaction.signer_id, - // &account_json.public_key, - // near_primitives::types::BlockReference::latest() - // ) - // .wrap_err( - // "Cannot sign a transaction due to an error while fetching the most recent nonce value", - // )?; - // ( - // rpc_query_response - // .access_key_view() - // .wrap_err("Error current_nonce")? - // .nonce - // + 1, - // rpc_query_response.block_hash, - // rpc_query_response.block_height, - // ) - // }; - - // let mut unsigned_transaction = near_primitives::transaction::Transaction { - // public_key: account_json.public_key.clone(), - // block_hash, - // nonce, - // signer_id: previous_context.prepopulated_transaction.signer_id.clone(), - // receiver_id: previous_context.prepopulated_transaction.receiver_id, - // actions: previous_context.prepopulated_transaction.actions, - // }; - - // (previous_context.on_before_signing_callback)(&mut unsigned_transaction, &network_config)?; - - // if network_config.meta_transaction_relayer_url.is_some() { - // let max_block_height = block_height - // + scope - // .meta_transaction_valid_for - // .unwrap_or(super::META_TRANSACTION_VALID_FOR_DEFAULT); + let file_name = format!( + "{}.json", + &previous_context.prepopulated_transaction.signer_id + ); + let mut path = + std::path::PathBuf::from(&previous_context.global_context.config.credentials_home_dir); - // let signed_delegate_action = super::get_signed_delegate_action( - // unsigned_transaction, - // &account_json.public_key, - // account_json.private_key, - // max_block_height, - // ); + let data_path: std::path::PathBuf = { + let dir_name = network_config.network_name.clone(); + path.push(&dir_name); - // return Ok(Self { - // network_config: previous_context.network_config, - // global_context: previous_context.global_context, - // signed_transaction_or_signed_delegate_action: signed_delegate_action.into(), - // on_before_sending_transaction_callback: previous_context - // .on_before_sending_transaction_callback, - // on_after_sending_transaction_callback: previous_context - // .on_after_sending_transaction_callback, - // }); - // } + if previous_context.global_context.offline { + path.push( + previous_context + .prepopulated_transaction + .signer_id + .to_string(), + ); + path.push(&format!( + "{}.json", + scope + .signer_public_key + .clone() + .wrap_err( + "Signer public key is required to sign a transaction in offline mode" + )? + .to_string() + .replace(':', "_") + )); + path + } else { + path.push(file_name); + if path.exists() { + path + } else { + let access_key_list = network_config + .json_rpc_client() + .blocking_call_view_access_key_list( + &previous_context.prepopulated_transaction.signer_id, + near_primitives::types::Finality::Final.into(), + ) + .wrap_err_with(|| { + format!( + "Failed to fetch access KeyList for {}", + previous_context.prepopulated_transaction.signer_id + ) + })? + .access_key_list_view()?; + let mut path = std::path::PathBuf::from( + &previous_context.global_context.config.credentials_home_dir, + ); + path.push(dir_name); + path.push( + &previous_context + .prepopulated_transaction + .signer_id + .to_string(), + ); + let mut data_path = std::path::PathBuf::new(); + 'outer: for access_key in access_key_list.keys { + let account_public_key = access_key.public_key.to_string(); + let is_full_access_key: bool = match &access_key.access_key.permission { + near_primitives::views::AccessKeyPermissionView::FullAccess => true, + near_primitives::views::AccessKeyPermissionView::FunctionCall { + allowance: _, + receiver_id: _, + method_names: _, + } => false, + }; + let dir = path + .read_dir() + .wrap_err("There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain.")?; + for entry in dir { + if let Ok(entry) = entry { + if entry + .path() + .file_stem() + .unwrap() + .to_str() + .unwrap() + .contains(account_public_key.rsplit(':').next().unwrap()) + && is_full_access_key + { + data_path.push(entry.path()); + break 'outer; + } + } else { + return Err(color_eyre::Report::msg( + "There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain." + )); + }; + } + } + data_path + } + } + }; + let data = std::fs::read_to_string(&data_path).wrap_err_with(|| { + format!( + "Access key file for account <{}> on network <{}> not found!", + previous_context.prepopulated_transaction.signer_id, network_config.network_name + ) + })?; + let account_json: super::AccountKeyPair = serde_json::from_str(&data) + .wrap_err_with(|| format!("Error reading data from file: {:?}", &data_path))?; + + let (nonce, block_hash, block_height) = if previous_context.global_context.offline { + ( + scope + .nonce + .wrap_err("Nonce is required to sign a transaction in offline mode")?, + scope + .block_hash + .wrap_err("Block Hash is required to sign a transaction in offline mode")? + .0, + scope + .block_height + .wrap_err("Block Height is required to sign a transaction in offline mode")?, + ) + } else { + let rpc_query_response = network_config + .json_rpc_client() + .blocking_call_view_access_key( + &previous_context.prepopulated_transaction.signer_id, + &account_json.public_key, + near_primitives::types::BlockReference::latest() + ) + .wrap_err( + "Cannot sign a transaction due to an error while fetching the most recent nonce value", + )?; + ( + rpc_query_response + .access_key_view() + .wrap_err("Error current_nonce")? + .nonce + + 1, + rpc_query_response.block_hash, + rpc_query_response.block_height, + ) + }; + + let mut unsigned_transaction = near_primitives::transaction::Transaction { + public_key: account_json.public_key.clone(), + block_hash, + nonce, + signer_id: previous_context.prepopulated_transaction.signer_id.clone(), + receiver_id: previous_context.prepopulated_transaction.receiver_id, + actions: previous_context.prepopulated_transaction.actions, + }; + + (previous_context.on_before_signing_callback)(&mut unsigned_transaction, &network_config)?; + + if network_config.meta_transaction_relayer_url.is_some() { + let max_block_height = block_height + + scope + .meta_transaction_valid_for + .unwrap_or(super::META_TRANSACTION_VALID_FOR_DEFAULT); + + let signed_delegate_action = super::get_signed_delegate_action( + unsigned_transaction, + &account_json.public_key, + account_json.private_key, + max_block_height, + ); + + return Ok(Self { + network_config: previous_context.network_config, + global_context: previous_context.global_context, + signed_transaction_or_signed_delegate_action: signed_delegate_action.into(), + on_before_sending_transaction_callback: previous_context + .on_before_sending_transaction_callback, + on_after_sending_transaction_callback: previous_context + .on_after_sending_transaction_callback, + }); + } - // let signature = account_json - // .private_key - // .sign(unsigned_transaction.get_hash_and_size().0.as_ref()); + let signature = account_json + .private_key + .sign(unsigned_transaction.get_hash_and_size().0.as_ref()); - // let signed_transaction = near_primitives::transaction::SignedTransaction::new( - // signature.clone(), - // unsigned_transaction, - // ); + let signed_transaction = near_primitives::transaction::SignedTransaction::new( + signature.clone(), + unsigned_transaction, + ); - // eprintln!("\nYour transaction was signed successfully."); - // eprintln!("Public key: {}", account_json.public_key); - // eprintln!("Signature: {}", signature); + eprintln!("\nYour transaction was signed successfully."); + eprintln!("Public key: {}", account_json.public_key); + eprintln!("Signature: {}", signature); - // Ok(Self { - // network_config: previous_context.network_config, - // global_context: previous_context.global_context, - // signed_transaction_or_signed_delegate_action: signed_transaction.into(), - // on_before_sending_transaction_callback: previous_context - // .on_before_sending_transaction_callback, - // on_after_sending_transaction_callback: previous_context - // .on_after_sending_transaction_callback, - // }) - qwe(previous_context, scope) + Ok(Self { + network_config: previous_context.network_config, + global_context: previous_context.global_context, + signed_transaction_or_signed_delegate_action: signed_transaction.into(), + on_before_sending_transaction_callback: previous_context + .on_before_sending_transaction_callback, + on_after_sending_transaction_callback: previous_context + .on_after_sending_transaction_callback, + }) } } @@ -340,217 +340,3 @@ impl SignLegacyKeychain { Ok(None) } } - -#[tracing::instrument( - name = "Signing the transaction with a key saved in legacy keychain ...", - skip_all, - // level = "debug" -)] -fn qwe( - previous_context: crate::commands::TransactionContext, - scope: &::InteractiveClapContextScope, -) -> color_eyre::eyre::Result { - let network_config = previous_context.network_config.clone(); - - let file_name = format!( - "{}.json", - &previous_context.prepopulated_transaction.signer_id - ); - let mut path = - std::path::PathBuf::from(&previous_context.global_context.config.credentials_home_dir); - - let data_path: std::path::PathBuf = get_data_path( - &network_config, - &previous_context.prepopulated_transaction.signer_id, - scope.signer_public_key.clone(), - &previous_context.global_context.config.credentials_home_dir, - previous_context.global_context.offline, - )?; - - let data = std::fs::read_to_string(&data_path).wrap_err_with(|| { - format!( - "Access key file for account <{}> on network <{}> not found!", - previous_context.prepopulated_transaction.signer_id, network_config.network_name - ) - })?; - let account_json: super::AccountKeyPair = serde_json::from_str(&data) - .wrap_err_with(|| format!("Error reading data from file: {:?}", &data_path))?; - - let (nonce, block_hash, block_height) = if previous_context.global_context.offline { - ( - scope - .nonce - .wrap_err("Nonce is required to sign a transaction in offline mode")?, - scope - .block_hash - .wrap_err("Block Hash is required to sign a transaction in offline mode")? - .0, - scope - .block_height - .wrap_err("Block Height is required to sign a transaction in offline mode")?, - ) - } else { - let rpc_query_response = network_config - .json_rpc_client() - .blocking_call_view_access_key( - &previous_context.prepopulated_transaction.signer_id, - &account_json.public_key, - near_primitives::types::BlockReference::latest() - ) - .wrap_err( - "Cannot sign a transaction due to an error while fetching the most recent nonce value", - )?; - ( - rpc_query_response - .access_key_view() - .wrap_err("Error current_nonce")? - .nonce - + 1, - rpc_query_response.block_hash, - rpc_query_response.block_height, - ) - }; - - let mut unsigned_transaction = near_primitives::transaction::Transaction { - public_key: account_json.public_key.clone(), - block_hash, - nonce, - signer_id: previous_context.prepopulated_transaction.signer_id.clone(), - receiver_id: previous_context.prepopulated_transaction.receiver_id, - actions: previous_context.prepopulated_transaction.actions, - }; - - (previous_context.on_before_signing_callback)(&mut unsigned_transaction, &network_config)?; - - if network_config.meta_transaction_relayer_url.is_some() { - let max_block_height = block_height - + scope - .meta_transaction_valid_for - .unwrap_or(super::META_TRANSACTION_VALID_FOR_DEFAULT); - - let signed_delegate_action = super::get_signed_delegate_action( - unsigned_transaction, - &account_json.public_key, - account_json.private_key, - max_block_height, - ); - - return Ok(SignLegacyKeychainContext { - network_config: previous_context.network_config, - global_context: previous_context.global_context, - signed_transaction_or_signed_delegate_action: signed_delegate_action.into(), - on_before_sending_transaction_callback: previous_context - .on_before_sending_transaction_callback, - on_after_sending_transaction_callback: previous_context - .on_after_sending_transaction_callback, - }); - } - - let signature = account_json - .private_key - .sign(unsigned_transaction.get_hash_and_size().0.as_ref()); - - let signed_transaction = near_primitives::transaction::SignedTransaction::new( - signature.clone(), - unsigned_transaction, - ); - - eprintln!("\nYour transaction was signed successfully."); - eprintln!("Public key: {}", account_json.public_key); - eprintln!("Signature: {}", signature); - - Ok(SignLegacyKeychainContext { - network_config: previous_context.network_config, - global_context: previous_context.global_context, - signed_transaction_or_signed_delegate_action: signed_transaction.into(), - on_before_sending_transaction_callback: previous_context - .on_before_sending_transaction_callback, - on_after_sending_transaction_callback: previous_context - .on_after_sending_transaction_callback, - }) -} - -#[tracing::instrument( - // name = "Signing the transaction with a key saved in legacy keychain ...", - skip_all -)] -fn get_data_path( - network_config: &crate::config::NetworkConfig, - signer_id: &near_primitives::types::AccountId, - signer_public_key: Option, - credentials_home_dir: &std::path::PathBuf, - offline: bool, -) -> color_eyre::eyre::Result { - let file_name = format!("{signer_id}.json"); - let mut path = std::path::PathBuf::from(credentials_home_dir); - - let dir_name: &str = &network_config.network_name; - path.push(dir_name); - - if offline { - path.push(signer_id.as_str()); - path.push(&format!( - "{}.json", - signer_public_key - .clone() - .wrap_err("Signer public key is required to sign a transaction in offline mode")? - .to_string() - .replace(':', "_") - )); - Ok(path) - } else { - path.push(file_name); - if path.exists() { - Ok(path) - } else { - println!("----------------"); - let access_key_list = network_config - .json_rpc_client() - .blocking_call_view_access_key_list( - signer_id, - near_primitives::types::Finality::Final.into(), - ) - .wrap_err_with(|| format!("Failed to fetch access KeyList for {}", signer_id))? - .access_key_list_view()?; - let mut path = std::path::PathBuf::from(&credentials_home_dir); - path.push(dir_name); - path.push(signer_id.to_string()); - let mut data_path = std::path::PathBuf::new(); - 'outer: for access_key in access_key_list.keys { - let account_public_key = access_key.public_key.to_string(); - let is_full_access_key: bool = match &access_key.access_key.permission { - near_primitives::views::AccessKeyPermissionView::FullAccess => true, - near_primitives::views::AccessKeyPermissionView::FunctionCall { - allowance: _, - receiver_id: _, - method_names: _, - } => false, - }; - let dir = path - .read_dir() - .wrap_err("There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain.")?; - for entry in dir { - if let Ok(entry) = entry { - if entry - .path() - .file_stem() - .unwrap() - .to_str() - .unwrap() - .contains(account_public_key.rsplit(':').next().unwrap()) - && is_full_access_key - { - data_path.push(entry.path()); - break 'outer; - } - } else { - return Err(color_eyre::Report::msg( - "There are no access keys found in the keychain for the signer account. Log in before signing transactions with keychain." - )); - }; - } - } - Ok(data_path) - } - } -} From 0b1d90416f19a89c9afbcfab0c4ed1140fdb14ad Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sun, 21 Jul 2024 08:20:03 +0300 Subject: [PATCH 06/13] refactored TEACH-ME to blocking_call_view_function() --- .../view_storage_balance.rs | 3 -- .../account/update_social_profile/sign_as.rs | 12 ++----- .../account/view_account_summary/mod.rs | 11 ++----- .../call_function/as_read_only/mod.rs | 10 +----- src/commands/staking/delegate/view_balance.rs | 14 -------- src/commands/tokens/send_ft/amount_ft.rs | 5 --- src/commands/tokens/view_ft_balance/mod.rs | 4 --- src/commands/tokens/view_nft_assets/mod.rs | 3 -- src/common.rs | 33 ++++++++++++++----- src/types/ft_properties.rs | 2 -- 10 files changed, 30 insertions(+), 67 deletions(-) diff --git a/src/commands/account/storage_management/view_storage_balance.rs b/src/commands/account/storage_management/view_storage_balance.rs index 2cb93242d..86ad44665 100644 --- a/src/commands/account/storage_management/view_storage_balance.rs +++ b/src/commands/account/storage_management/view_storage_balance.rs @@ -34,7 +34,6 @@ impl AccountContext { let storage_balance = get_storage_balance( - previous_context.global_context.teach_me, network_config, &contract_account_id, &account_id, @@ -85,7 +84,6 @@ impl Account { #[tracing::instrument(name = "Getting storage balance for", skip_all)] fn get_storage_balance( - teach_me: bool, network_config: &crate::config::NetworkConfig, contract_account_id: &near_primitives::types::AccountId, account_id: &crate::types::account_id::AccountId, @@ -95,7 +93,6 @@ fn get_storage_balance( network_config .json_rpc_client() .blocking_call_view_function( - teach_me, contract_account_id, "storage_balance_of", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/account/update_social_profile/sign_as.rs b/src/commands/account/update_social_profile/sign_as.rs index 6647f855d..6f1d82e29 100644 --- a/src/commands/account/update_social_profile/sign_as.rs +++ b/src/commands/account/update_social_profile/sign_as.rs @@ -50,7 +50,6 @@ impl From for crate::commands::ActionContext { Arc::new({ move |network_config| { get_prepopulated_transaction( - item.global_context.teach_me, network_config, &account_id, &signer_id, @@ -155,7 +154,6 @@ impl Signer { skip_all )] fn get_prepopulated_transaction( - teach_me: bool, network_config: &crate::config::NetworkConfig, account_id: &near_primitives::types::AccountId, signer_id: &near_primitives::types::AccountId, @@ -169,12 +167,8 @@ fn get_prepopulated_transaction( }; let local_profile: serde_json::Value = serde_json::from_slice(data)?; - let remote_profile = get_remote_profile( - teach_me, - network_config, - &contract_account_id, - account_id.clone(), - )?; + let remote_profile = + get_remote_profile(network_config, &contract_account_id, account_id.clone())?; let deposit = required_deposit( &network_config.json_rpc_client(), @@ -254,7 +248,6 @@ fn get_deposit( #[tracing::instrument(name = "Getting data about a remote profile ...", skip_all)] fn get_remote_profile( - teach_me: bool, network_config: &crate::config::NetworkConfig, near_social_account_id: &near_primitives::types::AccountId, account_id: near_primitives::types::AccountId, @@ -262,7 +255,6 @@ fn get_remote_profile( match network_config .json_rpc_client() .blocking_call_view_function( - teach_me, near_social_account_id, "get", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/account/view_account_summary/mod.rs b/src/commands/account/view_account_summary/mod.rs index 2f5132b06..5cda2b35a 100644 --- a/src/commands/account/view_account_summary/mod.rs +++ b/src/commands/account/view_account_summary/mod.rs @@ -29,7 +29,6 @@ impl ViewAccountSummaryContext { move |network_config, block_reference| { get_account_inquiry( - previous_context.teach_me, &account_id, network_config, block_reference ) @@ -62,7 +61,6 @@ impl ViewAccountSummary { #[tracing::instrument(name = "Receiving an inquiry about your account ...", skip_all)] fn get_account_inquiry( - teach_me: bool, account_id: &near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, @@ -156,10 +154,9 @@ fn get_account_inquiry( .try_collect(), )?; - let optional_account_profile = - get_account_profile(teach_me, account_id, network_config, block_reference) - .ok() - .flatten(); + 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, @@ -221,7 +218,6 @@ async fn get_delegated_staked_balance( #[tracing::instrument(name = "Getting an account profile ...", skip_all)] fn get_account_profile( - teach_me: bool, account_id: &near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, @@ -230,7 +226,6 @@ fn get_account_profile( let mut social_db = network_config .json_rpc_client() .blocking_call_view_function( - teach_me, &contract_account_id, "get", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/contract/call_function/as_read_only/mod.rs b/src/commands/contract/call_function/as_read_only/mod.rs index ed1cd87b0..bced442c9 100644 --- a/src/commands/contract/call_function/as_read_only/mod.rs +++ b/src/commands/contract/call_function/as_read_only/mod.rs @@ -78,7 +78,6 @@ impl FunctionContext { move |network_config, block_reference| { call_view_function( - previous_context.global_context.teach_me, network_config, &account_id, &function_name, @@ -119,7 +118,6 @@ impl Function { #[tracing::instrument(name = "Getting a response to a view method ...", skip_all)] fn call_view_function( - teach_me: bool, network_config: &crate::config::NetworkConfig, account_id: &near_primitives::types::AccountId, function_name: &str, @@ -130,13 +128,7 @@ fn call_view_function( let args = super::call_function_args_type::function_args(function_args, function_args_type)?; let call_result = network_config .json_rpc_client() - .blocking_call_view_function( - teach_me, - account_id, - function_name, - args, - block_reference.clone(), - ) + .blocking_call_view_function(account_id, function_name, args, block_reference.clone()) .wrap_err_with(|| { format!( "Failed to fetch query for view method: '{}' (contract <{}> on network <{}>)", diff --git a/src/commands/staking/delegate/view_balance.rs b/src/commands/staking/delegate/view_balance.rs index de241ad78..92a2cd387 100644 --- a/src/commands/staking/delegate/view_balance.rs +++ b/src/commands/staking/delegate/view_balance.rs @@ -31,7 +31,6 @@ impl ViewBalanceContext { move |network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference| { calculation_delegated_stake_balance( - previous_context.global_context.teach_me, &account_id, &validator_account_id, network_config, @@ -66,28 +65,24 @@ impl ViewBalance { skip_all )] fn calculation_delegated_stake_balance( - teach_me: bool, account_id: &near_primitives::types::AccountId, validator_account_id: &near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, ) -> crate::CliResult { let user_staked_balance: u128 = get_user_staked_balance( - teach_me, network_config, block_reference, validator_account_id, account_id, )?; let user_unstaked_balance: u128 = get_user_unstaked_balance( - teach_me, network_config, block_reference, validator_account_id, account_id, )?; let user_total_balance: u128 = get_user_total_balance( - teach_me, network_config, block_reference, validator_account_id, @@ -95,7 +90,6 @@ fn calculation_delegated_stake_balance( )?; let withdrawal_availability_message = match is_account_unstaked_balance_available_for_withdrawal( - teach_me, network_config, validator_account_id, account_id, @@ -126,7 +120,6 @@ fn calculation_delegated_stake_balance( #[tracing::instrument(name = "Getting the staked balance for the user ...", skip_all)] pub fn get_user_staked_balance( - teach_me: bool, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, validator_account_id: &near_primitives::types::AccountId, @@ -135,7 +128,6 @@ pub fn get_user_staked_balance( Ok(network_config .json_rpc_client() .blocking_call_view_function( - teach_me, validator_account_id, "get_account_staked_balance", serde_json::to_vec(&serde_json::json!({ @@ -156,7 +148,6 @@ pub fn get_user_staked_balance( #[tracing::instrument(name = "Getting the unstaked balance for the user ...", skip_all)] pub fn get_user_unstaked_balance( - teach_me: bool, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, validator_account_id: &near_primitives::types::AccountId, @@ -165,7 +156,6 @@ pub fn get_user_unstaked_balance( Ok(network_config .json_rpc_client() .blocking_call_view_function( - teach_me, validator_account_id, "get_account_unstaked_balance", serde_json::to_vec(&serde_json::json!({ @@ -186,7 +176,6 @@ pub fn get_user_unstaked_balance( #[tracing::instrument(name = "Getting the total balance for the user ...", skip_all)] pub fn get_user_total_balance( - teach_me: bool, network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference, validator_account_id: &near_primitives::types::AccountId, @@ -195,7 +184,6 @@ pub fn get_user_total_balance( Ok(network_config .json_rpc_client() .blocking_call_view_function( - teach_me, validator_account_id, "get_account_total_balance", serde_json::to_vec(&serde_json::json!({ @@ -219,7 +207,6 @@ pub fn get_user_total_balance( skip_all )] pub fn is_account_unstaked_balance_available_for_withdrawal( - teach_me: bool, network_config: &crate::config::NetworkConfig, validator_account_id: &near_primitives::types::AccountId, account_id: &near_primitives::types::AccountId, @@ -227,7 +214,6 @@ pub fn is_account_unstaked_balance_available_for_withdrawal( network_config .json_rpc_client() .blocking_call_view_function( - teach_me, validator_account_id, "is_account_unstaked_balance_available", serde_json::to_vec(&serde_json::json!({ diff --git a/src/commands/tokens/send_ft/amount_ft.rs b/src/commands/tokens/send_ft/amount_ft.rs index cccc022de..3a6f437d3 100644 --- a/src/commands/tokens/send_ft/amount_ft.rs +++ b/src/commands/tokens/send_ft/amount_ft.rs @@ -44,7 +44,6 @@ impl AmountFtContext { ) })?; let ft_metadata = crate::types::ft_properties::params_ft_metadata( - previous_context.global_context.teach_me, previous_context.ft_contract_account_id.clone(), &network_config, near_primitives::types::Finality::Final.into(), @@ -76,7 +75,6 @@ impl AmountFt { })?; let ft_metadata = crate::types::ft_properties::params_ft_metadata( - context.global_context.teach_me, context.ft_contract_account_id.clone(), &network_config, near_primitives::types::Finality::Final.into(), @@ -194,7 +192,6 @@ impl DepositContext { move |network_config| { get_prepopulated_transaction( - previous_context.global_context.teach_me, network_config, &ft_contract_account_id, &receiver_account_id, @@ -265,7 +262,6 @@ impl Deposit { skip_all )] fn get_prepopulated_transaction( - teach_me: bool, network_config: &crate::config::NetworkConfig, ft_contract_account_id: &near_primitives::types::AccountId, receiver_account_id: &near_primitives::types::AccountId, @@ -291,7 +287,6 @@ fn get_prepopulated_transaction( let call_result = network_config .json_rpc_client() .blocking_call_view_function( - teach_me, ft_contract_account_id, "storage_balance_of", args.clone(), diff --git a/src/commands/tokens/view_ft_balance/mod.rs b/src/commands/tokens/view_ft_balance/mod.rs index 339ae3eee..9a206b815 100644 --- a/src/commands/tokens/view_ft_balance/mod.rs +++ b/src/commands/tokens/view_ft_balance/mod.rs @@ -31,7 +31,6 @@ impl ViewFtBalanceContext { move |network_config, block_reference| { let crate::types::ft_properties::FtMetadata { decimals, symbol } = crate::types::ft_properties::params_ft_metadata( - previous_context.global_context.teach_me, ft_contract_account_id.clone(), network_config, block_reference.clone(), @@ -41,7 +40,6 @@ impl ViewFtBalanceContext { }))?; let call_result = get_ft_balance( - previous_context.global_context.teach_me, network_config, &ft_contract_account_id, args, @@ -91,7 +89,6 @@ impl ViewFtBalance { #[tracing::instrument(name = "Getting FT balance ...", skip_all)] fn get_ft_balance( - teach_me: bool, network_config: &crate::config::NetworkConfig, ft_contract_account_id: &near_primitives::types::AccountId, args: Vec, @@ -100,7 +97,6 @@ fn get_ft_balance( network_config .json_rpc_client() .blocking_call_view_function( - teach_me, ft_contract_account_id, "ft_balance_of", args, diff --git a/src/commands/tokens/view_nft_assets/mod.rs b/src/commands/tokens/view_nft_assets/mod.rs index 3791f7f97..e9edc818a 100644 --- a/src/commands/tokens/view_nft_assets/mod.rs +++ b/src/commands/tokens/view_nft_assets/mod.rs @@ -35,7 +35,6 @@ impl ViewNftAssetsContext { }))?; let call_result = get_nft_balance( - previous_context.global_context.teach_me, network_config, &nft_contract_account_id, args, @@ -79,7 +78,6 @@ impl ViewNftAssets { #[tracing::instrument(name = "Getting NFT balance ...", skip_all)] fn get_nft_balance( - teach_me: bool, network_config: &crate::config::NetworkConfig, nft_contract_account_id: &near_primitives::types::AccountId, args: Vec, @@ -88,7 +86,6 @@ fn get_nft_balance( network_config .json_rpc_client() .blocking_call_view_function( - teach_me, nft_contract_account_id, "nft_tokens_for_owner", args, diff --git a/src/common.rs b/src/common.rs index 6ab1be141..2c3d2493e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2009,7 +2009,6 @@ pub trait JsonRpcClientExt { /// arguments and function return value. fn blocking_call_view_function( &self, - teach_me: bool, account_id: &near_primitives::types::AccountId, method_name: &str, args: Vec, @@ -2069,7 +2068,6 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { #[tracing::instrument(name = "Getting the result of executing", skip_all)] fn blocking_call_view_function( &self, - teach_me: bool, account_id: &near_primitives::types::AccountId, method_name: &str, args: Vec, @@ -2087,6 +2085,14 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { }, }; let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request)?; + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nHTTP POST {}\nJSON-BODY:\n{:#}", + method_name, + account_id, + self.server_addr(), + request_payload, + ); let query_view_method_response = self .blocking_call(query_view_method_request) @@ -2094,10 +2100,13 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { let response_payload = serde_json::to_value(&query_view_method_response)?; - if teach_me { - eprintln!("\nJSON-BODY:\n{:#}", request_payload); - eprintln!("RESPONSE:\n{}", response_payload); - } + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nRESPONSE:\n{:#}", + method_name, + account_id, + response_payload + ); query_view_method_response.call_result() } @@ -2129,6 +2138,14 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { )) })?; + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting access key information for public_key: {}\nHTTP POST {}\nJSON-BODY:\n{:#}", + public_key, + self.server_addr(), + request_payload, + ); + let query_view_method_response = self.blocking_call(query_view_method_request)?; let response_payload = serde_json::to_value(&query_view_method_response).map_err(|err| { @@ -2139,10 +2156,8 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting access key information for public_key: {}\nHTTP POST {}\nJSON-BODY:\n{:#}\nRESPONSE:\n{:#}", + "\nTEACH-ME: Getting access key information for public_key: {}\nRESPONSE:\n{:#}", public_key, - self.server_addr(), - request_payload, response_payload ); diff --git a/src/types/ft_properties.rs b/src/types/ft_properties.rs index 6683af35b..8c6c5198b 100644 --- a/src/types/ft_properties.rs +++ b/src/types/ft_properties.rs @@ -153,7 +153,6 @@ pub struct FtMetadata { #[tracing::instrument(name = "Getting FT metadata ...", skip_all)] pub fn params_ft_metadata( - teach_me: bool, ft_contract_account_id: near_primitives::types::AccountId, network_config: &crate::config::NetworkConfig, block_reference: near_primitives::types::BlockReference, @@ -161,7 +160,6 @@ pub fn params_ft_metadata( let ft_metadata: FtMetadata = network_config .json_rpc_client() .blocking_call_view_function( - teach_me, &ft_contract_account_id, "ft_metadata", vec![], From 6f685710363561e56e398cc30fc1cdb10aa83b88 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 22 Jul 2024 21:35:18 +0300 Subject: [PATCH 07/13] refactored --- src/common.rs | 110 +++++++++++++++--- src/transaction_signature_options/send/mod.rs | 46 ++++++-- 2 files changed, 131 insertions(+), 25 deletions(-) diff --git a/src/common.rs b/src/common.rs index 2c3d2493e..7ca1c66b9 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2098,14 +2098,17 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { .blocking_call(query_view_method_request) .wrap_err("Failed to make a view-function call")?; - let response_payload = serde_json::to_value(&query_view_method_response)?; + let call_result = query_view_method_response.call_result()?; tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nRESPONSE:\n{:#}", + "\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nRESPONSE:\n{{\n \"block_hash\": {}\n \"block_height\": {}\n \"logs\": {:?}\n \"result\": {:?}\n}}", method_name, account_id, - response_payload + query_view_method_response.block_hash, + query_view_method_response.block_height, + call_result.logs, + call_result.result ); query_view_method_response.call_result() @@ -2132,11 +2135,7 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { public_key: public_key.clone(), }, }; - let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request).map_err(|err| { - near_jsonrpc_client::errors::JsonRpcError::TransportError(near_jsonrpc_client::errors::RpcTransportError::SendError( - near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(err), - )) - })?; + let request_payload = request_payload(&query_view_method_request)?; tracing::info!( target: "near_teach_me", @@ -2146,13 +2145,10 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { request_payload, ); - let query_view_method_response = self.blocking_call(query_view_method_request)?; + let query_view_method_response: near_jsonrpc_client::methods::query::RpcQueryResponse = + self.blocking_call(query_view_method_request)?; - let response_payload = serde_json::to_value(&query_view_method_response).map_err(|err| { - near_jsonrpc_client::errors::JsonRpcError::TransportError(near_jsonrpc_client::errors::RpcTransportError::SendError( - near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(err.into()), - )) - })?; + let response_payload = response_payload(&query_view_method_response)?; tracing::info!( target: "near_teach_me", @@ -2176,12 +2172,35 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { >, > { tracing::Span::current().pb_set_message(&format!("{account_id} access keys ...")); - self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest { + + let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, request: near_primitives::views::QueryRequest::ViewAccessKeyList { account_id: account_id.clone(), }, - }) + }; + let request_payload = request_payload(&query_view_method_request)?; + + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting a list of {} access keys\nHTTP POST {}\nJSON-BODY:\n{:#}", + account_id, + self.server_addr(), + request_payload, + ); + + let query_view_method_response = self.blocking_call(query_view_method_request)?; + + let response_payload = response_payload(&query_view_method_response)?; + + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting a list of {} access keys\nRESPONSE:\n{:#}", + account_id, + response_payload + ); + + Ok(query_view_method_response) } #[tracing::instrument(name = "Getting information about", skip_all)] @@ -2196,15 +2215,70 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { >, > { tracing::Span::current().pb_set_message(&format!("{account_id} ...")); - self.blocking_call(near_jsonrpc_client::methods::query::RpcQueryRequest { + + let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, request: near_primitives::views::QueryRequest::ViewAccount { account_id: account_id.clone(), }, - }) + }; + let request_payload = request_payload(&query_view_method_request)?; + + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting information about {}\nHTTP POST {}\nJSON-BODY:\n{:#}", + account_id, + self.server_addr(), + request_payload, + ); + + let query_view_method_response = self.blocking_call(query_view_method_request)?; + + let response_payload = response_payload(&query_view_method_response)?; + + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Getting information about {}\nRESPONSE:\n{:#}", + account_id, + response_payload + ); + + Ok(query_view_method_response) } } +fn request_payload( + query_view_method_request: &near_jsonrpc_client::methods::query::RpcQueryRequest, +) -> Result< + serde_json::Value, + near_jsonrpc_client::errors::JsonRpcError, +> { + near_jsonrpc_client::methods::to_json(query_view_method_request).map_err(|err| { + near_jsonrpc_client::errors::JsonRpcError::TransportError( + near_jsonrpc_client::errors::RpcTransportError::SendError( + near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError(err), + ), + ) + }) +} + +fn response_payload( + query_view_method_response: &near_jsonrpc_client::methods::query::RpcQueryResponse, +) -> Result< + serde_json::Value, + near_jsonrpc_client::errors::JsonRpcError, +> { + serde_json::to_value(query_view_method_response).map_err(|err| { + near_jsonrpc_client::errors::JsonRpcError::TransportError( + near_jsonrpc_client::errors::RpcTransportError::SendError( + near_jsonrpc_client::errors::JsonRpcTransportSendError::PayloadSerializeError( + err.into(), + ), + ), + ) + }) +} + #[easy_ext::ext(RpcQueryResponseExt)] pub impl near_jsonrpc_primitives::types::query::RpcQueryResponse { fn access_key_view(&self) -> color_eyre::eyre::Result { diff --git a/src/transaction_signature_options/send/mod.rs b/src/transaction_signature_options/send/mod.rs index d9624b48c..4ae853378 100644 --- a/src/transaction_signature_options/send/mod.rs +++ b/src/transaction_signature_options/send/mod.rs @@ -81,11 +81,19 @@ pub fn sending_signed_transaction( let retries_number = 5; let mut retries = (1..=retries_number).rev(); let transaction_info = loop { - let transaction_info_result = network_config.json_rpc_client().blocking_call( + let request = near_jsonrpc_client::methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest { signed_transaction: signed_transaction.clone(), - }, + }; + let request_payload = near_jsonrpc_client::methods::to_json(&request)?; + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Broadcasting transaction\nHTTP POST {}\nJSON-BODY:\n{:#}", + network_config.rpc_url.as_str(), + request_payload, ); + + let transaction_info_result = network_config.json_rpc_client().blocking_call(request); match transaction_info_result { Ok(response) => { break response; @@ -107,6 +115,15 @@ pub fn sending_signed_transaction( }, }; }; + + let response_payload = serde_json::to_value(&transaction_info)?; + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Broadcasting transaction\nHTTP POST {}\nRESPONSE:\n{:#}", + network_config.rpc_url.as_str(), + response_payload, + ); + Ok(transaction_info) } @@ -126,13 +143,28 @@ fn sending_delegate_action( ) -> Result { tracing::Span::current().pb_set_message(meta_transaction_relayer_url.as_str()); let client = reqwest::blocking::Client::new(); - let json_payload = serde_json::json!({ + let request_payload = serde_json::json!({ "signed_delegate_action": crate::types::signed_delegate_action::SignedDelegateActionAsBase64::from( signed_delegate_action ).to_string() }); - client - .post(meta_transaction_relayer_url) - .json(&json_payload) - .send() + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Broadcasting delegate action\nHTTP POST {}\nJSON-BODY:\n{:#}", + meta_transaction_relayer_url.as_str(), + request_payload, + ); + + let response = client + .post(meta_transaction_relayer_url.clone()) + .json(&request_payload) + .send()?; + tracing::info!( + target: "near_teach_me", + "\nTEACH-ME: Broadcasting delegate action\nHTTP POST {}\nRESPONSE:\n{:#?}", + meta_transaction_relayer_url.as_str(), + response, + ); + + Ok(response) } From 12d01500911e232ec3b07d1b038936f5013b47f6 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Tue, 23 Jul 2024 15:44:54 +0300 Subject: [PATCH 08/13] updated GUIDE --- docs/GUIDE.en.md | 152 +++++++++++++++++++++++++++++++++++++++++++++++ docs/GUIDE.ru.md | 152 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 304 insertions(+) diff --git a/docs/GUIDE.en.md b/docs/GUIDE.en.md index b4a8f93eb..c71043a32 100644 --- a/docs/GUIDE.en.md +++ b/docs/GUIDE.en.md @@ -17,6 +17,158 @@ near --offline tokens \ sign-later ``` +The _near CLI_ is a great tool for training NEAR developers, as is everything that works in NEAR. For example, if you want to view more detailed information about the RPC calls being made and their parameters, simply run the CLI with the --teach-me flag: +```txt +near --teach-me tokens \ + fro_volod.testnet \ + send-near volodymyr.testnet 0.1NEAR \ + network-config testnet \ + sign-with-keychain \ + send +``` + +
The result of this command will be as follows: + +```txt +2024-07-23T12:29:46.494692Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: +TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx +HTTP POST https://archival-rpc.testnet.near.org/ +JSON-BODY: +{ + "id": "Ky9vRhGq8", + "jsonrpc": "2.0", + "method": "query", + "params": { + "account_id": "fro_volod.testnet", + "finality": "optimistic", + "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + "request_type": "view_access_key" + } +} +2024-07-23T12:29:46.644841Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: +TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx +RESPONSE: +{ + "block_hash": "BjYtcgF1ynmbCk7xxiq4gSbR9gz5M6HkjVW2EYBMZXVQ", + "block_height": 169663524, + "nonce": 147781057000102, + "permission": "FullAccess" +} + +Your transaction was signed successfully. +Public key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx +Signature: ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69 +2024-07-23T12:29:46.648690Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: +TEACH-ME: Broadcasting transaction +HTTP POST https://archival-rpc.testnet.near.org/ +JSON-BODY: +{ + "id": "uiBiMwggW", + "jsonrpc": "2.0", + "method": "broadcast_tx_commit", + "params": [ + "EQAAAGZyb192b2xvZC50ZXN0bmV0AGYjuraPK0UBuPn9vFOErFp7IcGKqhQ5AqH8v2LHNdzhpzJo9WeGAAARAAAAdm9sb2R5bXlyLnRlc3RuZXSfe1I/45KUz3forrObVqRfwEiUMAgc5HX4Rb4CX6GgZwEAAAADAACA9krhxwItFQAAAAAAAABp3wNpB74/a7r/4KgEtSGfQ6QcMI6znGuExxgMQbVKfs+v8wdznrDx0Lc+swUE7fkusoO+msfMH/fhAElOkUMC" + ] +} +2024-07-23T12:29:50.307444Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: +TEACH-ME: Broadcasting transaction +HTTP POST https://archival-rpc.testnet.near.org/ +RESPONSE: +{ + "receipts_outcome": [ + { + "block_hash": "7GJfCwNBB8TUQX7HmdEFGy1eeWbAskA5nxUTQ7naVAR8", + "id": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq", + "outcome": { + "executor_id": "volodymyr.testnet", + "gas_burnt": 223182562500, + "logs": [], + "metadata": { + "gas_profile": [], + "version": 3 + }, + "receipt_ids": [ + "4DJdfaFNSWCQnXoTVcuhB421hV316u2phg4px8B4vgos" + ], + "status": { + "SuccessValue": "" + }, + "tokens_burnt": "22318256250000000000" + }, + "proof": [ + { + "direction": "Left", + "hash": "HpiLaQSivM8bKvc9Ea6ifvgrV9F1R7ejiMJowGudHFWp" + } + ] + } + ], + "status": { + "SuccessValue": "" + }, + "transaction": { + "actions": [ + { + "Transfer": { + "deposit": "100000000000000000000000" + } + } + ], + "hash": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", + "nonce": 147781057000103, + "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + "receiver_id": "volodymyr.testnet", + "signature": "ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69", + "signer_id": "fro_volod.testnet" + }, + "transaction_outcome": { + "block_hash": "GZ8nNshNRyr9FqdKL3zn2o7cFEXhKWRctQC5hXEyJzV9", + "id": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", + "outcome": { + "executor_id": "fro_volod.testnet", + "gas_burnt": 223182562500, + "logs": [], + "metadata": { + "gas_profile": null, + "version": 1 + }, + "receipt_ids": [ + "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" + ], + "status": { + "SuccessReceiptId": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" + }, + "tokens_burnt": "22318256250000000000" + }, + "proof": [ + { + "direction": "Right", + "hash": "CxokG8nzMMQ8bpbs9HXAE8pmrWpexz7mk84geksmjW7" + }, + { + "direction": "Right", + "hash": "jLXQeFWfC5oK6EAEjgYHxo148yBfA52Hn2HfhTNEheZ" + } + ] + } +} + +--- Logs --------------------------- +Logs [volodymyr.testnet]: No logs +--- Result ------------------------- +Empty result +------------------------------------ + + has transferred 0.1 NEAR to successfully. + +Gas burned: 0.447 Tgas +Transaction fee: 0.0000446365125 NEAR (approximately $0.00026424 USD, using $5.92 USD/NEAR exchange rate) +Transaction ID: E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +To see the transaction in the transaction explorer, please open this url in your browser: +https://explorer.testnet.near.org/transactionsE7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +``` +
+ Before proceeding to the description of specific commands, it is necessary to consider two points common to these commands: 1. Sign transaction diff --git a/docs/GUIDE.ru.md b/docs/GUIDE.ru.md index a5a517a48..23472a20c 100644 --- a/docs/GUIDE.ru.md +++ b/docs/GUIDE.ru.md @@ -15,6 +15,158 @@ near --offline tokens \ sign-later ``` +_near CLI_ — отличный инструмент для обучения разработчиков NEAR, как и все, что работает в NEAR. Например, если вы хотите просмотреть более подробную информацию о выполняемых вызовах RPC и их параметрах, просто запустите CLI с флагом --teach-me: +```txt +near --teach-me tokens \ + fro_volod.testnet \ + send-near volodymyr.testnet 0.1NEAR \ + network-config testnet \ + sign-with-keychain \ + send +``` + +
Результат выполнения команды + +```txt +2024-07-23T12:29:46.494692Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: +TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx +HTTP POST https://archival-rpc.testnet.near.org/ +JSON-BODY: +{ + "id": "Ky9vRhGq8", + "jsonrpc": "2.0", + "method": "query", + "params": { + "account_id": "fro_volod.testnet", + "finality": "optimistic", + "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + "request_type": "view_access_key" + } +} +2024-07-23T12:29:46.644841Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: +TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx +RESPONSE: +{ + "block_hash": "BjYtcgF1ynmbCk7xxiq4gSbR9gz5M6HkjVW2EYBMZXVQ", + "block_height": 169663524, + "nonce": 147781057000102, + "permission": "FullAccess" +} + +Your transaction was signed successfully. +Public key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx +Signature: ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69 +2024-07-23T12:29:46.648690Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: +TEACH-ME: Broadcasting transaction +HTTP POST https://archival-rpc.testnet.near.org/ +JSON-BODY: +{ + "id": "uiBiMwggW", + "jsonrpc": "2.0", + "method": "broadcast_tx_commit", + "params": [ + "EQAAAGZyb192b2xvZC50ZXN0bmV0AGYjuraPK0UBuPn9vFOErFp7IcGKqhQ5AqH8v2LHNdzhpzJo9WeGAAARAAAAdm9sb2R5bXlyLnRlc3RuZXSfe1I/45KUz3forrObVqRfwEiUMAgc5HX4Rb4CX6GgZwEAAAADAACA9krhxwItFQAAAAAAAABp3wNpB74/a7r/4KgEtSGfQ6QcMI6znGuExxgMQbVKfs+v8wdznrDx0Lc+swUE7fkusoO+msfMH/fhAElOkUMC" + ] +} +2024-07-23T12:29:50.307444Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: +TEACH-ME: Broadcasting transaction +HTTP POST https://archival-rpc.testnet.near.org/ +RESPONSE: +{ + "receipts_outcome": [ + { + "block_hash": "7GJfCwNBB8TUQX7HmdEFGy1eeWbAskA5nxUTQ7naVAR8", + "id": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq", + "outcome": { + "executor_id": "volodymyr.testnet", + "gas_burnt": 223182562500, + "logs": [], + "metadata": { + "gas_profile": [], + "version": 3 + }, + "receipt_ids": [ + "4DJdfaFNSWCQnXoTVcuhB421hV316u2phg4px8B4vgos" + ], + "status": { + "SuccessValue": "" + }, + "tokens_burnt": "22318256250000000000" + }, + "proof": [ + { + "direction": "Left", + "hash": "HpiLaQSivM8bKvc9Ea6ifvgrV9F1R7ejiMJowGudHFWp" + } + ] + } + ], + "status": { + "SuccessValue": "" + }, + "transaction": { + "actions": [ + { + "Transfer": { + "deposit": "100000000000000000000000" + } + } + ], + "hash": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", + "nonce": 147781057000103, + "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + "receiver_id": "volodymyr.testnet", + "signature": "ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69", + "signer_id": "fro_volod.testnet" + }, + "transaction_outcome": { + "block_hash": "GZ8nNshNRyr9FqdKL3zn2o7cFEXhKWRctQC5hXEyJzV9", + "id": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", + "outcome": { + "executor_id": "fro_volod.testnet", + "gas_burnt": 223182562500, + "logs": [], + "metadata": { + "gas_profile": null, + "version": 1 + }, + "receipt_ids": [ + "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" + ], + "status": { + "SuccessReceiptId": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" + }, + "tokens_burnt": "22318256250000000000" + }, + "proof": [ + { + "direction": "Right", + "hash": "CxokG8nzMMQ8bpbs9HXAE8pmrWpexz7mk84geksmjW7" + }, + { + "direction": "Right", + "hash": "jLXQeFWfC5oK6EAEjgYHxo148yBfA52Hn2HfhTNEheZ" + } + ] + } +} + +--- Logs --------------------------- +Logs [volodymyr.testnet]: No logs +--- Result ------------------------- +Empty result +------------------------------------ + + has transferred 0.1 NEAR to successfully. + +Gas burned: 0.447 Tgas +Transaction fee: 0.0000446365125 NEAR (approximately $0.00026424 USD, using $5.92 USD/NEAR exchange rate) +Transaction ID: E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +To see the transaction in the transaction explorer, please open this url in your browser: +https://explorer.testnet.near.org/transactionsE7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +``` +
+ Прежде, чем перейти к описанию конкретных команд, необходимо рассмотреть два общих для этих команд пункта: 1. Подпись транзакции From ceb621a288d7600d7f36fe4067c6b2455697055b Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Tue, 23 Jul 2024 15:29:40 +0200 Subject: [PATCH 09/13] Apply suggestions from code review --- docs/GUIDE.en.md | 2 +- docs/GUIDE.ru.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/GUIDE.en.md b/docs/GUIDE.en.md index c71043a32..02a904453 100644 --- a/docs/GUIDE.en.md +++ b/docs/GUIDE.en.md @@ -17,7 +17,7 @@ near --offline tokens \ sign-later ``` -The _near CLI_ is a great tool for training NEAR developers, as is everything that works in NEAR. For example, if you want to view more detailed information about the RPC calls being made and their parameters, simply run the CLI with the --teach-me flag: +_near CLI_ is a great tool for understanding NEAR on the low level. For example, if you want to view more detailed information about the RPC calls being made and their parameters, simply run the CLI with the `--teach-me` flag: ```txt near --teach-me tokens \ fro_volod.testnet \ diff --git a/docs/GUIDE.ru.md b/docs/GUIDE.ru.md index 23472a20c..87a99cef5 100644 --- a/docs/GUIDE.ru.md +++ b/docs/GUIDE.ru.md @@ -15,7 +15,7 @@ near --offline tokens \ sign-later ``` -_near CLI_ — отличный инструмент для обучения разработчиков NEAR, как и все, что работает в NEAR. Например, если вы хотите просмотреть более подробную информацию о выполняемых вызовах RPC и их параметрах, просто запустите CLI с флагом --teach-me: +_near CLI_ — отличный инструмент для глубокого понимания NEAR. Например, если вы хотите просмотреть более подробную информацию о выполняемых вызовах RPC и их параметрах, просто запустите CLI с флагом `--teach-me`: ```txt near --teach-me tokens \ fro_volod.testnet \ From 4758e859e481bfc8119c410f83f4ec9d4050769d Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Tue, 23 Jul 2024 15:42:08 +0200 Subject: [PATCH 10/13] reverted unnecessary changes --- .../account/storage_management/view_storage_balance.rs | 8 +------- src/commands/account/update_social_profile/sign_as.rs | 7 +------ src/commands/account/view_account_summary/mod.rs | 5 +---- src/commands/contract/call_function/as_read_only/mod.rs | 9 +-------- src/commands/staking/delegate/view_balance.rs | 7 +------ src/commands/tokens/send_ft/amount_ft.rs | 2 -- src/commands/tokens/view_ft_balance/mod.rs | 8 +------- src/commands/tokens/view_nft_assets/mod.rs | 8 +------- 8 files changed, 7 insertions(+), 47 deletions(-) diff --git a/src/commands/account/storage_management/view_storage_balance.rs b/src/commands/account/storage_management/view_storage_balance.rs index 86ad44665..f490ca728 100644 --- a/src/commands/account/storage_management/view_storage_balance.rs +++ b/src/commands/account/storage_management/view_storage_balance.rs @@ -32,13 +32,7 @@ impl AccountContext { move |network_config, block_reference| { let contract_account_id = (previous_context.get_contract_account_id)(network_config)?; - let storage_balance = - get_storage_balance( - network_config, - &contract_account_id, - &account_id, - block_reference - )?; + 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:", diff --git a/src/commands/account/update_social_profile/sign_as.rs b/src/commands/account/update_social_profile/sign_as.rs index 6f1d82e29..c3b8645ea 100644 --- a/src/commands/account/update_social_profile/sign_as.rs +++ b/src/commands/account/update_social_profile/sign_as.rs @@ -49,12 +49,7 @@ impl From for crate::commands::ActionContext { let get_prepopulated_transaction_after_getting_network_callback: crate::commands::GetPrepopulatedTransactionAfterGettingNetworkCallback = Arc::new({ move |network_config| { - get_prepopulated_transaction( - network_config, - &account_id, - &signer_id, - &data - ) + get_prepopulated_transaction(network_config, &account_id, &signer_id, &data) } }); diff --git a/src/commands/account/view_account_summary/mod.rs b/src/commands/account/view_account_summary/mod.rs index 5cda2b35a..3b33a2d04 100644 --- a/src/commands/account/view_account_summary/mod.rs +++ b/src/commands/account/view_account_summary/mod.rs @@ -28,10 +28,7 @@ impl ViewAccountSummaryContext { let account_id: near_primitives::types::AccountId = scope.account_id.clone().into(); move |network_config, block_reference| { - get_account_inquiry( - &account_id, network_config, - block_reference - ) + get_account_inquiry(&account_id, network_config, block_reference) } }); Ok(Self(crate::network_view_at_block::ArgsForViewContext { diff --git a/src/commands/contract/call_function/as_read_only/mod.rs b/src/commands/contract/call_function/as_read_only/mod.rs index bced442c9..bc2bc4e15 100644 --- a/src/commands/contract/call_function/as_read_only/mod.rs +++ b/src/commands/contract/call_function/as_read_only/mod.rs @@ -77,14 +77,7 @@ 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) } }); diff --git a/src/commands/staking/delegate/view_balance.rs b/src/commands/staking/delegate/view_balance.rs index 92a2cd387..2a4a34adb 100644 --- a/src/commands/staking/delegate/view_balance.rs +++ b/src/commands/staking/delegate/view_balance.rs @@ -30,12 +30,7 @@ impl ViewBalanceContext { let on_after_getting_block_reference_callback: crate::network_view_at_block::OnAfterGettingBlockReferenceCallback = std::sync::Arc::new({ move |network_config: &crate::config::NetworkConfig, block_reference: &near_primitives::types::BlockReference| { - calculation_delegated_stake_balance( - &account_id, - &validator_account_id, - network_config, - block_reference - ) + calculation_delegated_stake_balance(&account_id, &validator_account_id, network_config, block_reference) } }); Ok(Self(crate::network_view_at_block::ArgsForViewContext { diff --git a/src/commands/tokens/send_ft/amount_ft.rs b/src/commands/tokens/send_ft/amount_ft.rs index 3a6f437d3..6257470f1 100644 --- a/src/commands/tokens/send_ft/amount_ft.rs +++ b/src/commands/tokens/send_ft/amount_ft.rs @@ -1,5 +1,3 @@ -#![allow(clippy::too_many_arguments)] - use color_eyre::eyre::{Context, ContextCompat}; use inquire::CustomType; use serde_json::{json, Value}; diff --git a/src/commands/tokens/view_ft_balance/mod.rs b/src/commands/tokens/view_ft_balance/mod.rs index 9a206b815..ec7e3a677 100644 --- a/src/commands/tokens/view_ft_balance/mod.rs +++ b/src/commands/tokens/view_ft_balance/mod.rs @@ -38,13 +38,7 @@ impl ViewFtBalanceContext { let args = serde_json::to_vec(&json!({ "account_id": owner_account_id.to_string(), }))?; - let call_result = - get_ft_balance( - network_config, - &ft_contract_account_id, - args, - block_reference.clone() - )?; + let call_result = get_ft_balance(network_config, &ft_contract_account_id, args, block_reference.clone())?; call_result.print_logs(); let amount: String = call_result.parse_result_from_json()?; let fungible_token = crate::types::ft_properties::FungibleToken::from_params_ft( diff --git a/src/commands/tokens/view_nft_assets/mod.rs b/src/commands/tokens/view_nft_assets/mod.rs index e9edc818a..b4aaf2e5d 100644 --- a/src/commands/tokens/view_nft_assets/mod.rs +++ b/src/commands/tokens/view_nft_assets/mod.rs @@ -33,13 +33,7 @@ impl ViewNftAssetsContext { let args = serde_json::to_vec(&json!({ "account_id": owner_account_id.to_string(), }))?; - let call_result = - get_nft_balance( - network_config, - &nft_contract_account_id, - args, - block_reference.clone() - )?; + let call_result = get_nft_balance(network_config, &nft_contract_account_id, args, block_reference.clone())?; call_result.print_logs(); let serde_call_result: serde_json::Value = call_result.parse_result_from_json()?; From 35361d2d453ade067533e43ca7e4cebcd1c3603e Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Sat, 27 Jul 2024 01:41:06 +0200 Subject: [PATCH 11/13] improved teach-me formatting --- Cargo.lock | 1 + Cargo.toml | 1 + .../call_function/as_read_only/mod.rs | 4 +- src/commands/contract/inspect/mod.rs | 2 +- src/common.rs | 135 +++++++++++++----- src/main.rs | 15 +- src/transaction_signature_options/send/mod.rs | 51 +++++-- 7 files changed, 160 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 825ab5e78..72766bcf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2487,6 +2487,7 @@ dependencies = [ "ed25519-dalek", "futures", "hex 0.4.3", + "indenter", "indicatif", "inquire", "interactive-clap", diff --git a/Cargo.toml b/Cargo.toml index 0fe903c75..ee190c237 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ open = "5" shell-words = "1" cargo-util = "0.1.1" indicatif = "0.17.8" +indenter = "0.3" tracing = "0.1.40" tracing-indicatif = "0.3.6" tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/src/commands/contract/call_function/as_read_only/mod.rs b/src/commands/contract/call_function/as_read_only/mod.rs index bc2bc4e15..cac43ff1d 100644 --- a/src/commands/contract/call_function/as_read_only/mod.rs +++ b/src/commands/contract/call_function/as_read_only/mod.rs @@ -109,7 +109,7 @@ impl Function { } } -#[tracing::instrument(name = "Getting a response to a view method ...", skip_all)] +#[tracing::instrument(name = "Getting a response to a read-only function call ...", skip_all)] fn call_view_function( network_config: &crate::config::NetworkConfig, account_id: &near_primitives::types::AccountId, @@ -124,7 +124,7 @@ fn call_view_function( .blocking_call_view_function(account_id, function_name, args, block_reference.clone()) .wrap_err_with(|| { format!( - "Failed to fetch query for view method: '{}' (contract <{}> on network <{}>)", + "Failed to fetch query for read-only function call: '{}' (contract <{}> on network <{}>)", function_name, account_id, network_config.network_name ) })?; diff --git a/src/commands/contract/inspect/mod.rs b/src/commands/contract/inspect/mod.rs index dde4b13b5..6a63a2704 100644 --- a/src/commands/contract/inspect/mod.rs +++ b/src/commands/contract/inspect/mod.rs @@ -586,6 +586,6 @@ pub async fn get_contract_abi( .map_err(FetchAbiError::AbiUnknownFormat); } } - std::thread::sleep(std::time::Duration::from_millis(100)); + tokio::time::sleep(std::time::Duration::from_millis(100)).await; } } diff --git a/src/common.rs b/src/common.rs index 7ca1c66b9..c81e06455 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2076,6 +2076,8 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::Span::current().pb_set_message(&format!( "the '{method_name}' method of the <{account_id}> contract ..." )); + tracing::info!(target: "near_teach_me", "the '{method_name}' method of the <{account_id}> contract ..."); + let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, request: near_primitives::views::QueryRequest::CallFunction { @@ -2085,13 +2087,25 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { }, }; let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request)?; + tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nHTTP POST {}\nJSON-BODY:\n{:#}", + parent: &tracing::Span::none(), + "I am making HTTP call to NEAR JSON RPC to call a read-only function {} on `{}` account, learn more https://docs.near.org/api/rpc/contracts#call-a-contract-function", method_name, - account_id, - self.server_addr(), - request_payload, + account_id + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP POST {}", + self.server_addr() + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + indent_payload(&format!("{:#}", request_payload)) ); let query_view_method_response = self @@ -2102,13 +2116,21 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting the result of executing the '{}' method of the <{}> contract\nRESPONSE:\n{{\n \"block_hash\": {}\n \"block_height\": {}\n \"logs\": {:?}\n \"result\": {:?}\n}}", - method_name, - account_id, - query_view_method_response.block_hash, - query_view_method_response.block_height, - call_result.logs, - call_result.result + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + indent_payload(&format!( + "{{\n \"block_hash\": {}\n \"block_height\": {}\n \"logs\": {:?}\n \"result\": {:?}\n}}", + query_view_method_response.block_hash, + query_view_method_response.block_height, + call_result.logs, + call_result.result + )) + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "Decoding the \"result\" array of bytes as UTF-8 string (tip: here is a Python snippet: `\"\".join([chr(c) for c in result])`):\n{}", + indent_payload(&String::from_utf8(call_result.result.clone()).unwrap_or_else(|_| "".to_owned())) ); query_view_method_response.call_result() @@ -2126,7 +2148,9 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { near_jsonrpc_primitives::types::query::RpcQueryError, >, > { - tracing::Span::current().pb_set_message(&format!("{public_key} ...")); + tracing::Span::current() + .pb_set_message(&format!("{public_key} on {account_id} account ...")); + tracing::info!(target: "near_teach_me", "{public_key} on {account_id} account ..."); let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, @@ -2139,10 +2163,22 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting access key information for public_key: {}\nHTTP POST {}\nJSON-BODY:\n{:#}", + parent: &tracing::Span::none(), + "I am making HTTP call to NEAR JSON RPC to get an access key {} details on `{}` account, learn more https://docs.near.org/api/rpc/access-keys#view-access-key", public_key, - self.server_addr(), - request_payload, + account_id + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP POST {}", + self.server_addr() + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + indent_payload(&format!("{:#}", request_payload)) ); let query_view_method_response: near_jsonrpc_client::methods::query::RpcQueryResponse = @@ -2152,9 +2188,9 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting access key information for public_key: {}\nRESPONSE:\n{:#}", - public_key, - response_payload + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + indent_payload(&format!("{:#}", response_payload)) ); Ok(query_view_method_response) @@ -2172,6 +2208,7 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { >, > { tracing::Span::current().pb_set_message(&format!("{account_id} access keys ...")); + tracing::info!(target: "near_teach_me", "{account_id} access keys ..."); let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, @@ -2183,10 +2220,21 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting a list of {} access keys\nHTTP POST {}\nJSON-BODY:\n{:#}", - account_id, - self.server_addr(), - request_payload, + parent: &tracing::Span::none(), + "I am making HTTP call to NEAR JSON RPC to get a list of keys for `{}` account, learn more https://docs.near.org/api/rpc/access-keys#view-access-key-list", + account_id + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP POST {}", + self.server_addr() + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + indent_payload(&format!("{:#}", request_payload)) ); let query_view_method_response = self.blocking_call(query_view_method_request)?; @@ -2195,9 +2243,9 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting a list of {} access keys\nRESPONSE:\n{:#}", - account_id, - response_payload + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + indent_payload(&format!("{:#}", response_payload)) ); Ok(query_view_method_response) @@ -2215,6 +2263,7 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { >, > { tracing::Span::current().pb_set_message(&format!("{account_id} ...")); + tracing::info!(target: "near_teach_me", "{account_id} ..."); let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, @@ -2226,10 +2275,21 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting information about {}\nHTTP POST {}\nJSON-BODY:\n{:#}", - account_id, - self.server_addr(), - request_payload, + parent: &tracing::Span::none(), + "I am making HTTP call to NEAR JSON RPC to query information about `{}` account, learn more https://docs.near.org/api/rpc/contracts#view-account", + account_id + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP POST {}", + self.server_addr() + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + indent_payload(&format!("{:#}", request_payload)) ); let query_view_method_response = self.blocking_call(query_view_method_request)?; @@ -2238,9 +2298,9 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Getting information about {}\nRESPONSE:\n{:#}", - account_id, - response_payload + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + indent_payload(&format!("{:#}", response_payload)) ); Ok(query_view_method_response) @@ -2279,6 +2339,17 @@ fn response_payload( }) } +pub(crate) fn indent_payload(s: &str) -> String { + use std::fmt::Write; + + let mut indented_string = String::new(); + indenter::indented(&mut indented_string) + .with_str(" | ") + .write_str(s) + .ok(); + indented_string +} + #[easy_ext::ext(RpcQueryResponseExt)] pub impl near_jsonrpc_primitives::types::query::RpcQueryResponse { fn access_key_view(&self) -> color_eyre::eyre::Result { diff --git a/src/main.rs b/src/main.rs index 75e3b776f..7793ae93e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,9 +137,14 @@ fn main() -> crate::common::CliResult { if cli.teach_me { let env_filter = EnvFilter::from_default_env() .add_directive(tracing::Level::WARN.into()) - .add_directive("near=info".parse()?); + .add_directive("near_teach_me=info".parse()?) + .add_directive("near_cli_rs=info".parse()?); tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer()) + .with( + tracing_subscriber::fmt::layer() + .without_time() + .with_target(false), + ) .with(env_filter) .init(); } else { @@ -164,7 +169,11 @@ fn main() -> crate::common::CliResult { .add_directive(tracing::Level::WARN.into()) .add_directive("near_cli_rs=info".parse()?); tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer())) + .with( + tracing_subscriber::fmt::layer() + .without_time() + .with_writer(indicatif_layer.get_stderr_writer()), + ) .with(indicatif_layer) .with(env_filter) .init(); diff --git a/src/transaction_signature_options/send/mod.rs b/src/transaction_signature_options/send/mod.rs index 4ae853378..da2686f3d 100644 --- a/src/transaction_signature_options/send/mod.rs +++ b/src/transaction_signature_options/send/mod.rs @@ -78,6 +78,8 @@ pub fn sending_signed_transaction( signed_transaction: &near_primitives::transaction::SignedTransaction, ) -> color_eyre::Result { tracing::Span::current().pb_set_message(network_config.rpc_url.as_str()); + tracing::info!(target: "near_teach_me", "{}", network_config.rpc_url.as_str()); + let retries_number = 5; let mut retries = (1..=retries_number).rev(); let transaction_info = loop { @@ -86,11 +88,23 @@ pub fn sending_signed_transaction( signed_transaction: signed_transaction.clone(), }; let request_payload = near_jsonrpc_client::methods::to_json(&request)?; + + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "I am making HTTP call to NEAR JSON RPC to broadcast a transaction, learn more https://docs.near.org/api/rpc/transactions#send-tx" + ); tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Broadcasting transaction\nHTTP POST {}\nJSON-BODY:\n{:#}", + parent: &tracing::Span::none(), + "HTTP POST {}", network_config.rpc_url.as_str(), - request_payload, + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + crate::common::indent_payload(&format!("{:#}", request_payload)) ); let transaction_info_result = network_config.json_rpc_client().blocking_call(request); @@ -117,11 +131,12 @@ pub fn sending_signed_transaction( }; let response_payload = serde_json::to_value(&transaction_info)?; + tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Broadcasting transaction\nHTTP POST {}\nRESPONSE:\n{:#}", - network_config.rpc_url.as_str(), - response_payload, + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + crate::common::indent_payload(&format!("{:#}", response_payload)) ); Ok(transaction_info) @@ -142,6 +157,8 @@ fn sending_delegate_action( meta_transaction_relayer_url: url::Url, ) -> Result { tracing::Span::current().pb_set_message(meta_transaction_relayer_url.as_str()); + tracing::info!(target: "near_teach_me", "{}", meta_transaction_relayer_url.as_str()); + let client = reqwest::blocking::Client::new(); let request_payload = serde_json::json!({ "signed_delegate_action": crate::types::signed_delegate_action::SignedDelegateActionAsBase64::from( @@ -150,20 +167,32 @@ fn sending_delegate_action( }); tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Broadcasting delegate action\nHTTP POST {}\nJSON-BODY:\n{:#}", - meta_transaction_relayer_url.as_str(), - request_payload, + parent: &tracing::Span::none(), + "I am making HTTP call to NEAR JSON RPC to broadcast a transaction, learn more https://docs.near.org/concepts/abstraction/relayers" + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP POST {}", + meta_transaction_relayer_url.as_str() + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + crate::common::indent_payload(&format!("{:#}", request_payload)) ); let response = client .post(meta_transaction_relayer_url.clone()) .json(&request_payload) .send()?; + tracing::info!( target: "near_teach_me", - "\nTEACH-ME: Broadcasting delegate action\nHTTP POST {}\nRESPONSE:\n{:#?}", - meta_transaction_relayer_url.as_str(), - response, + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + crate::common::indent_payload(&format!("{:#?}", response)) ); Ok(response) From cc232a31ca8c09d6c7bc8fb01e400ff3dc295295 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 27 Jul 2024 08:00:01 +0300 Subject: [PATCH 12/13] fixed GUIDE --- docs/GUIDE.en.md | 330 +++++++++++++++++++++++++++++------------------ docs/GUIDE.ru.md | 330 +++++++++++++++++++++++++++++------------------ 2 files changed, 416 insertions(+), 244 deletions(-) diff --git a/docs/GUIDE.en.md b/docs/GUIDE.en.md index 02a904453..a41f31da4 100644 --- a/docs/GUIDE.en.md +++ b/docs/GUIDE.en.md @@ -30,128 +30,208 @@ near --teach-me tokens \
The result of this command will be as follows: ```txt -2024-07-23T12:29:46.494692Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: -TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx -HTTP POST https://archival-rpc.testnet.near.org/ -JSON-BODY: -{ - "id": "Ky9vRhGq8", - "jsonrpc": "2.0", - "method": "query", - "params": { - "account_id": "fro_volod.testnet", - "finality": "optimistic", - "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", - "request_type": "view_access_key" - } -} -2024-07-23T12:29:46.644841Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: -TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx -RESPONSE: -{ - "block_hash": "BjYtcgF1ynmbCk7xxiq4gSbR9gz5M6HkjVW2EYBMZXVQ", - "block_height": 169663524, - "nonce": 147781057000102, - "permission": "FullAccess" -} +Unsigned transaction: + +signer_id: fro_volod.testnet +receiver_id: volodymyr.testnet +actions: + -- transfer deposit: 0.1 NEAR + + INFO Signing the transaction with a key saved in the secure keychain ...:Getting a list of: fro_volod.testnet access keys ... + INFO I am making HTTP call to NEAR JSON RPC to get a list of keys for `fro_volod.testnet` account, learn more https://docs.near.org/api/rpc/access-keys#view-access-key-list + INFO HTTP POST https://archival-rpc.testnet.near.org/ + INFO JSON Body: + | { + | "id": "RSDcGn4WP", + | "jsonrpc": "2.0", + | "method": "query", + | "params": { + | "account_id": "fro_volod.testnet", + | "finality": "final", + | "request_type": "view_access_key_list" + | } + | } + INFO JSON RPC Response: + | { + | "block_hash": "DaoWCSVSMVS6d5rLsYBgVKwSKb8XxZWN2KpEg2dQEbEY", + | "block_height": 169978024, + | "keys": [ + | { + | "access_key": { + | "nonce": 116133598000035, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:1TprKa4burMqDMjDHyBSUaFQQczF7NamhxTx2yEXe9P" + | }, + | { + | "access_key": { + | "nonce": 94982716000000, + | "permission": { + | "FunctionCall": { + | "allowance": "250000000000000000000000", + | "method_names": [], + | "receiver_id": "mintspace2.testnet" + | } + | } + | }, + | "public_key": "ed25519:7YCfA1KrToJtAYGTBgAMe4LWfQEi4iwLGcH2q5SvGKzD" + | }, + | { + | "access_key": { + | "nonce": 147781057000109, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx" + | }, + | { + | "access_key": { + | "nonce": 101493245000000, + | "permission": { + | "FunctionCall": { + | "allowance": "10000000000000000000000000", + | "method_names": [ + | "set_a", + | "set_b" + | ], + | "receiver_id": "meta.pool.testnet" + | } + | } + | }, + | "public_key": "ed25519:8KHRkmpWbAp6wHZ5imAGFZzRAHzha2cZoz7cc3J42Bz8" + | }, + | { + | "access_key": { + | "nonce": 98944792000000, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:8dGkLiLD285Pzgp6v4mhaUbJyFvwEMvzjss1u9xZokTz" + | }, + | { + | "access_key": { + | "nonce": 105032344000005, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:J5uajy3m24sEQdw1uWA5kD2i3PDcxRxcFYotVZqRyrm6" + | } + | ] + | } + INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx on fro_volod.testnet account ... + INFO I am making HTTP call to NEAR JSON RPC to get an access key ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx details on `fro_volod.testnet` account, learn more https://docs.near.org/api/rpc/access-keys#view-access-key + INFO HTTP POST https://archival-rpc.testnet.near.org/ + INFO JSON Body: + | { + | "id": "3DCGHrjRK", + | "jsonrpc": "2.0", + | "method": "query", + | "params": { + | "account_id": "fro_volod.testnet", + | "finality": "optimistic", + | "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + | "request_type": "view_access_key" + | } + | } + INFO JSON RPC Response: + | { + | "block_hash": "BDT76QHmdMC5yKHBuJBi3vgAC1j4hPSHoX5oVFpx1SG2", + | "block_height": 169978028, + | "nonce": 147781057000109, + | "permission": "FullAccess" + | } Your transaction was signed successfully. Public key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx -Signature: ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69 -2024-07-23T12:29:46.648690Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: -TEACH-ME: Broadcasting transaction -HTTP POST https://archival-rpc.testnet.near.org/ -JSON-BODY: -{ - "id": "uiBiMwggW", - "jsonrpc": "2.0", - "method": "broadcast_tx_commit", - "params": [ - "EQAAAGZyb192b2xvZC50ZXN0bmV0AGYjuraPK0UBuPn9vFOErFp7IcGKqhQ5AqH8v2LHNdzhpzJo9WeGAAARAAAAdm9sb2R5bXlyLnRlc3RuZXSfe1I/45KUz3forrObVqRfwEiUMAgc5HX4Rb4CX6GgZwEAAAADAACA9krhxwItFQAAAAAAAABp3wNpB74/a7r/4KgEtSGfQ6QcMI6znGuExxgMQbVKfs+v8wdznrDx0Lc+swUE7fkusoO+msfMH/fhAElOkUMC" - ] -} -2024-07-23T12:29:50.307444Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: -TEACH-ME: Broadcasting transaction -HTTP POST https://archival-rpc.testnet.near.org/ -RESPONSE: -{ - "receipts_outcome": [ - { - "block_hash": "7GJfCwNBB8TUQX7HmdEFGy1eeWbAskA5nxUTQ7naVAR8", - "id": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq", - "outcome": { - "executor_id": "volodymyr.testnet", - "gas_burnt": 223182562500, - "logs": [], - "metadata": { - "gas_profile": [], - "version": 3 - }, - "receipt_ids": [ - "4DJdfaFNSWCQnXoTVcuhB421hV316u2phg4px8B4vgos" - ], - "status": { - "SuccessValue": "" - }, - "tokens_burnt": "22318256250000000000" - }, - "proof": [ - { - "direction": "Left", - "hash": "HpiLaQSivM8bKvc9Ea6ifvgrV9F1R7ejiMJowGudHFWp" - } - ] - } - ], - "status": { - "SuccessValue": "" - }, - "transaction": { - "actions": [ - { - "Transfer": { - "deposit": "100000000000000000000000" - } - } - ], - "hash": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", - "nonce": 147781057000103, - "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", - "receiver_id": "volodymyr.testnet", - "signature": "ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69", - "signer_id": "fro_volod.testnet" - }, - "transaction_outcome": { - "block_hash": "GZ8nNshNRyr9FqdKL3zn2o7cFEXhKWRctQC5hXEyJzV9", - "id": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", - "outcome": { - "executor_id": "fro_volod.testnet", - "gas_burnt": 223182562500, - "logs": [], - "metadata": { - "gas_profile": null, - "version": 1 - }, - "receipt_ids": [ - "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" - ], - "status": { - "SuccessReceiptId": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" - }, - "tokens_burnt": "22318256250000000000" - }, - "proof": [ - { - "direction": "Right", - "hash": "CxokG8nzMMQ8bpbs9HXAE8pmrWpexz7mk84geksmjW7" - }, - { - "direction": "Right", - "hash": "jLXQeFWfC5oK6EAEjgYHxo148yBfA52Hn2HfhTNEheZ" - } - ] - } -} +Signature: ed25519:4r8YNLMkqhxSTFLejMf8JvZw6q8ue9BuQHf7JEycamAWCqLckfE5zNG7ceWoUfagQaJLTunD59ig4LuecYyVk8Qe + INFO Sending transaction ...:Broadcasting transaction via RPC: https://archival-rpc.testnet.near.org/ + INFO I am making HTTP call to NEAR JSON RPC to broadcast a transaction, learn more https://docs.near.org/api/rpc/transactions#send-tx + INFO HTTP POST https://archival-rpc.testnet.near.org/ + INFO JSON Body: + | { + | "id": "1ARLaDA3J", + | "jsonrpc": "2.0", + | "method": "broadcast_tx_commit", + | "params": [ + | "EQAAAGZyb192b2xvZC50ZXN0bmV0AGYjuraPK0UBuPn9vFOErFp7IcGKqhQ5AqH8v2LHNdzhrjJo9WeGAAARAAAAdm9sb2R5bXlyLnRlc3RuZXSXxVsmKUVo0lmnzQ013O+bqjznbnB5g/3biI+j62VuOwEAAAADAACA9krhxwItFQAAAAAAAADAazoMP0kIphzt/zQ7Z97rr64FLGGsXMJDS8sXpuX8WwQdEgF6GZX+fz8hvKx5GqB4nrxnwrxbZScTQcs9mcEP" + | ] + | } + INFO JSON RPC Response: + | { + | "receipts_outcome": [ + | { + | "block_hash": "7rKeJTzfty8YKWmxjCSAi3YB3AYkTs6b6BbrN3TfbzSu", + | "id": "HgDAs4D8SCe7RxgH9Knomg99N9LF92b7nUCC6FRbC7Eo", + | "outcome": { + | "executor_id": "volodymyr.testnet", + | "gas_burnt": 223182562500, + | "logs": [], + | "metadata": { + | "gas_profile": [], + | "version": 3 + | }, + | "receipt_ids": [ + | "5LTPntJ4CDmnHCjb4URXqmnCxudBhmPHDv7kpuEWY8U4" + | ], + | "status": { + | "SuccessValue": "" + | }, + | "tokens_burnt": "22318256250000000000" + | }, + | "proof": [ + | { + | "direction": "Right", + | "hash": "3sWdNsYk1wmbuQBJtWFuxVTViYjhqHrd7oahLAtGK6xC" + | } + | ] + | } + | ], + | "status": { + | "SuccessValue": "" + | }, + | "transaction": { + | "actions": [ + | { + | "Transfer": { + | "deposit": "100000000000000000000000" + | } + | } + | ], + | "hash": "F3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW", + | "nonce": 147781057000110, + | "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + | "receiver_id": "volodymyr.testnet", + | "signature": "ed25519:4r8YNLMkqhxSTFLejMf8JvZw6q8ue9BuQHf7JEycamAWCqLckfE5zNG7ceWoUfagQaJLTunD59ig4LuecYyVk8Qe", + | "signer_id": "fro_volod.testnet" + | }, + | "transaction_outcome": { + | "block_hash": "4Ctk97bpxgY3npmU41n5t7ZviKcVDD2sK6N9E1RvanER", + | "id": "F3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW", + | "outcome": { + | "executor_id": "fro_volod.testnet", + | "gas_burnt": 223182562500, + | "logs": [], + | "metadata": { + | "gas_profile": null, + | "version": 1 + | }, + | "receipt_ids": [ + | "HgDAs4D8SCe7RxgH9Knomg99N9LF92b7nUCC6FRbC7Eo" + | ], + | "status": { + | "SuccessReceiptId": "HgDAs4D8SCe7RxgH9Knomg99N9LF92b7nUCC6FRbC7Eo" + | }, + | "tokens_burnt": "22318256250000000000" + | }, + | "proof": [ + | { + | "direction": "Right", + | "hash": "2ktmkisPC2M6uXFKc6XuAWGA1WbtewS2L6ugkLv92K6T" + | }, + | { + | "direction": "Right", + | "hash": "HLHeyozXBSqN7Tz1JV3bxQ8J9z9dhAUSbKc5tXHDzHh2" + | } + | ] + | } + | } --- Logs --------------------------- Logs [volodymyr.testnet]: No logs @@ -162,10 +242,16 @@ Empty result has transferred 0.1 NEAR to successfully. Gas burned: 0.447 Tgas -Transaction fee: 0.0000446365125 NEAR (approximately $0.00026424 USD, using $5.92 USD/NEAR exchange rate) -Transaction ID: E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +Transaction fee: 0.0000446365125 NEAR (approximately $0.00025308 USD, using $5.67 USD/NEAR exchange rate) +Transaction ID: F3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW To see the transaction in the transaction explorer, please open this url in your browser: -https://explorer.testnet.near.org/transactionsE7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +https://explorer.testnet.near.org/transactionsF3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW + + + + +Here is your console command if you need to script it or re-run: + ./target/debug/near --teach-me tokens fro_volod.testnet send-near volodymyr.testnet '0.1 NEAR' network-config testnet sign-with-keychain send ```
diff --git a/docs/GUIDE.ru.md b/docs/GUIDE.ru.md index 87a99cef5..367385d95 100644 --- a/docs/GUIDE.ru.md +++ b/docs/GUIDE.ru.md @@ -28,128 +28,208 @@ near --teach-me tokens \
Результат выполнения команды ```txt -2024-07-23T12:29:46.494692Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: -TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx -HTTP POST https://archival-rpc.testnet.near.org/ -JSON-BODY: -{ - "id": "Ky9vRhGq8", - "jsonrpc": "2.0", - "method": "query", - "params": { - "account_id": "fro_volod.testnet", - "finality": "optimistic", - "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", - "request_type": "view_access_key" - } -} -2024-07-23T12:29:46.644841Z INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: near_teach_me: -TEACH-ME: Getting access key information for public_key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx -RESPONSE: -{ - "block_hash": "BjYtcgF1ynmbCk7xxiq4gSbR9gz5M6HkjVW2EYBMZXVQ", - "block_height": 169663524, - "nonce": 147781057000102, - "permission": "FullAccess" -} +Unsigned transaction: + +signer_id: fro_volod.testnet +receiver_id: volodymyr.testnet +actions: + -- transfer deposit: 0.1 NEAR + + INFO Signing the transaction with a key saved in the secure keychain ...:Getting a list of: fro_volod.testnet access keys ... + INFO I am making HTTP call to NEAR JSON RPC to get a list of keys for `fro_volod.testnet` account, learn more https://docs.near.org/api/rpc/access-keys#view-access-key-list + INFO HTTP POST https://archival-rpc.testnet.near.org/ + INFO JSON Body: + | { + | "id": "RSDcGn4WP", + | "jsonrpc": "2.0", + | "method": "query", + | "params": { + | "account_id": "fro_volod.testnet", + | "finality": "final", + | "request_type": "view_access_key_list" + | } + | } + INFO JSON RPC Response: + | { + | "block_hash": "DaoWCSVSMVS6d5rLsYBgVKwSKb8XxZWN2KpEg2dQEbEY", + | "block_height": 169978024, + | "keys": [ + | { + | "access_key": { + | "nonce": 116133598000035, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:1TprKa4burMqDMjDHyBSUaFQQczF7NamhxTx2yEXe9P" + | }, + | { + | "access_key": { + | "nonce": 94982716000000, + | "permission": { + | "FunctionCall": { + | "allowance": "250000000000000000000000", + | "method_names": [], + | "receiver_id": "mintspace2.testnet" + | } + | } + | }, + | "public_key": "ed25519:7YCfA1KrToJtAYGTBgAMe4LWfQEi4iwLGcH2q5SvGKzD" + | }, + | { + | "access_key": { + | "nonce": 147781057000109, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx" + | }, + | { + | "access_key": { + | "nonce": 101493245000000, + | "permission": { + | "FunctionCall": { + | "allowance": "10000000000000000000000000", + | "method_names": [ + | "set_a", + | "set_b" + | ], + | "receiver_id": "meta.pool.testnet" + | } + | } + | }, + | "public_key": "ed25519:8KHRkmpWbAp6wHZ5imAGFZzRAHzha2cZoz7cc3J42Bz8" + | }, + | { + | "access_key": { + | "nonce": 98944792000000, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:8dGkLiLD285Pzgp6v4mhaUbJyFvwEMvzjss1u9xZokTz" + | }, + | { + | "access_key": { + | "nonce": 105032344000005, + | "permission": "FullAccess" + | }, + | "public_key": "ed25519:J5uajy3m24sEQdw1uWA5kD2i3PDcxRxcFYotVZqRyrm6" + | } + | ] + | } + INFO Signing the transaction with a key saved in the secure keychain ...:Trying to sign with the legacy keychain ...:Signing the transaction with a key saved in legacy keychain ...:Getting access key information:: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx on fro_volod.testnet account ... + INFO I am making HTTP call to NEAR JSON RPC to get an access key ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx details on `fro_volod.testnet` account, learn more https://docs.near.org/api/rpc/access-keys#view-access-key + INFO HTTP POST https://archival-rpc.testnet.near.org/ + INFO JSON Body: + | { + | "id": "3DCGHrjRK", + | "jsonrpc": "2.0", + | "method": "query", + | "params": { + | "account_id": "fro_volod.testnet", + | "finality": "optimistic", + | "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + | "request_type": "view_access_key" + | } + | } + INFO JSON RPC Response: + | { + | "block_hash": "BDT76QHmdMC5yKHBuJBi3vgAC1j4hPSHoX5oVFpx1SG2", + | "block_height": 169978028, + | "nonce": 147781057000109, + | "permission": "FullAccess" + | } Your transaction was signed successfully. Public key: ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx -Signature: ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69 -2024-07-23T12:29:46.648690Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: -TEACH-ME: Broadcasting transaction -HTTP POST https://archival-rpc.testnet.near.org/ -JSON-BODY: -{ - "id": "uiBiMwggW", - "jsonrpc": "2.0", - "method": "broadcast_tx_commit", - "params": [ - "EQAAAGZyb192b2xvZC50ZXN0bmV0AGYjuraPK0UBuPn9vFOErFp7IcGKqhQ5AqH8v2LHNdzhpzJo9WeGAAARAAAAdm9sb2R5bXlyLnRlc3RuZXSfe1I/45KUz3forrObVqRfwEiUMAgc5HX4Rb4CX6GgZwEAAAADAACA9krhxwItFQAAAAAAAABp3wNpB74/a7r/4KgEtSGfQ6QcMI6znGuExxgMQbVKfs+v8wdznrDx0Lc+swUE7fkusoO+msfMH/fhAElOkUMC" - ] -} -2024-07-23T12:29:50.307444Z INFO Sending transaction ...:Broadcasting transaction via RPC: near_teach_me: -TEACH-ME: Broadcasting transaction -HTTP POST https://archival-rpc.testnet.near.org/ -RESPONSE: -{ - "receipts_outcome": [ - { - "block_hash": "7GJfCwNBB8TUQX7HmdEFGy1eeWbAskA5nxUTQ7naVAR8", - "id": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq", - "outcome": { - "executor_id": "volodymyr.testnet", - "gas_burnt": 223182562500, - "logs": [], - "metadata": { - "gas_profile": [], - "version": 3 - }, - "receipt_ids": [ - "4DJdfaFNSWCQnXoTVcuhB421hV316u2phg4px8B4vgos" - ], - "status": { - "SuccessValue": "" - }, - "tokens_burnt": "22318256250000000000" - }, - "proof": [ - { - "direction": "Left", - "hash": "HpiLaQSivM8bKvc9Ea6ifvgrV9F1R7ejiMJowGudHFWp" - } - ] - } - ], - "status": { - "SuccessValue": "" - }, - "transaction": { - "actions": [ - { - "Transfer": { - "deposit": "100000000000000000000000" - } - } - ], - "hash": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", - "nonce": 147781057000103, - "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", - "receiver_id": "volodymyr.testnet", - "signature": "ed25519:37mbDV7DutzcfjRsNE5A42ZxKw6BVodPb5siHGRpBzCoS9hZtAwYLmpEn9nZ9SuDL5WjxUEDbf29SztMFYyd8q69", - "signer_id": "fro_volod.testnet" - }, - "transaction_outcome": { - "block_hash": "GZ8nNshNRyr9FqdKL3zn2o7cFEXhKWRctQC5hXEyJzV9", - "id": "E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J", - "outcome": { - "executor_id": "fro_volod.testnet", - "gas_burnt": 223182562500, - "logs": [], - "metadata": { - "gas_profile": null, - "version": 1 - }, - "receipt_ids": [ - "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" - ], - "status": { - "SuccessReceiptId": "B3HdGyGGJSKDDWMeoyoRVGHM9JSweVSmEkukBd1S5Niq" - }, - "tokens_burnt": "22318256250000000000" - }, - "proof": [ - { - "direction": "Right", - "hash": "CxokG8nzMMQ8bpbs9HXAE8pmrWpexz7mk84geksmjW7" - }, - { - "direction": "Right", - "hash": "jLXQeFWfC5oK6EAEjgYHxo148yBfA52Hn2HfhTNEheZ" - } - ] - } -} +Signature: ed25519:4r8YNLMkqhxSTFLejMf8JvZw6q8ue9BuQHf7JEycamAWCqLckfE5zNG7ceWoUfagQaJLTunD59ig4LuecYyVk8Qe + INFO Sending transaction ...:Broadcasting transaction via RPC: https://archival-rpc.testnet.near.org/ + INFO I am making HTTP call to NEAR JSON RPC to broadcast a transaction, learn more https://docs.near.org/api/rpc/transactions#send-tx + INFO HTTP POST https://archival-rpc.testnet.near.org/ + INFO JSON Body: + | { + | "id": "1ARLaDA3J", + | "jsonrpc": "2.0", + | "method": "broadcast_tx_commit", + | "params": [ + | "EQAAAGZyb192b2xvZC50ZXN0bmV0AGYjuraPK0UBuPn9vFOErFp7IcGKqhQ5AqH8v2LHNdzhrjJo9WeGAAARAAAAdm9sb2R5bXlyLnRlc3RuZXSXxVsmKUVo0lmnzQ013O+bqjznbnB5g/3biI+j62VuOwEAAAADAACA9krhxwItFQAAAAAAAADAazoMP0kIphzt/zQ7Z97rr64FLGGsXMJDS8sXpuX8WwQdEgF6GZX+fz8hvKx5GqB4nrxnwrxbZScTQcs9mcEP" + | ] + | } + INFO JSON RPC Response: + | { + | "receipts_outcome": [ + | { + | "block_hash": "7rKeJTzfty8YKWmxjCSAi3YB3AYkTs6b6BbrN3TfbzSu", + | "id": "HgDAs4D8SCe7RxgH9Knomg99N9LF92b7nUCC6FRbC7Eo", + | "outcome": { + | "executor_id": "volodymyr.testnet", + | "gas_burnt": 223182562500, + | "logs": [], + | "metadata": { + | "gas_profile": [], + | "version": 3 + | }, + | "receipt_ids": [ + | "5LTPntJ4CDmnHCjb4URXqmnCxudBhmPHDv7kpuEWY8U4" + | ], + | "status": { + | "SuccessValue": "" + | }, + | "tokens_burnt": "22318256250000000000" + | }, + | "proof": [ + | { + | "direction": "Right", + | "hash": "3sWdNsYk1wmbuQBJtWFuxVTViYjhqHrd7oahLAtGK6xC" + | } + | ] + | } + | ], + | "status": { + | "SuccessValue": "" + | }, + | "transaction": { + | "actions": [ + | { + | "Transfer": { + | "deposit": "100000000000000000000000" + | } + | } + | ], + | "hash": "F3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW", + | "nonce": 147781057000110, + | "public_key": "ed25519:7siBhHN2eYNCubz5jAJhMdo34x33QJt5ZgUJBTNifZAx", + | "receiver_id": "volodymyr.testnet", + | "signature": "ed25519:4r8YNLMkqhxSTFLejMf8JvZw6q8ue9BuQHf7JEycamAWCqLckfE5zNG7ceWoUfagQaJLTunD59ig4LuecYyVk8Qe", + | "signer_id": "fro_volod.testnet" + | }, + | "transaction_outcome": { + | "block_hash": "4Ctk97bpxgY3npmU41n5t7ZviKcVDD2sK6N9E1RvanER", + | "id": "F3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW", + | "outcome": { + | "executor_id": "fro_volod.testnet", + | "gas_burnt": 223182562500, + | "logs": [], + | "metadata": { + | "gas_profile": null, + | "version": 1 + | }, + | "receipt_ids": [ + | "HgDAs4D8SCe7RxgH9Knomg99N9LF92b7nUCC6FRbC7Eo" + | ], + | "status": { + | "SuccessReceiptId": "HgDAs4D8SCe7RxgH9Knomg99N9LF92b7nUCC6FRbC7Eo" + | }, + | "tokens_burnt": "22318256250000000000" + | }, + | "proof": [ + | { + | "direction": "Right", + | "hash": "2ktmkisPC2M6uXFKc6XuAWGA1WbtewS2L6ugkLv92K6T" + | }, + | { + | "direction": "Right", + | "hash": "HLHeyozXBSqN7Tz1JV3bxQ8J9z9dhAUSbKc5tXHDzHh2" + | } + | ] + | } + | } --- Logs --------------------------- Logs [volodymyr.testnet]: No logs @@ -160,10 +240,16 @@ Empty result has transferred 0.1 NEAR to successfully. Gas burned: 0.447 Tgas -Transaction fee: 0.0000446365125 NEAR (approximately $0.00026424 USD, using $5.92 USD/NEAR exchange rate) -Transaction ID: E7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +Transaction fee: 0.0000446365125 NEAR (approximately $0.00025308 USD, using $5.67 USD/NEAR exchange rate) +Transaction ID: F3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW To see the transaction in the transaction explorer, please open this url in your browser: -https://explorer.testnet.near.org/transactionsE7ME6GPYb6DxoBFY9H5EQT4CauZKmcNNgyc3G7eDqs6J +https://explorer.testnet.near.org/transactionsF3eZmhtFekCrzKMbc3uk5UbKkMsuuecj6WbK9spcz8bW + + + + +Here is your console command if you need to script it or re-run: + ./target/debug/near --teach-me tokens fro_volod.testnet send-near volodymyr.testnet '0.1 NEAR' network-config testnet sign-with-keychain send ```
From aca5298f47a373fa0524f60924f997ae621115c7 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Sat, 27 Jul 2024 10:44:08 +0200 Subject: [PATCH 13/13] fixed clippy --- .../sign_with_legacy_keychain/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs b/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs index fcfe7d601..8c509f020 100644 --- a/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs +++ b/src/transaction_signature_options/sign_with_legacy_keychain/mod.rs @@ -107,7 +107,7 @@ impl SignLegacyKeychainContext { ); path.push(dir_name); path.push( - &previous_context + previous_context .prepopulated_transaction .signer_id .to_string(),