fix: Enable solc optimization (#427)

* 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.
This commit is contained in:
wolflo 2021-09-02 04:18:04 -06:00 committed by GitHub
parent 584b683c12
commit 664ccfe9d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 6 deletions

View File

@ -45,7 +45,7 @@ pub struct CompiledContract {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Give it a glob /// // Give it a glob
/// let contracts = Solc::new("./contracts/*") /// let contracts = Solc::new("./contracts/*")
/// .optimizer(200) /// .optimizer(Some(200))
/// .build()?; /// .build()?;
/// ///
/// // this will return None if the specified contract did not exist in the compiled /// // 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 /// The path where contracts will be read from
pub paths: Vec<String>, pub paths: Vec<String>,
/// Number of runs /// Number of optimizer runs. None for no optimization
pub optimizer: usize, pub optimizer: Option<usize>,
/// Evm Version /// Evm Version
pub evm_version: EvmVersion, pub evm_version: EvmVersion,
/// Paths for importing other libraries /// Paths for importing other libraries
pub allowed_paths: Vec<PathBuf>, pub allowed_paths: Vec<PathBuf>,
/// Additional arguments to pass to solc
pub args: Vec<String>,
} }
impl Solc { impl Solc {
@ -80,9 +83,10 @@ impl Solc {
Self { Self {
paths, paths,
optimizer: 200, // default optimizer runs = 200 optimizer: Some(200), // default optimizer runs = 200
evm_version: EvmVersion::Istanbul, evm_version: EvmVersion::Istanbul,
allowed_paths: Vec::new(), allowed_paths: Vec::new(),
args: Vec::new(),
} }
} }
@ -96,6 +100,15 @@ impl Solc {
.arg("--combined-json") .arg("--combined-json")
.arg("abi,bin"); .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 { for path in self.paths {
command.arg(path); command.arg(path);
} }
@ -196,8 +209,8 @@ impl Solc {
self self
} }
/// Sets the optimizer runs (default = 200) /// Sets the optimizer runs (default = 200). None indicates no optimization
pub fn optimizer(mut self, runs: usize) -> Self { pub fn optimizer(mut self, runs: Option<usize>) -> Self {
self.optimizer = runs; self.optimizer = runs;
self self
} }
@ -208,6 +221,24 @@ impl Solc {
self.allowed_paths = paths; self.allowed_paths = paths;
self 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)] #[derive(Clone, Debug)]