diff --git a/src/common.rs b/src/common.rs index 71e10e9b8..527d99099 100644 --- a/src/common.rs +++ b/src/common.rs @@ -268,6 +268,7 @@ pub fn verify_account_access_key( } } +#[tracing::instrument(name = "Checking the existence of the account ...", skip_all)] pub fn is_account_exist( networks: &linked_hash_map::LinkedHashMap, account_id: near_primitives::types::AccountId, @@ -326,7 +327,7 @@ pub fn ask_if_different_account_id_wanted() -> color_eyre::eyre::Result { Ok(select_choose_input == ConfirmOptions::Yes) } -#[tracing::instrument(name = "Getting the account state ...", skip_all)] +#[tracing::instrument(name = "Getting account status information for", skip_all)] pub async fn get_account_state( network_config: &crate::config::NetworkConfig, account_id: &near_primitives::types::AccountId, @@ -336,6 +337,12 @@ pub async fn get_account_state( near_jsonrpc_client::errors::JsonRpcError, > { loop { + tracing::Span::current().pb_set_message(&format!( + "<{account_id}> on network <{}> ...", + network_config.network_name + )); + tracing::info!(target: "near_teach_me", "<{account_id}> on network <{}> ...", network_config.network_name); + let query_view_method_response = view_account( format!("{}", network_config.rpc_url), &network_config.json_rpc_client(), @@ -403,14 +410,68 @@ async fn view_account( near_jsonrpc_client::errors::JsonRpcError, > { tracing::Span::current().pb_set_message(&instrument_message); + + let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { + block_reference, + request: near_primitives::views::QueryRequest::ViewAccount { + account_id: account_id.clone(), + }, + }; + + tracing::info!( + target: "near_teach_me", + 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 + ); + + if let Ok(request_payload) = near_jsonrpc_client::methods::to_json(&query_view_method_request) { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP POST {}", + json_rpc_client.server_addr() + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Request Body:\n{}", + indent_payload(&format!("{:#}", request_payload)) + ); + } + json_rpc_client - .call(near_jsonrpc_client::methods::query::RpcQueryRequest { - block_reference, - request: near_primitives::views::QueryRequest::ViewAccount { - account_id: account_id.clone(), - }, - }) + .call(query_view_method_request) .await + .inspect_err(|err| match err { + near_jsonrpc_client::errors::JsonRpcError::TransportError(transport_error) => { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Request failed due to connectivity issue:\n{}", + indent_payload(&format!("{:#?}", transport_error)) + ); + } + near_jsonrpc_client::errors::JsonRpcError::ServerError( + near_jsonrpc_client::errors::JsonRpcServerError::HandlerError(handler_error), + ) => { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Request returned a handling error:\n{}", + indent_payload(&serde_json::to_string_pretty(handler_error).unwrap_or_else(|_| handler_error.to_string())) + ); + } + near_jsonrpc_client::errors::JsonRpcError::ServerError(server_error) => { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Request returned a generic server error:\n{}", + indent_payload(&format!("{:#?}", server_error)) + ); + } + }) + .inspect(teach_me_call_response) } fn need_check_account(message: String) -> bool { @@ -2025,7 +2086,8 @@ pub trait JsonRpcClientExt { method: M, ) -> near_jsonrpc_client::MethodCallResult where - M: near_jsonrpc_client::methods::RpcMethod; + M: near_jsonrpc_client::methods::RpcMethod, + M::Error: serde::Serialize + std::fmt::Debug + std::fmt::Display; /// A helper function to make a view-funcation call using JSON encoding for the function /// arguments and function return value. @@ -2079,10 +2141,54 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { ) -> near_jsonrpc_client::MethodCallResult where M: near_jsonrpc_client::methods::RpcMethod, + M::Error: serde::Serialize + std::fmt::Debug + std::fmt::Display, { + if let Ok(request_payload) = near_jsonrpc_client::methods::to_json(&method) { + 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 Request Body:\n{}", + indent_payload(&format!("{:#}", request_payload)) + ); + } + tokio::runtime::Runtime::new() .unwrap() .block_on(self.call(method)) + .inspect_err(|err| match err { + near_jsonrpc_client::errors::JsonRpcError::TransportError(transport_error) => { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Request failed due to connectivity issue:\n{}", + indent_payload(&format!("{:#?}", transport_error)) + ); + } + near_jsonrpc_client::errors::JsonRpcError::ServerError( + near_jsonrpc_client::errors::JsonRpcServerError::HandlerError(handler_error), + ) => { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Request returned a handling error:\n{}", + indent_payload(&serde_json::to_string_pretty(handler_error).unwrap_or_else(|_| handler_error.to_string())) + ); + } + near_jsonrpc_client::errors::JsonRpcError::ServerError(server_error) => { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Request returned a generic server error:\n{}", + indent_payload(&format!("{:#?}", server_error)) + ); + } + }) } /// A helper function to make a view-funcation call using JSON encoding for the function @@ -2091,71 +2197,68 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { fn blocking_call_view_function( &self, account_id: &near_primitives::types::AccountId, - method_name: &str, + function_name: &str, args: Vec, block_reference: near_primitives::types::BlockReference, ) -> Result { tracing::Span::current().pb_set_message(&format!( - "the '{method_name}' method of the <{account_id}> contract ..." + "a read-only function '{function_name}' of the <{account_id}> contract ..." )); - tracing::info!(target: "near_teach_me", "the '{method_name}' method of the <{account_id}> contract ..."); + tracing::info!(target: "near_teach_me", "a read-only function '{function_name}' 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(), + method_name: function_name.to_owned(), args: near_primitives::types::FunctionArgs::from(args), }, }; - let request_payload = near_jsonrpc_client::methods::to_json(&query_view_method_request)?; tracing::info!( target: "near_teach_me", 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, + "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", + function_name, 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) - .wrap_err("Failed to make a view-function call")?; - - let call_result = query_view_method_response.call_result()?; - - tracing::info!( - target: "near_teach_me", - 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())) - ); + .wrap_err("Read-only function execution failed")?; query_view_method_response.call_result() + .inspect(|call_result| { + tracing::info!( + target: "near_teach_me", + 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: you can use this Python snippet to do it: `\"\".join([chr(c) for c in result])`):\n{}", + indent_payload( + &String::from_utf8(call_result.result.clone()) + .unwrap_or_else(|_| "".to_owned()) + ) + ); + }) + .inspect_err(|_| { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + indent_payload("Internal error: Received unexpected query kind in response to a view-function query call") + ); + }) } #[tracing::instrument(name = "Getting access key information:", skip_all)] @@ -2170,9 +2273,10 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { near_jsonrpc_primitives::types::query::RpcQueryError, >, > { - 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 ..."); + tracing::Span::current().pb_set_message(&format!( + "public key {public_key} on account <{account_id}>..." + )); + tracing::info!(target: "near_teach_me", "public key {public_key} on account <{account_id}>..."); let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, @@ -2181,41 +2285,17 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { public_key: public_key.clone(), }, }; - let request_payload = request_payload(&query_view_method_request)?; tracing::info!( target: "near_teach_me", 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", + "I am making HTTP call to NEAR JSON RPC to get an access key details for public key {} on account <{}>, learn more https://docs.near.org/api/rpc/access-keys#view-access-key", public_key, 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 = - self.blocking_call(query_view_method_request)?; - - let response_payload = response_payload(&query_view_method_response)?; - - tracing::info!( - target: "near_teach_me", - parent: &tracing::Span::none(), - "JSON RPC Response:\n{}", - indent_payload(&format!("{:#}", response_payload)) - ); - Ok(query_view_method_response) + self.blocking_call(query_view_method_request) + .inspect(teach_me_call_response) } #[tracing::instrument(name = "Getting a list of", skip_all)] @@ -2229,8 +2309,9 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { near_jsonrpc_primitives::types::query::RpcQueryError, >, > { - tracing::Span::current().pb_set_message(&format!("{account_id} access keys ...")); - tracing::info!(target: "near_teach_me", "{account_id} access keys ..."); + tracing::Span::current() + .pb_set_message(&format!("access keys on account <{account_id}>...")); + tracing::info!(target: "near_teach_me", "access keys on account <{account_id}>..."); let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, @@ -2238,39 +2319,16 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { account_id: account_id.clone(), }, }; - let request_payload = request_payload(&query_view_method_request)?; tracing::info!( target: "near_teach_me", 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", + "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)?; - let response_payload = response_payload(&query_view_method_response)?; - - tracing::info!( - target: "near_teach_me", - parent: &tracing::Span::none(), - "JSON RPC Response:\n{}", - indent_payload(&format!("{:#}", response_payload)) - ); - - Ok(query_view_method_response) + self.blocking_call(query_view_method_request) + .inspect(teach_me_call_response) } #[tracing::instrument(name = "Getting information about", skip_all)] @@ -2284,8 +2342,8 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { near_jsonrpc_primitives::types::query::RpcQueryError, >, > { - tracing::Span::current().pb_set_message(&format!("{account_id} ...")); - tracing::info!(target: "near_teach_me", "{account_id} ..."); + tracing::Span::current().pb_set_message(&format!("account <{account_id}>...")); + tracing::info!(target: "near_teach_me", "account <{account_id}>..."); let query_view_method_request = near_jsonrpc_client::methods::query::RpcQueryRequest { block_reference, @@ -2293,75 +2351,31 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { account_id: account_id.clone(), }, }; - let request_payload = request_payload(&query_view_method_request)?; tracing::info!( target: "near_teach_me", 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", + "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)?; - let response_payload = response_payload(&query_view_method_response)?; + self.blocking_call(query_view_method_request) + .inspect(teach_me_call_response) + } +} +pub(crate) fn teach_me_call_response(response: &impl serde::Serialize) { + if let Ok(response_payload) = serde_json::to_value(response) { tracing::info!( target: "near_teach_me", parent: &tracing::Span::none(), "JSON RPC Response:\n{}", indent_payload(&format!("{:#}", 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(), - ), - ), - ) - }) -} - -pub(crate) fn indent_payload(s: &str) -> String { +pub fn indent_payload(s: &str) -> String { use std::fmt::Write; let mut indented_string = String::new(); diff --git a/src/transaction_signature_options/send/mod.rs b/src/transaction_signature_options/send/mod.rs index da2686f3d..52fb85d19 100644 --- a/src/transaction_signature_options/send/mod.rs +++ b/src/transaction_signature_options/send/mod.rs @@ -87,27 +87,17 @@ pub fn sending_signed_transaction( 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", 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", - parent: &tracing::Span::none(), - "HTTP POST {}", - network_config.rpc_url.as_str(), - ); - 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); + let transaction_info_result = network_config + .json_rpc_client() + .blocking_call(request) + .inspect(crate::common::teach_me_call_response); match transaction_info_result { Ok(response) => { break response; @@ -130,15 +120,6 @@ pub fn sending_signed_transaction( }; }; - let response_payload = serde_json::to_value(&transaction_info)?; - - tracing::info!( - target: "near_teach_me", - parent: &tracing::Span::none(), - "JSON RPC Response:\n{}", - crate::common::indent_payload(&format!("{:#}", response_payload)) - ); - Ok(transaction_info) } @@ -148,6 +129,7 @@ pub fn sending_signed_transaction( )] pub fn sleep_after_error(additional_message_for_name: String) { tracing::Span::current().pb_set_message(&additional_message_for_name); + tracing::info!(target: "near_teach_me", "{}", &additional_message_for_name); std::thread::sleep(std::time::Duration::from_secs(5)); }