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

Commit

Permalink
feat(solc): Add standard-json-input (#1126)
Browse files Browse the repository at this point in the history
* Add standard-json-input

* remove debugging

* Update the error message

* Remove unnecessary changes

* Refactor

* make clippy happy

* Return CompilerInput instead of String
  • Loading branch information
pyk authored Apr 9, 2022
1 parent bf4aa42 commit 247f08f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ethers-etherscan/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl AsRef<str> for CodeFormat {

impl Default for CodeFormat {
fn default() -> Self {
CodeFormat::SingleFile
CodeFormat::StandardJsonInput
}
}

Expand Down
29 changes: 28 additions & 1 deletion ethers-solc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod project_util;
/// Represents a project workspace and handles `solc` compiling of all contracts in that workspace.
#[derive(Debug)]
pub struct Project<T: ArtifactOutput = ConfigurableArtifacts> {
/// The layout of the
/// The layout of the project
pub paths: ProjectPathsConfig,
/// Where to find solc
pub solc: Solc,
Expand Down Expand Up @@ -426,6 +426,33 @@ impl<T: ArtifactOutput> Project<T> {
pub fn flatten(&self, target: &Path) -> Result<String> {
self.paths.flatten(target)
}

/// Returns standard-json-input to compile the target contract
pub fn standard_json_input(&self, target: &Path) -> Result<CompilerInput> {
tracing::trace!("Building standard-json-input");
let graph = Graph::resolve(&self.paths)?;
let target_index = graph.files().get(target).ok_or_else(|| {
SolcError::msg(format!("cannot resolve file at \"{:?}\"", target.display()))
})?;
let mut sources = Vec::new();
let (path, source) = graph.node(*target_index).unpack();
sources.push((path, source));
sources.extend(
graph.all_imported_nodes(*target_index).map(|index| graph.node(index).unpack()),
);

let compiler_inputs = CompilerInput::with_sources(
sources.into_iter().map(|(s, p)| (s.clone(), p.clone())).collect(),
);
let compiler_input = compiler_inputs
.first()
.ok_or_else(|| SolcError::msg("cannot get the compiler input"))?
.clone()
.settings(self.solc_config.settings.clone())
.with_remappings(self.paths.remappings.clone());

Ok(compiler_input)
}
}

pub struct ProjectBuilder<T: ArtifactOutput = ConfigurableArtifacts> {
Expand Down
9 changes: 9 additions & 0 deletions ethers-solc/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ impl Graph {
self.edges.imported_nodes(from)
}

/// Returns an iterator that yields all imports of a node and all their imports
pub fn all_imported_nodes(&self, from: usize) -> impl Iterator<Item = usize> + '_ {
self.edges.all_imported_nodes(from)
}

/// Returns `true` if the given node has any outgoing edges.
pub(crate) fn has_outgoing_edges(&self, index: usize) -> bool {
!self.edges.edges[index].is_empty()
Expand Down Expand Up @@ -782,6 +787,10 @@ impl Node {
pub fn license(&self) -> &Option<SolDataUnit<String>> {
&self.data.license
}

pub fn unpack(&self) -> (&PathBuf, &Source) {
(&self.path, &self.source)
}
}

/// Helper type for formatting a node
Expand Down

0 comments on commit 247f08f

Please sign in to comment.