Skip to content

Commit

Permalink
add --decimal flag
Browse files Browse the repository at this point in the history
  • Loading branch information
makemake-kbo committed Aug 2, 2023
1 parent 84836b1 commit 5ce21e9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.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')
Expand Down Expand Up @@ -197,6 +201,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

let block_listen_time = matches.get_one::<String>("block_listen_time").expect("required").parse::<u64>()?;
let decimal = matches.get_occurrences::<String>("decimal").is_some();
let path = matches.get_one::<String>("path").expect("required").to_string();
let filename = matches.get_one::<String>("filename").expect("required").to_string();

Expand All @@ -206,6 +211,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
contract_address,
terminal_block,
block_listen_time,
decimal,
path,
filename,
).await?;
Expand All @@ -223,6 +229,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let origin_block = matches.get_one::<String>("origin_block").expect("Invalid origin_block").parse::<u64>()?;
let query_interval = matches.get_one::<String>("query_interval").map(|x| x.parse().expect("Invalid query interval"));
let decimal = matches.get_occurrences::<String>("decimal").is_some();
let path = matches.get_one::<String>("path").expect("Invalid path").to_string();
let filename = matches.get_one::<String>("filename").expect("Invalid filename").to_string();

Expand All @@ -233,6 +240,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
terminal_block,
origin_block,
query_interval,
decimal,
path,
filename,
).await?;
Expand All @@ -249,6 +257,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let origin_block = matches.get_one::<String>("origin_block").expect("Invalid origin_block").parse::<u64>()?;
let query_interval = matches.get_one::<String>("query_interval").map(|x| x.parse().expect("Invalid query interval"));
let decimal = matches.get_occurrences::<String>("decimal").is_some();
let path = matches.get_one::<String>("path").expect("Invalid path").to_string();
let filename = matches.get_one::<String>("filename").expect("Invalid filename").to_string();

Expand All @@ -259,6 +268,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
terminal_block,
origin_block,
query_interval,
decimal,
path,
filename,
).await?;
Expand Down
3 changes: 2 additions & 1 deletion src/tracker/call_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub async fn call_track(
terminal_block: Option<u64>,
origin_block: u64,
query_interval: Option<u64>,
decimal: bool,
path: String,
filename: String,
) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -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(())
}
11 changes: 3 additions & 8 deletions src/tracker/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn set_filename_and_serialize<T: SerializeStorage>(
middle_value: String,
output_as_decimal: bool,
) -> Result<(), Box<dyn std::error::Error>> {
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
Expand All @@ -40,7 +40,7 @@ pub fn set_filename_and_serialize<T: SerializeStorage>(
println!("\nWriting to file: {}", path);

if output_as_decimal {
let json = output_to_dec(json);
json = output_to_dec(json);
}

fs::write(path, json)?;
Expand All @@ -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()
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/tracker/fast_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub async fn fast_track_state(
terminal_block: Option<u64>,
origin_block: u64,
query_interval: Option<u64>,
decimal: bool,
path: String,
filename: String,
) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -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(())
}
3 changes: 2 additions & 1 deletion src/tracker/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub async fn track_state(
contract_address: String,
terminal_block: Option<u64>,
block_listen_time: u64,
decimal: bool,
path: String,
filename: String,
) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -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(())
}

0 comments on commit 5ce21e9

Please sign in to comment.