From 5ce21e9c04fed4ec586dc369ed14996e8481c04e Mon Sep 17 00:00:00 2001 From: Vukasin Gostovic Date: Wed, 2 Aug 2023 20:15:23 +0200 Subject: [PATCH] add --decimal flag --- src/main.rs | 10 ++++++++++ src/tracker/call_track.rs | 3 ++- src/tracker/common.rs | 11 +++-------- src/tracker/fast_track.rs | 3 ++- src/tracker/tracker.rs | 3 ++- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index a4ee4dd..538b096 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,10 @@ async fn main() -> Result<(), Box> { .long("no_setup") .num_args(0..) .help("Start replaying immediately.")) + .arg(Arg::new("decimal") + .long("decimal") + .num_args(0..) + .help("Start replaying immediately.")) .arg(Arg::new("contract_address") .long("contract_address") .short('c') @@ -197,6 +201,7 @@ async fn main() -> Result<(), Box> { } let block_listen_time = matches.get_one::("block_listen_time").expect("required").parse::()?; + let decimal = matches.get_occurrences::("decimal").is_some(); let path = matches.get_one::("path").expect("required").to_string(); let filename = matches.get_one::("filename").expect("required").to_string(); @@ -206,6 +211,7 @@ async fn main() -> Result<(), Box> { contract_address, terminal_block, block_listen_time, + decimal, path, filename, ).await?; @@ -223,6 +229,7 @@ async fn main() -> Result<(), Box> { let origin_block = matches.get_one::("origin_block").expect("Invalid origin_block").parse::()?; let query_interval = matches.get_one::("query_interval").map(|x| x.parse().expect("Invalid query interval")); + let decimal = matches.get_occurrences::("decimal").is_some(); let path = matches.get_one::("path").expect("Invalid path").to_string(); let filename = matches.get_one::("filename").expect("Invalid filename").to_string(); @@ -233,6 +240,7 @@ async fn main() -> Result<(), Box> { terminal_block, origin_block, query_interval, + decimal, path, filename, ).await?; @@ -249,6 +257,7 @@ async fn main() -> Result<(), Box> { let origin_block = matches.get_one::("origin_block").expect("Invalid origin_block").parse::()?; let query_interval = matches.get_one::("query_interval").map(|x| x.parse().expect("Invalid query interval")); + let decimal = matches.get_occurrences::("decimal").is_some(); let path = matches.get_one::("path").expect("Invalid path").to_string(); let filename = matches.get_one::("filename").expect("Invalid filename").to_string(); @@ -259,6 +268,7 @@ async fn main() -> Result<(), Box> { terminal_block, origin_block, query_interval, + decimal, path, filename, ).await?; diff --git a/src/tracker/call_track.rs b/src/tracker/call_track.rs index db3e33f..991e894 100644 --- a/src/tracker/call_track.rs +++ b/src/tracker/call_track.rs @@ -17,6 +17,7 @@ pub async fn call_track( terminal_block: Option, origin_block: u64, query_interval: Option, + decimal: bool, path: String, filename: String, ) -> Result<(), Box> { @@ -83,7 +84,7 @@ pub async fn call_track( current_block += interval; } - set_filename_and_serialize(path, filename, storage, contract_address, "calldata", calldata)?; + set_filename_and_serialize(path, filename, storage, contract_address, "calldata", calldata, decimal)?; Ok(()) } \ No newline at end of file diff --git a/src/tracker/common.rs b/src/tracker/common.rs index 96e8540..3923493 100644 --- a/src/tracker/common.rs +++ b/src/tracker/common.rs @@ -14,7 +14,7 @@ pub fn set_filename_and_serialize( middle_value: String, output_as_decimal: bool, ) -> Result<(), Box> { - let json: String; + let mut json: String; // Set the filename to `address{contract_address}-{middle_label}-{storage_slot}-timestamp-{unix_timestamp} if its the default one // We also check if we should serialize it as csv @@ -40,7 +40,7 @@ pub fn set_filename_and_serialize( println!("\nWriting to file: {}", path); if output_as_decimal { - let json = output_to_dec(json); + json = output_to_dec(json); } fs::write(path, json)?; @@ -67,12 +67,7 @@ fn output_to_dec(json: String) -> String { hex_str.to_string() } else { // Convert the hexadecimal to decimal and return as a string - if let decimal = hex_to_decimal(hex_str).unwrap() { - decimal.to_string() - } else { - // If the conversion fails, keep the original hexadecimal as is - hex_str.to_string() - } + hex_to_decimal(hex_str).unwrap().to_string() } }); diff --git a/src/tracker/fast_track.rs b/src/tracker/fast_track.rs index 6ff91e0..9e68fb6 100644 --- a/src/tracker/fast_track.rs +++ b/src/tracker/fast_track.rs @@ -17,6 +17,7 @@ pub async fn fast_track_state( terminal_block: Option, origin_block: u64, query_interval: Option, + decimal: bool, path: String, filename: String, ) -> Result<(), Box> { @@ -77,7 +78,7 @@ pub async fn fast_track_state( current_block += interval; } - set_filename_and_serialize(path, filename, storage, contract_address, "slot", storage_slot.to_string())?; + set_filename_and_serialize(path, filename, storage, contract_address, "slot", storage_slot.to_string(), decimal)?; Ok(()) } diff --git a/src/tracker/tracker.rs b/src/tracker/tracker.rs index 2180d46..cf2540c 100644 --- a/src/tracker/tracker.rs +++ b/src/tracker/tracker.rs @@ -16,6 +16,7 @@ pub async fn track_state( contract_address: String, terminal_block: Option, block_listen_time: u64, + decimal: bool, path: String, filename: String, ) -> Result<(), Box> { @@ -56,7 +57,7 @@ pub async fn track_state( block_number = source_rpc.listen_for_blocks(block_listen_time).await?; } - set_filename_and_serialize(path, filename, storage, contract_address, "slot", storage_slot.to_string())?; + set_filename_and_serialize(path, filename, storage, contract_address, "slot", storage_slot.to_string(), decimal)?; Ok(()) }