From da70e0caab79935defd7484ae15ca5330e7f7098 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 2 Oct 2021 04:02:16 +0200 Subject: [PATCH] feat: allow to configure combined-json (#483) --- ethers-core/src/utils/solc.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/ethers-core/src/utils/solc.rs b/ethers-core/src/utils/solc.rs index af796fcd..5beecaa9 100644 --- a/ethers-core/src/utils/solc.rs +++ b/ethers-core/src/utils/solc.rs @@ -97,6 +97,10 @@ pub struct Solc { /// Paths for importing other libraries pub allowed_paths: Vec, + /// Output a single json document containing the specified information. + /// Default is `abi,bin,bin-runtime` + pub combined_json: Option, + /// Additional arguments to pass to solc pub args: Vec, } @@ -121,18 +125,21 @@ impl Solc { optimizer: Some(200), // default optimizer runs = 200 evm_version: EvmVersion::Istanbul, allowed_paths: Vec::new(), + combined_json: Some("abi,bin,bin-runtime".to_string()), args: Vec::new(), } } - /// Gets the ABI for the contracts - pub fn build_raw(self) -> Result> { + /// Gets the complete solc output as json object + pub fn exec(self) -> Result { let path = self.solc_path.unwrap_or_else(|| PathBuf::from(SOLC)); let mut command = Command::new(&path); let version = Solc::version(Some(path)); - command.arg("--combined-json").arg("abi,bin,bin-runtime"); + if let Some(combined_json) = self.combined_json { + command.arg("--combined-json").arg(combined_json); + } if let Some(evm_version) = normalize_evm_version(&version, self.evm_version) { command.arg("--evm-version").arg(evm_version.to_string()); @@ -160,7 +167,12 @@ impl Solc { } // Deserialize the output - let mut output: serde_json::Value = serde_json::from_slice(&command.stdout)?; + Ok(serde_json::from_slice(&command.stdout)?) + } + + /// Gets the ABI for the contracts + pub fn build_raw(self) -> Result> { + let mut output = self.exec()?; let contract_values = output["contracts"].as_object_mut().ok_or_else(|| { SolcError::SolcError("no contracts found in `solc` output".to_string()) })?; @@ -281,6 +293,13 @@ impl Solc { self } + /// Sets the `combined-json` option, by default this is set to `abi,bin,bin-runtime` + /// NOTE: In order to get the `CompiledContract` from `Self::build`, this _must_ contain `abi,bin`. + pub fn combined_json(mut self, combined_json: impl Into) -> Self { + self.combined_json = Some(combined_json.into()); + self + } + /// Sets the optimizer runs (default = 200). None indicates no optimization /// /// ```rust,no_run