Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: Enable solc optimization (#427)
Browse files Browse the repository at this point in the history
* fix: solc optimization enabled

* feat(solc): optional optimizer and passthrough args

Change optimizer() method to accept an Option<usize> (breaking).
Add args() option to pass arbitrary arguments to solc command.
  • Loading branch information
wolflo authored Sep 2, 2021
1 parent 584b683 commit 664ccfe
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions ethers-core/src/utils/solc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct CompiledContract {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // 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
Expand All @@ -58,14 +58,17 @@ pub struct Solc {
/// The path where contracts will be read from
pub paths: Vec<String>,

/// Number of runs
pub optimizer: usize,
/// Number of optimizer runs. None for no optimization
pub optimizer: Option<usize>,

/// Evm Version
pub evm_version: EvmVersion,

/// Paths for importing other libraries
pub allowed_paths: Vec<PathBuf>,

/// Additional arguments to pass to solc
pub args: Vec<String>,
}

impl Solc {
Expand All @@ -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(),
}
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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<usize>) -> Self {
self.optimizer = runs;
self
}
Expand All @@ -208,6 +221,24 @@ impl Solc {
self.allowed_paths = paths;
self
}

/// Adds an argument to pass to solc
pub fn arg<T: Into<String>>(mut self, arg: T) -> Self {
self.args.push(arg.into());
self
}

/// Adds multiple arguments to pass to solc
pub fn args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for arg in args {
self = self.arg(arg);
}
self
}
}

#[derive(Clone, Debug)]
Expand Down

0 comments on commit 664ccfe

Please sign in to comment.