diff --git a/ethers-solc/src/lib.rs b/ethers-solc/src/lib.rs index 4bfa68cb7..92bfc155e 100644 --- a/ethers-solc/src/lib.rs +++ b/ethers-solc/src/lib.rs @@ -33,6 +33,7 @@ use crate::{ error::{SolcError, SolcIoError}, }; use error::Result; +use serde::{Deserialize, Serialize}; use std::{ borrow::Cow, collections::BTreeMap, convert::TryInto, fmt, fs, marker::PhantomData, path::PathBuf, @@ -45,6 +46,21 @@ pub mod project_util; /// Name of the json file containing the compiler versions used per file pub const SOLIDITY_VERSIONS: &str = "solidity-versions.json"; +#[derive(Clone, Serialize, Deserialize)] +/// Deserialized version of artifacts file which contains the paths and solidity +/// versions for each artifact. +pub struct SourceVersions { + /// The optimizer runs used during compilation + // NB: Currently we compile all versions with the same [`Settings`] object. + // If that were to change, we'd need to move this filed inside the map, depending + // on how we determine which files get compiled with optimizations on and which not. + #[serde(skip_serializing_if = "Option::is_none")] + pub optimizer_runs: Option, + /// Mapping from compiler version string to a vector of all the files + /// that were compiled with that compiler + pub versions: BTreeMap>, +} + /// Represents a project workspace and handles `solc` compiling of all contracts in that workspace. #[derive(Debug)] pub struct Project { @@ -335,7 +351,7 @@ impl Project { /// Returns the `solidity-versions.json` file under the artifacts directory /// which contains the Solidity compiler versions which were used to compile /// each file. - pub fn source_versions(&self) -> Result>> { + pub fn source_versions(&self) -> Result { let versions_file_path = self.artifacts_path().join(SOLIDITY_VERSIONS); let versions_file = File::open(versions_file_path)?; Ok(serde_json::from_reader(versions_file)?) @@ -345,6 +361,7 @@ impl Project { &self, sources_by_version: BTreeMap>, ) -> Result<()> { + let runs = self.solc_config.settings.optimizer.runs; let solidity_versions = sources_by_version .iter() .map(|(solc, sources)| { @@ -352,12 +369,13 @@ impl Project { // skips the platform from the compiler version string let trimmed = &version.build.as_str()[..15]; version.build = semver::BuildMetadata::new(trimmed)?; - Ok((format!("v{}", version), sources.keys().collect::>())) + Ok((format!("v{}", version), sources.keys().cloned().collect::>())) }) .collect::>>()?; + let sources_versions = SourceVersions { optimizer_runs: runs, versions: solidity_versions }; let versions_file_path = self.artifacts_path().join(SOLIDITY_VERSIONS); let versions_file = File::create(versions_file_path)?; - serde_json::to_writer(versions_file, &solidity_versions)?; + serde_json::to_writer(versions_file, &sources_versions)?; Ok(()) } diff --git a/ethers-solc/tests/project.rs b/ethers-solc/tests/project.rs index 95ea4ee0e..19efc40fc 100644 --- a/ethers-solc/tests/project.rs +++ b/ethers-solc/tests/project.rs @@ -2,10 +2,9 @@ use ethers_solc::{ cache::SOLIDITY_FILES_CACHE_FILENAME, project_util::*, MinimalCombinedArtifacts, Project, - ProjectPathsConfig, SOLIDITY_VERSIONS, + ProjectPathsConfig, SourceVersions, SOLIDITY_VERSIONS, }; use std::{ - collections::BTreeMap, io, path::{Path, PathBuf}, }; @@ -55,7 +54,7 @@ fn can_compile_dapp_sample() { let solc_versions_path = project.project().artifacts_path().join(SOLIDITY_VERSIONS); let reader = std::fs::File::open(solc_versions_path).unwrap(); - let _: BTreeMap> = serde_json::from_reader(reader).unwrap(); + let _: SourceVersions = serde_json::from_reader(reader).unwrap(); // delete artifacts std::fs::remove_dir_all(&project.paths().artifacts).unwrap();