Skip to content

Commit

Permalink
feat(chisel): determine proper path to Vm.sol based on proj remappings (
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored Jan 19, 2025
1 parent b0630f9 commit 18cb6f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/chisel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ time = { version = "0.3", features = ["formatting"] }
tokio = { workspace = true, features = ["full"] }
yansi.workspace = true
tracing.workspace = true
walkdir.workspace = true

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }
Expand Down
30 changes: 21 additions & 9 deletions crates/chisel/src/session_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use semver::Version;
use serde::{Deserialize, Serialize};
use solang_parser::{diagnostics::Diagnostic, pt};
use std::{fs, path::PathBuf};
use walkdir::WalkDir;
use yansi::Paint;

/// The minimum Solidity version of the `Vm` interface.
Expand Down Expand Up @@ -466,15 +467,26 @@ contract {contract_name} is Script {{
pub fn to_repl_source(&self) -> String {
let Version { major, minor, patch, .. } = self.solc.version;
let Self { contract_name, global_code, top_level_code, run_code, config, .. } = self;

let (vm_import, vm_constant) = if !config.no_vm {
(
"import {Vm} from \"forge-std/Vm.sol\";\n",
"Vm internal constant vm = Vm(address(uint160(uint256(keccak256(\"hevm cheat code\")))));\n"
)
} else {
("", "")
};
let (mut vm_import, mut vm_constant) = (String::new(), String::new());
if !config.no_vm {
// Check if there's any `forge-std` remapping and determine proper path to it by
// searching remapping path.
if let Some(remapping) = config
.foundry_config
.remappings
.iter()
.find(|remapping| remapping.name == "forge-std/")
{
if let Some(vm_path) = WalkDir::new(&remapping.path.path)
.into_iter()
.filter_map(|e| e.ok())
.find(|e| e.file_name() == "Vm.sol")
{
vm_import = format!("import {{Vm}} from \"{}\";\n", vm_path.path().display());
vm_constant = "Vm internal constant vm = Vm(address(uint160(uint256(keccak256(\"hevm cheat code\")))));\n".to_string();
}
}
}

format!(
r#"
Expand Down

0 comments on commit 18cb6f9

Please sign in to comment.