Struct ethers_solc::Project

source ·
pub struct Project<T: ArtifactOutput = ConfigurableArtifacts> {
Show 14 fields pub paths: ProjectPathsConfig, pub solc: Solc, pub solc_config: SolcConfig, pub cached: bool, pub build_info: bool, pub no_artifacts: bool, pub auto_detect: bool, pub artifacts: T, pub ignored_error_codes: Vec<u64>, pub compiler_severity_filter: Severity, pub allowed_paths: AllowedLibPaths, pub include_paths: IncludePaths, pub offline: bool, pub slash_paths: bool, /* private fields */
}
Expand description

Represents a project workspace and handles solc compiling of all contracts in that workspace.

Fields§

§paths: ProjectPathsConfig

The layout of the project

§solc: Solc

Where to find solc

§solc_config: SolcConfig

How solc invocation should be configured.

§cached: bool

Whether caching is enabled

§build_info: bool

Whether to output build information with each solc call.

§no_artifacts: bool

Whether writing artifacts to disk is enabled

§auto_detect: bool

Whether writing artifacts to disk is enabled

§artifacts: T

Handles 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

§compiler_severity_filter: Severity

The minimum severity level that is treated as a compiler error

§allowed_paths: AllowedLibPaths

The paths which will be allowed for library inclusion

§include_paths: IncludePaths

The paths which will be used with solc’s --include-path attribute

§offline: bool

Offline mode, if set, network access (download solc) is disallowed

§slash_paths: bool

Windows only config value to ensure the all paths use / instead of \\, same as solc

This is a noop on other platforms

Implementations§

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();

Returns the path to the artifacts directory

Returns the path to the sources directory

Returns the path to the cache file

Returns the path to the build-info directory nested in the artifacts dir

Returns the root directory of the project

Returns the handler that takes care of processing all artifacts

Convenience function to read the cache file. See also SolFilesCache::read_joined()

Sets the maximum number of parallel solc processes to run simultaneously.

Panics

if jobs == 0

Returns all sources found under the project’s configured sources path

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();

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();

Convenience function to compile a series of solidity files with the project’s settings. Same as Self::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();

Convenience function to compile only (re)compile files that match the provided FileFilter. Same as Self::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();

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();

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());

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.

Returns standard-json-input to compile the target contract

Trait Implementations§

Represents the artifact that will be stored for a Contract
Handle the aggregated set of compiled contracts from the solc crate::CompilerOutput. Read more
Write additional files for the contract
Writes additional files for the contracts if the included in the Contract, such as ir, ewasm, iropt. Read more
Returns the file name for the contract’s artifact Greeter.json Read more
Returns the file name for the contract’s artifact and the given version Greeter.0.8.11.json Read more
Returns the path to the contract’s artifact location based on the contract’s file and name Read more
Returns the path to the contract’s artifact location based on the contract’s file, name and version Read more
The inverse of contract_file_name Read more
Whether the corresponding artifact of the given contract file and name exists
Read the artifact that’s stored at the given path Read more
Read the cached artifacts that are located the paths the iterator yields Read more
Convert a contract to the artifact type Read more
Convert the compiler output into a set of artifacts Read more
This converts a SourceFile that doesn’t contain any contract definitions (interfaces, contracts, libraries) to an artifact. Read more
Returns the appropriate file name for the conflicting file. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more