diff --git a/ethers-core/src/utils/solc.rs b/ethers-core/src/utils/solc.rs index 175c295d..e3886169 100644 --- a/ethers-core/src/utils/solc.rs +++ b/ethers-core/src/utils/solc.rs @@ -45,7 +45,7 @@ pub struct CompiledContract { /// # fn main() -> Result<(), Box> { /// // Give it a glob /// let contracts = Solc::new("./contracts/*") -/// .optimizer(200) +/// .optimizer(Some(200)) /// .build()?; /// /// // this will return None if the specified contract did not exist in the compiled @@ -58,14 +58,17 @@ pub struct Solc { /// The path where contracts will be read from pub paths: Vec, - /// Number of runs - pub optimizer: usize, + /// Number of optimizer runs. None for no optimization + pub optimizer: Option, /// Evm Version pub evm_version: EvmVersion, /// Paths for importing other libraries pub allowed_paths: Vec, + + /// Additional arguments to pass to solc + pub args: Vec, } impl Solc { @@ -80,9 +83,10 @@ impl Solc { Self { paths, - optimizer: 200, // default optimizer runs = 200 + optimizer: Some(200), // default optimizer runs = 200 evm_version: EvmVersion::Istanbul, allowed_paths: Vec::new(), + args: Vec::new(), } } @@ -96,6 +100,15 @@ impl Solc { .arg("--combined-json") .arg("abi,bin"); + if let Some(runs) = self.optimizer { + command + .arg("--optimize") + .arg("--optimize-runs") + .arg(runs.to_string()); + } + + command.args(self.args); + for path in self.paths { command.arg(path); } @@ -196,8 +209,8 @@ impl Solc { self } - /// Sets the optimizer runs (default = 200) - pub fn optimizer(mut self, runs: usize) -> Self { + /// Sets the optimizer runs (default = 200). None indicates no optimization + pub fn optimizer(mut self, runs: Option) -> Self { self.optimizer = runs; self } @@ -208,6 +221,24 @@ impl Solc { self.allowed_paths = paths; self } + + /// Adds an argument to pass to solc + pub fn arg>(mut self, arg: T) -> Self { + self.args.push(arg.into()); + self + } + + /// Adds multiple arguments to pass to solc + pub fn args(mut self, args: I) -> Self + where + I: IntoIterator, + S: Into, + { + for arg in args { + self = self.arg(arg); + } + self + } } #[derive(Clone, Debug)]