From 3fee6bb70572efa405c31e9fe0332fa8f0bea2d5 Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Wed, 16 Oct 2024 19:30:39 -0400 Subject: [PATCH] [WIP] feat: implement load_abi util function --- Cargo.lock | 30 +++++++++---- Cargo.toml | 2 + src/protocol/mod.rs | 1 + src/protocol/vm/mod.rs | 1 + src/protocol/vm/utils.rs | 95 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 src/protocol/vm/mod.rs create mode 100644 src/protocol/vm/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 84396322..3d5103da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3389,9 +3389,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" [[package]] name = "fastrlp" @@ -4995,9 +4995,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.155" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "libm" @@ -6113,6 +6113,7 @@ dependencies = [ "starknet_in_rust", "strum 0.25.0", "strum_macros 0.25.2", + "tempfile", "thiserror", "tokio", "tokio-tungstenite 0.20.1", @@ -6122,6 +6123,7 @@ dependencies = [ "tycho-client", "tycho-core", "uuid 1.4.1", + "walkdir", "warp", ] @@ -6850,9 +6852,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.34" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.6.0", "errno", @@ -8122,14 +8124,15 @@ checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" [[package]] name = "tempfile" -version = "3.10.1" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", "fastrand", + "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -9198,6 +9201,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-targets" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 27aa5f54..e6f3033e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,8 @@ cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"], optional = true } foundry-config = { git = "https://github.com/foundry-rs/foundry", rev = "2544793" } foundry-evm = { git = "https://github.com/foundry-rs/foundry", rev = "2544793" } revm-inspectors = { version = "0.5", features = ["serde"] } +walkdir = "2.5.0" +tempfile = "3.13.0" [dev-dependencies] mockito = "1.1.1" diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 55f161fd..9c373cc9 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -10,6 +10,7 @@ pub mod state; pub mod tycho; pub mod uniswap_v2; pub mod uniswap_v3; +mod vm; /// A trait for converting types to and from `Bytes`. /// diff --git a/src/protocol/vm/mod.rs b/src/protocol/vm/mod.rs new file mode 100644 index 00000000..95adf51c --- /dev/null +++ b/src/protocol/vm/mod.rs @@ -0,0 +1 @@ +mod utils; diff --git a/src/protocol/vm/utils.rs b/src/protocol/vm/utils.rs new file mode 100644 index 00000000..12c8d5f2 --- /dev/null +++ b/src/protocol/vm/utils.rs @@ -0,0 +1,95 @@ +// TODO: remove skip for clippy dead_code check +#![allow(dead_code)] + +use serde_json::Value; +use std::{ + fs::File, + io::Read, + path::{Path, PathBuf}, +}; +use walkdir::WalkDir; + +fn assets_folder() -> PathBuf { + // Get the directory of the current file (similar to Python's __file__) + let current_file = Path::new(file!()); + + // Go one level up (parent directory) and then add the "assets" folder + let assets_folder = current_file + .parent() + .unwrap() + .join("assets"); + + assets_folder +} + +fn load_abi(name_or_path: &str) -> Result { + let assets_folder = assets_folder(); + + let path = if Path::new(name_or_path).exists() { + PathBuf::from(name_or_path) + } else { + PathBuf::from(assets_folder.clone()).join(format!("{}.abi", name_or_path)) + }; + + match File::open(&path) { + Ok(mut file) => { + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(serde_json::from_str(&contents)?) + } + Err(_) => { + let available_files: Vec = WalkDir::new(&assets_folder) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| { + e.path() + .extension() + .map_or(false, |ext| ext == "abi") + }) + .filter_map(|e| e.path().strip_prefix(&assets_folder).ok().map(|p| p.to_owned())) + .filter_map(|p| { + p.to_str() + .map(|s| s.replace(".abi", "")) + }) + .collect(); + + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + format!( + "File {} not found. Did you mean one of these? {}", + name_or_path, + available_files.join(", ") + ), + )) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::TempDir; + + #[test] + fn test_load_abi() { + let assets_folder = assets_folder(); + + // Create a test ABI file + let test_abi = r#"{"test": "abi"}"#; + let test_file_path = assets_folder.join("test.abi"); + std::fs::write(&test_file_path, test_abi).unwrap(); + + // Test loading an existing file + let result = load_abi(test_file_path.to_str().unwrap()); + assert!(result.is_ok()); + assert_eq!(result.unwrap(), serde_json::json!({"test": "abi"})); + + // Test loading a non-existent file + let result = load_abi("non_existent"); + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Did you mean one of these? test")); + } +}