Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the ability to save payload for broadcast_tx_commit #413

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2150,12 +2150,23 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
"HTTP POST {}",
self.server_addr()
);

let (request_payload, message_about_saving_payload) =
check_request_payload_for_broadcast_tx_commit(request_payload);

tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"JSON Request Body:\n{}",
indent_payload(&format!("{:#}", request_payload))
);
if !message_about_saving_payload.is_empty() {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"{}", message_about_saving_payload
);
}
}

tokio::runtime::Runtime::new()
Expand Down Expand Up @@ -2364,6 +2375,66 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
}
}

fn check_request_payload_for_broadcast_tx_commit(
mut request_payload: serde_json::Value,
) -> (serde_json::Value, String) {
let mut message_about_saving_payload = String::new();
if let Some(method) = request_payload.get("method") {
if method.to_string().contains("broadcast_tx_commit") {
if let Some(serde_json::Value::Array(array)) = request_payload.get("params") {
let signed_transaction_as_base64 =
array.iter().map(|val| val.to_string()).collect::<String>();
let signed_transaction_as_base64 = signed_transaction_as_base64.trim_matches('"');

if signed_transaction_as_base64.len() > 1000 {
let data_signed_transaction = serde_json::json!(
{"Signed transaction (serialized as base64)": signed_transaction_as_base64});

let file_path = std::path::PathBuf::from("broadcast_tx_commit.json");

request_payload["params"] = serde_json::json!(format!(
"A transaction in base64 encoding contains {} characters. The entire payload will be stored in `{}`",
signed_transaction_as_base64.len(),
&file_path.display()
));

match std::fs::File::create(&file_path) {
Ok(mut file) => {
match serde_json::to_vec(&data_signed_transaction) {
Ok(buf) => match file.write(&buf) {
Ok(_) => {
message_about_saving_payload = format!("The file `{}` was created successfully. It has a signed transaction (serialized as base64).", &file_path.display());
}
Err(err) => {
message_about_saving_payload = format!("Failed to save payload to `{}`. Failed to write file:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err)));
}
},
Err(err) => {
message_about_saving_payload = format!(
"Failed to save payload to `{}`. Serialization error:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err))
);
}
}
}
Err(err) => {
message_about_saving_payload = format!(
"Failed to save payload to `{}`. Failed to create file:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err))
);
}
}
}
}
}
}
(request_payload, message_about_saving_payload)
}

pub(crate) fn teach_me_call_response(response: &impl serde::Serialize) {
if let Ok(response_payload) = serde_json::to_value(response) {
tracing::info!(
Expand Down
Loading