Created
November 17, 2023 10:34
-
-
Save gavriliumircea/2a9797f207a2a2f3832ddaa376337e8c to your computer and use it in GitHub Desktop.
Rust merkle csv generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
extern crate csv; | |
use rand::Rng; | |
use std::fs::File; | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let n = 1000; | |
let output_file = "large.csv"; | |
let mut wtr = csv::Writer::from_writer(File::create(output_file)?); | |
wtr.write_record(&["address", "amount"])?; | |
for _ in 0..n { | |
let address = generate_ethereum_address(); | |
let amount = generate_random_amount(); | |
wtr.write_record(&[&address, &amount.to_string()])?; | |
} | |
wtr.flush()?; | |
println!("Generated {}", output_file); | |
Ok(()) | |
} | |
fn generate_ethereum_address() -> String { | |
let mut rng = rand::thread_rng(); | |
let address: String = (0..40) | |
.map(|_| { | |
let choice = rng.gen_range(0..16); | |
std::char::from_digit(choice, 16).unwrap() | |
}) | |
.collect(); | |
format!("0x{}", address) | |
} | |
fn generate_random_amount() -> u32 { | |
let mut rng = rand::thread_rng(); | |
rng.gen_range(1..1000) // Generate a random positive number between 1 and 999 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment