Skip to content

Commit

Permalink
feat(solc): migrate new ethers-solc backend (#660)
Browse files Browse the repository at this point in the history
* chore: migrate to new solc compiler

* chore: migrate tests

* fix: migrate and cleanup

* misc: cleanup

* chore: patch against ethers pr

* misc: rollback ignored test

* bump ethers

* chore: bump ethers

Co-authored-by: Georgios Konstantopoulos <[email protected]>
  • Loading branch information
mattsse and gakonst authored Feb 4, 2022
1 parent b9493ed commit 0b6b202
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 78 deletions.
83 changes: 58 additions & 25 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ debug = true
#ethers-providers = { path = "../ethers-rs/ethers-providers" }
#ethers-signers = { path = "../ethers-rs/ethers-signers" }
#ethers-etherscan = { path = "../ethers-rs/ethers-etherscan" }
#ethers-solc = { path = "../ethers-rs/ethers-solc" }
#ethers-solc = { path = "../ethers-rs/ethers-solc" }
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ once_cell = "1.9.0"
locate-cargo-manifest = "0.2.2"

[dev-dependencies]
foundry-utils = { path = "./../utils", features = ["test"] }
foundry-cli-test-utils = { path = "./test-utils" }
pretty_assertions = "1.0.0"
toml = "0.5"
Expand Down
52 changes: 19 additions & 33 deletions cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ use ethers::{
artifacts::{CompactBytecode, CompactDeployedBytecode},
Graph,
},
solc::{
artifacts::{Source, Sources},
cache::SolFilesCache,
},
solc::{artifacts::Source, cache::SolFilesCache},
};
use std::path::PathBuf;

Expand All @@ -70,11 +67,12 @@ pub trait Cmd: clap::Parser + Sized {
fn run(self) -> eyre::Result<Self::Output>;
}

use ethers::solc::{MinimalCombinedArtifacts, Project, ProjectCompileOutput};
use ethers::solc::{
artifacts::CompactContractBytecode, MinimalCombinedArtifacts, Project, ProjectCompileOutput,
};

/// Compiles the provided [`Project`], throws if there's any compiler error and logs whether
/// compilation was successful or if there was a cache hit.
// TODO: Move this to ethers-solc.
pub fn compile(project: &Project) -> eyre::Result<ProjectCompileOutput<MinimalCombinedArtifacts>> {
if !project.paths.sources.exists() {
eyre::bail!(
Expand All @@ -94,6 +92,7 @@ If you are in a subdirectory in a Git repository, try adding `--root .`"#,
} else if output.is_unchanged() {
println!("no files changed, compilation skipped.");
} else {
println!("{}", output);
println!("success.");
}
Ok(output)
Expand Down Expand Up @@ -122,7 +121,7 @@ pub fn manual_compile(
solc = solc.arg("--allow-paths").arg(project.allowed_lib_paths.to_string());
}

let sources = Graph::resolve_sources(&project.paths, sources)?.into_sources();
let (sources, _) = Graph::resolve_sources(&project.paths, sources)?.into_sources();
let output = project.compile_with_version(&solc, sources)?;
if output.has_compiler_errors() {
// return the diagnostics error back to the user.
Expand Down Expand Up @@ -191,41 +190,28 @@ fn get_artifact_from_name(
}

/// Find using src/ContractSource.sol:ContractName
// TODO: Is there a better / more ergonomic way to get the artifacts given a project and a
// path?
fn get_artifact_from_path(
project: &Project,
path: String,
name: String,
contract_path: String,
contract_name: String,
) -> eyre::Result<(Abi, CompactBytecode, CompactDeployedBytecode)> {
// Get sources from the requested location
let abs_path = dunce::canonicalize(PathBuf::from(path))?;
let mut sources = Sources::new();
sources.insert(abs_path.clone(), Source::read(&abs_path)?);

// Get artifact from the contract name and sources
let mut config = SolFilesCache::builder().insert_files(sources.clone(), None)?;
config.files.entry(abs_path).and_modify(|f| f.artifacts = vec![name.clone()]);

let artifacts = config
.read_artifacts::<MinimalCombinedArtifacts>(project.artifacts_path())?
.into_values()
.collect::<Vec<_>>();

if artifacts.is_empty() {
eyre::bail!("could not find artifact")
} else if artifacts.len() > 1 {
eyre::bail!("duplicate contract name in the same source file")
}
let artifact = artifacts[0].clone();
let abs_path = dunce::canonicalize(PathBuf::from(contract_path))?;

let cache = SolFilesCache::read_joined(&project.paths)?;

// Read the artifact from disk
let artifact: CompactContractBytecode = cache.read_artifact(abs_path, &contract_name)?;

Ok((
artifact.abi.ok_or_else(|| eyre::Error::msg(format!("abi not found for {}", name)))?,
artifact
.abi
.ok_or_else(|| eyre::Error::msg(format!("abi not found for {}", contract_name)))?,
artifact
.bytecode
.ok_or_else(|| eyre::Error::msg(format!("bytecode not found for {}", name)))?,
.ok_or_else(|| eyre::Error::msg(format!("bytecode not found for {}", contract_name)))?,
artifact
.deployed_bytecode
.ok_or_else(|| eyre::Error::msg(format!("bytecode not found for {}", name)))?,
.ok_or_else(|| eyre::Error::msg(format!("bytecode not found for {}", contract_name)))?,
))
}
13 changes: 4 additions & 9 deletions cli/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,10 @@ impl RunArgs {

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

let mut contracts: BTreeMap<String, CompactContractBytecode> = BTreeMap::new();
all_contracts.0.iter().for_each(|(source, output_contracts)| {
contracts.extend(
output_contracts
.iter()
.map(|(n, c)| (format!("{}:{}", source, n), c.clone().into()))
.collect::<BTreeMap<String, CompactContractBytecode>>(),
);
});
let contracts: BTreeMap<String, CompactContractBytecode> = all_contracts
.contracts_with_files()
.map(|(file, name, contract)| (format!("{}:{}", file, name), contract.clone().into()))
.collect();

let mut run_dependencies = vec![];
let mut contract =
Expand Down
Loading

0 comments on commit 0b6b202

Please sign in to comment.