Skip to content

refactor(forge): use new ethers-solc functions in forge run #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

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

36 changes: 6 additions & 30 deletions cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ pub mod verify;
use crate::opts::forge::ContractInfo;
use ethers::{
abi::Abi,
prelude::{
artifacts::{CompactBytecode, CompactDeployedBytecode},
Graph,
},
solc::{artifacts::Source, cache::SolFilesCache},
prelude::artifacts::{CompactBytecode, CompactDeployedBytecode},
solc::cache::SolFilesCache,
};
use std::path::PathBuf;

Expand Down Expand Up @@ -97,35 +94,14 @@ If you are in a subdirectory in a Git repository, try adding `--root .`"#,
Ok(output)
}

/// Manually compile a project with added sources
pub fn manual_compile(
project: &Project,
added_sources: Vec<PathBuf>,
) -> eyre::Result<ProjectCompileOutput> {
let mut sources = project.paths.read_input_files()?;
sources.extend(Source::read_all_files(added_sources)?);
/// Compile a set of files not necessarily included in the `project`'s source dir
pub fn compile_files(project: &Project, files: Vec<PathBuf>) -> eyre::Result<ProjectCompileOutput> {
println!("compiling...");
if project.auto_detect {
tracing::trace!("using solc auto detection to compile sources");
let output = project.svm_compile(sources)?;
if output.has_compiler_errors() {
// return the diagnostics error back to the user.
eyre::bail!(output.to_string())
}
return Ok(output)
}

let mut solc = project.solc.clone();
if !project.allowed_lib_paths.is_empty() {
solc = solc.arg("--allow-paths").arg(project.allowed_lib_paths.to_string());
}

let (sources, _) = Graph::resolve_sources(&project.paths, sources)?.into_sources();
let output = project.compile_with_version(&solc, sources)?;
let output = project.compile_files(files)?;
if output.has_compiler_errors() {
// return the diagnostics error back to the user.
eyre::bail!(output.to_string())
}
println!("success.");
Ok(output)
}

Expand Down
32 changes: 4 additions & 28 deletions cli/src/cmd/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmd::{build::BuildArgs, compile, manual_compile, Cmd};
use crate::cmd::{build::BuildArgs, compile_files, Cmd};
use clap::{Parser, ValueHint};
use evm_adapters::sputnik::cheatcodes::{CONSOLE_ABI, HEVMCONSOLE_ABI, HEVM_ABI};

Expand Down Expand Up @@ -247,7 +247,7 @@ impl Cmd for RunArgs {
}

println!("Gas Used: {}", result.gas_used);
println!("== Logs == ");
println!("== Logs ==");
result.logs.iter().for_each(|log| println!("{}", log));
}

Expand Down Expand Up @@ -275,32 +275,8 @@ impl RunArgs {
/// Compiles the file with auto-detection and compiler params.
pub fn build(&self, config: Config, evm_opts: &EvmOpts) -> eyre::Result<BuildOutput> {
let target_contract = dunce::canonicalize(&self.path)?;
let (project, output) = if let Ok(mut project) = config.project() {
// TODO: caching causes no output until https://github.com/gakonst/ethers-rs/issues/727
// is fixed
project.cached = false;
project.no_artifacts = true;

// target contract may not be in the compilation path, add it and manually compile
match manual_compile(&project, vec![target_contract]) {
Ok(output) => (project, output),
Err(e) => {
println!("No extra contracts compiled {:?}", e);
let mut target_project = config.ephemeral_no_artifacts_project()?;
target_project.cached = false;
target_project.no_artifacts = true;
let res = compile(&target_project)?;
(target_project, res)
}
}
} else {
let mut target_project = config.ephemeral_no_artifacts_project()?;
target_project.cached = false;
target_project.no_artifacts = true;
let res = compile(&target_project)?;
(target_project, res)
};
println!("success.");
let project = config.ephemeral_no_artifacts_project()?;
let output = compile_files(&project, vec![target_contract])?;

let (sources, all_contracts) = output.output().split();

Expand Down
39 changes: 39 additions & 0 deletions cli/tests/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Contains various tests for checking forge's commands
use ansi_term::Colour;
use ethers::solc::{artifacts::Metadata, ConfigurableContractArtifact};
use evm_adapters::evm_opts::{EvmOpts, EvmType};
use foundry_cli_test_utils::{
Expand Down Expand Up @@ -316,6 +317,44 @@ success.
);
});

// tests that the `run` command works correctly
forgetest!(can_execute_run_command, |prj: TestProject, mut cmd: TestCommand| {
let script = prj
.inner()
.add_source(
"Foo",
r#"
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;
contract Demo {
event log_string(string);
function run() external {
emit log_string("script ran");
}
}
"#,
)
.unwrap();

cmd.arg("run").arg(script);
let output = cmd.stdout_lossy();
assert_eq!(
format!(
"compiling...
Compiling 1 files with 0.8.10
Compilation finished successfully
success.
{}
Gas Used: 1751
== Logs ==
script ran
",
Colour::Green.paint("Script ran successfully.")
),
output
);
});

// test against a local checkout, useful to debug with local ethers-rs patch
forgetest_ignore!(can_compile_local_spells, |_: TestProject, mut cmd: TestCommand| {
let current_dir = std::env::current_dir().unwrap();
Expand Down