Skip to content

Commit

Permalink
[WIP] feat: implement load_abi util function
Browse files Browse the repository at this point in the history
  • Loading branch information
TAMARA LIPOWSKI committed Oct 16, 2024
1 parent 5b2bdc0 commit 3fee6bb
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down
1 change: 1 addition & 0 deletions src/protocol/vm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod utils;
95 changes: 95 additions & 0 deletions src/protocol/vm/utils.rs
Original file line number Diff line number Diff line change
@@ -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<Value, std::io::Error> {
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<String> = 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"));
}
}

0 comments on commit 3fee6bb

Please sign in to comment.