|
| 1 | +use super::rpc::get_code; |
| 2 | +use crate::{ |
| 3 | + constants::{ADDRESS_REGEX, BYTECODE_REGEX}, |
| 4 | + debug_max, |
| 5 | + utils::io::logging::Logger, |
| 6 | +}; |
| 7 | +use std::fs; |
| 8 | + |
| 9 | +pub async fn get_bytecode_from_target( |
| 10 | + target: &str, |
| 11 | + rpc_url: &str, |
| 12 | +) -> Result<String, Box<dyn std::error::Error>> { |
| 13 | + let (logger, _) = Logger::new(""); |
| 14 | + |
| 15 | + if ADDRESS_REGEX.is_match(target)? { |
| 16 | + // Target is a contract address, so we need to fetch the bytecode from the RPC provider. |
| 17 | + get_code(target, rpc_url).await |
| 18 | + } else if BYTECODE_REGEX.is_match(target)? { |
| 19 | + debug_max!("using provided bytecode for snapshotting."); |
| 20 | + |
| 21 | + // Target is already a bytecode, so we just need to remove 0x from the begining |
| 22 | + Ok(target.replacen("0x", "", 1)) |
| 23 | + } else { |
| 24 | + debug_max!("using provided file for snapshotting."); |
| 25 | + |
| 26 | + // Target is a file path, so we need to read the bytecode from the file. |
| 27 | + match fs::read_to_string(target) { |
| 28 | + Ok(contents) => { |
| 29 | + let _contents = contents.replace('\n', ""); |
| 30 | + if BYTECODE_REGEX.is_match(&_contents)? && _contents.len() % 2 == 0 { |
| 31 | + Ok(_contents.replacen("0x", "", 1)) |
| 32 | + } else { |
| 33 | + logger.error(&format!("file '{}' doesn't contain valid bytecode.", &target)); |
| 34 | + std::process::exit(1) |
| 35 | + } |
| 36 | + } |
| 37 | + Err(_) => { |
| 38 | + logger.error(&format!("failed to open file '{}' .", &target)); |
| 39 | + std::process::exit(1) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +#[cfg(test)] |
| 45 | +mod tests { |
| 46 | + use super::*; |
| 47 | + use std::fs; |
| 48 | + |
| 49 | + #[tokio::test] |
| 50 | + async fn test_get_bytecode_when_target_is_address() { |
| 51 | + let bytecode = get_bytecode_from_target( |
| 52 | + "0x9f00c43700bc0000Ff91bE00841F8e04c0495000", |
| 53 | + "https://rpc.ankr.com/eth", |
| 54 | + ) |
| 55 | + .await |
| 56 | + .unwrap(); |
| 57 | + |
| 58 | + assert!(BYTECODE_REGEX.is_match(&bytecode).unwrap()); |
| 59 | + } |
| 60 | + |
| 61 | + #[tokio::test] |
| 62 | + async fn test_get_bytecode_when_target_is_bytecode() { |
| 63 | + let bytecode = get_bytecode_from_target( |
| 64 | + "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", |
| 65 | + "https://rpc.ankr.com/eth", |
| 66 | + ) |
| 67 | + .await |
| 68 | + .unwrap(); |
| 69 | + |
| 70 | + assert!(BYTECODE_REGEX.is_match(&bytecode).unwrap()); |
| 71 | + } |
| 72 | + |
| 73 | + #[tokio::test] |
| 74 | + async fn test_get_bytecode_when_target_is_file_path() { |
| 75 | + let file_path = "./mock-file.txt"; |
| 76 | + let mock_bytecode = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"; |
| 77 | + |
| 78 | + fs::write(file_path, mock_bytecode).unwrap(); |
| 79 | + |
| 80 | + let bytecode = |
| 81 | + get_bytecode_from_target(file_path, "https://rpc.ankr.com/eth").await.unwrap(); |
| 82 | + |
| 83 | + assert!(BYTECODE_REGEX.is_match(&bytecode).unwrap()); |
| 84 | + |
| 85 | + fs::remove_file(file_path).unwrap(); |
| 86 | + } |
| 87 | +} |
0 commit comments