Struct ethers_solc::Project
source · [−]pub struct Project<T: ArtifactOutput = ConfigurableArtifacts> {
pub paths: ProjectPathsConfig,
pub solc: Solc,
pub solc_config: SolcConfig,
pub cached: bool,
pub no_artifacts: bool,
pub auto_detect: bool,
pub artifacts: T,
pub ignored_error_codes: Vec<u64>,
pub allowed_lib_paths: AllowedLibPaths,
pub offline: bool,
/* private fields */
}Expand description
Represents a project workspace and handles solc compiling of all contracts in that workspace.
Fields
paths: ProjectPathsConfigThe layout of the project
solc: SolcWhere to find solc
solc_config: SolcConfigHow solc invocation should be configured.
cached: boolWhether caching is enabled
no_artifacts: boolWhether writing artifacts to disk is enabled
auto_detect: boolWhether writing artifacts to disk is enabled
artifacts: THandles all artifacts related tasks, reading and writing from the artifact dir.
ignored_error_codes: Vec<u64>Errors/Warnings which match these error codes are not going to be logged
allowed_lib_paths: AllowedLibPathsThe paths which will be allowed for library inclusion
offline: boolOffline mode, if set, network access (download solc) is disallowed
Implementations
sourceimpl Project
impl Project
sourcepub fn builder() -> ProjectBuilder
pub fn builder() -> ProjectBuilder
Convenience function to call ProjectBuilder::default()
Example
Configure with ConfigurableArtifacts artifacts output
use ethers_solc::Project;
let config = Project::builder().build().unwrap();To configure any a project with any ArtifactOutput use either
use ethers_solc::Project;
let config = Project::builder().build().unwrap();or use the builder directly
use ethers_solc::{ConfigurableArtifacts, ProjectBuilder};
let config = ProjectBuilder::<ConfigurableArtifacts>::default().build().unwrap();sourceimpl<T: ArtifactOutput> Project<T>
impl<T: ArtifactOutput> Project<T>
sourcepub fn artifacts_path(&self) -> &PathBuf
pub fn artifacts_path(&self) -> &PathBuf
Returns the path to the artifacts directory
sourcepub fn sources_path(&self) -> &PathBuf
pub fn sources_path(&self) -> &PathBuf
Returns the path to the sources directory
sourcepub fn cache_path(&self) -> &PathBuf
pub fn cache_path(&self) -> &PathBuf
Returns the path to the cache file
sourcepub fn artifacts_handler(&self) -> &T
pub fn artifacts_handler(&self) -> &T
Returns the handler that takes care of processing all artifacts
sourcepub fn read_cache_file(&self) -> Result<SolFilesCache>
pub fn read_cache_file(&self) -> Result<SolFilesCache>
Convenience function to read the cache file. See also SolFilesCache::read_joined()
sourcepub fn set_solc_jobs(&mut self, jobs: usize)
pub fn set_solc_jobs(&mut self, jobs: usize)
sourcepub fn sources(&self) -> Result<Sources>
pub fn sources(&self) -> Result<Sources>
Returns all sources found under the project’s configured sources path
sourcepub fn rerun_if_sources_changed(&self)
pub fn rerun_if_sources_changed(&self)
This emits the cargo rerun-if-changed instruction.
Which tells Cargo to re-run the build script if a file inside the project’s sources
directory has changed.
Use this if you compile a project in a build.rs file.
Example build.rs file
use ethers_solc::{Project, ProjectPathsConfig};
// configure the project with all its paths, solc, cache etc. where the root dir is the current rust project.
let project = Project::builder()
.paths(ProjectPathsConfig::hardhat(env!("CARGO_MANIFEST_DIR")).unwrap())
.build()
.unwrap();
let output = project.compile().unwrap();
// Tell Cargo that if a source file changes, to rerun this build script.
project.rerun_if_sources_changed();sourcepub fn compile(&self) -> Result<ProjectCompileOutput<T>>
pub fn compile(&self) -> Result<ProjectCompileOutput<T>>
Attempts to compile the contracts found at the configured source location, see
ProjectPathsConfig::sources.
NOTE: this does not check if the contracts were successfully compiled, see
CompilerOutput::has_error instead.
NB: If the svm feature is enabled, this function will automatically detect
solc versions across files.
Example
use ethers_solc::Project;
let project = Project::builder().build().unwrap();
let output = project.compile().unwrap();sourcepub fn compile_files<P, I>(&self, files: I) -> Result<ProjectCompileOutput<T>> where
I: IntoIterator<Item = P>,
P: Into<PathBuf>,
pub fn compile_files<P, I>(&self, files: I) -> Result<ProjectCompileOutput<T>> where
I: IntoIterator<Item = P>,
P: Into<PathBuf>,
Convenience function to compile a series of solidity files with the project’s settings.
Same as [Self::svm_compile()] but with the given files as input.
Example
use ethers_solc::Project;
let project = Project::builder().build().unwrap();
let output = project
.compile_files(
vec!["examples/Foo.sol", "examples/Bar.sol"]
).unwrap();sourcepub fn compile_sparse<F: FileFilter + 'static>(
&self,
filter: F
) -> Result<ProjectCompileOutput<T>>
pub fn compile_sparse<F: FileFilter + 'static>(
&self,
filter: F
) -> Result<ProjectCompileOutput<T>>
Convenience function to compile only (re)compile files that match the provided FileFilter.
Same as [Self::svm_compile()] but with only with those files as input that match
FileFilter::is_match().
Example - Only compile Test files
use ethers_solc::{Project, TestFileFilter};
let project = Project::builder().build().unwrap();
let output = project
.compile_sparse(
TestFileFilter::default()
).unwrap();Example - Apply a custom filter
use std::path::Path;
use ethers_solc::Project;
let project = Project::builder().build().unwrap();
let output = project
.compile_sparse(
|path: &Path| path.ends_with("Greeter.sol")
).unwrap();sourcepub fn compile_with_version(
&self,
solc: &Solc,
sources: Sources
) -> Result<ProjectCompileOutput<T>>
pub fn compile_with_version(
&self,
solc: &Solc,
sources: Sources
) -> Result<ProjectCompileOutput<T>>
Compiles the given source files with the exact Solc executable
First all libraries for the sources are resolved by scanning all their imports.
If caching is enabled for the Project, then all unchanged files are filtered from the
sources and their existing artifacts are read instead. This will also update the cache
file and cleans up entries for files which may have been removed. Unchanged files that
for which an artifact exist, are not compiled again.
Example
use ethers_solc::{Project, Solc};
let project = Project::builder().build().unwrap();
let sources = project.paths.read_sources().unwrap();
project
.compile_with_version(
&Solc::find_svm_installed_version("0.8.11").unwrap().unwrap(),
sources,
)
.unwrap();sourcepub fn cleanup(&self) -> Result<(), SolcIoError>
pub fn cleanup(&self) -> Result<(), SolcIoError>
Removes the project’s artifacts and cache file
If the cache file was the only file in the folder, this also removes the empty folder.
Example
use ethers_solc::Project;
let project = Project::builder().build().unwrap();
let _ = project.compile().unwrap();
assert!(project.artifacts_path().exists());
assert!(project.cache_path().exists());
project.cleanup();
assert!(!project.artifacts_path().exists());
assert!(!project.cache_path().exists());sourcepub fn flatten(&self, target: &Path) -> Result<String>
pub fn flatten(&self, target: &Path) -> Result<String>
Flattens the target solidity file into a single string suitable for verification.
This method uses a dependency graph to resolve imported files and substitute import directives with the contents of target files. It will strip the pragma version directives and SDPX license identifiers from all imported files.
NB: the SDPX license identifier will be removed from the imported file only if it is found at the beginning of the file.
sourcepub fn standard_json_input(
&self,
target: impl AsRef<Path>
) -> Result<StandardJsonCompilerInput>
pub fn standard_json_input(
&self,
target: impl AsRef<Path>
) -> Result<StandardJsonCompilerInput>
Returns standard-json-input to compile the target contract
Trait Implementations
sourceimpl<T: ArtifactOutput> ArtifactOutput for Project<T>
impl<T: ArtifactOutput> ArtifactOutput for Project<T>
type Artifact = <T as ArtifactOutput>::Artifact
type Artifact = <T as ArtifactOutput>::Artifact
Represents the artifact that will be stored for a Contract
sourcefn on_output(
&self,
contracts: &VersionedContracts,
sources: &VersionedSourceFiles,
layout: &ProjectPathsConfig
) -> Result<Artifacts<Self::Artifact>>
fn on_output(
&self,
contracts: &VersionedContracts,
sources: &VersionedSourceFiles,
layout: &ProjectPathsConfig
) -> Result<Artifacts<Self::Artifact>>
Handle the aggregated set of compiled contracts from the solc crate::CompilerOutput. Read more
sourcefn write_contract_extras(&self, contract: &Contract, file: &Path) -> Result<()>
fn write_contract_extras(&self, contract: &Contract, file: &Path) -> Result<()>
Write additional files for the contract
sourcefn write_extras(
&self,
contracts: &VersionedContracts,
layout: &ProjectPathsConfig
) -> Result<()>
fn write_extras(
&self,
contracts: &VersionedContracts,
layout: &ProjectPathsConfig
) -> Result<()>
Writes additional files for the contracts if the included in the Contract, such as ir,
ewasm, iropt. Read more
sourcefn output_file_name(name: impl AsRef<str>) -> PathBuf
fn output_file_name(name: impl AsRef<str>) -> PathBuf
Returns the file name for the contract’s artifact
Greeter.json Read more
sourcefn output_file_name_versioned(
name: impl AsRef<str>,
version: &Version
) -> PathBuf
fn output_file_name_versioned(
name: impl AsRef<str>,
version: &Version
) -> PathBuf
Returns the file name for the contract’s artifact and the given version
Greeter.0.8.11.json Read more
sourcefn output_file(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>
) -> PathBuf
fn output_file(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>
) -> PathBuf
Returns the path to the contract’s artifact location based on the contract’s file and name Read more
sourcefn output_file_versioned(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>,
version: &Version
) -> PathBuf
fn output_file_versioned(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>,
version: &Version
) -> PathBuf
Returns the path to the contract’s artifact location based on the contract’s file, name and version Read more
sourcefn contract_name(file: impl AsRef<Path>) -> Option<String>
fn contract_name(file: impl AsRef<Path>) -> Option<String>
The inverse of contract_file_name Read more
sourcefn output_exists(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>,
root: impl AsRef<Path>
) -> bool
fn output_exists(
contract_file: impl AsRef<Path>,
name: impl AsRef<str>,
root: impl AsRef<Path>
) -> bool
Whether the corresponding artifact of the given contract file and name exists
sourcefn read_cached_artifact(path: impl AsRef<Path>) -> Result<Self::Artifact>
fn read_cached_artifact(path: impl AsRef<Path>) -> Result<Self::Artifact>
Read the artifact that’s stored at the given path Read more
sourcefn read_cached_artifacts<P, I>(
files: I
) -> Result<BTreeMap<PathBuf, Self::Artifact>> where
I: IntoIterator<Item = P>,
P: Into<PathBuf>,
fn read_cached_artifacts<P, I>(
files: I
) -> Result<BTreeMap<PathBuf, Self::Artifact>> where
I: IntoIterator<Item = P>,
P: Into<PathBuf>,
Read the cached artifacts that are located the paths the iterator yields Read more
sourcefn contract_to_artifact(
&self,
file: &str,
name: &str,
contract: Contract,
source_file: Option<&SourceFile>
) -> Self::Artifact
fn contract_to_artifact(
&self,
file: &str,
name: &str,
contract: Contract,
source_file: Option<&SourceFile>
) -> Self::Artifact
Convert a contract to the artifact type Read more
sourcefn output_to_artifacts(
&self,
contracts: &VersionedContracts,
sources: &VersionedSourceFiles
) -> Artifacts<Self::Artifact>
fn output_to_artifacts(
&self,
contracts: &VersionedContracts,
sources: &VersionedSourceFiles
) -> Artifacts<Self::Artifact>
Convert the compiler output into a set of artifacts Read more
sourcefn standalone_source_file_to_artifact(
&self,
path: &str,
file: &VersionedSourceFile
) -> Option<Self::Artifact>
fn standalone_source_file_to_artifact(
&self,
path: &str,
file: &VersionedSourceFile
) -> Option<Self::Artifact>
This converts a SourceFile that doesn’t contain any contract definitions (interfaces,
contracts, libraries) to an artifact. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for Project<T> where
T: RefUnwindSafe,
impl<T> Send for Project<T> where
T: Send,
impl<T> Sync for Project<T> where
T: Sync,
impl<T> Unpin for Project<T> where
T: Unpin,
impl<T> UnwindSafe for Project<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more