foundry_compilers/artifact_output/
hh.rs

1use crate::{output::sources::VersionedSourceFile, ArtifactOutput};
2use foundry_compilers_artifacts::{
3    hh::{HardhatArtifact, HH_ARTIFACT_VERSION},
4    Contract, SourceFile,
5};
6use std::path::Path;
7
8/// Hardhat style artifacts handler
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10pub struct HardhatArtifacts {
11    _priv: (),
12}
13
14impl ArtifactOutput for HardhatArtifacts {
15    type Artifact = HardhatArtifact;
16    type CompilerContract = Contract;
17
18    fn contract_to_artifact(
19        &self,
20        file: &Path,
21        name: &str,
22        contract: Contract,
23        _source_file: Option<&SourceFile>,
24    ) -> Self::Artifact {
25        let (bytecode, link_references, deployed_bytecode, deployed_link_references) =
26            if let Some(evm) = contract.evm {
27                let (deployed_bytecode, deployed_link_references) =
28                    if let Some(code) = evm.deployed_bytecode.and_then(|code| code.bytecode) {
29                        (Some(code.object), code.link_references)
30                    } else {
31                        (None, Default::default())
32                    };
33
34                let (bytecode, link_ref) = if let Some(bc) = evm.bytecode {
35                    (Some(bc.object), bc.link_references)
36                } else {
37                    (None, Default::default())
38                };
39
40                (bytecode, link_ref, deployed_bytecode, deployed_link_references)
41            } else {
42                (Default::default(), Default::default(), None, Default::default())
43            };
44
45        HardhatArtifact {
46            format: HH_ARTIFACT_VERSION.to_string(),
47            contract_name: name.to_string(),
48            source_name: file.to_string_lossy().to_string(),
49            abi: contract.abi.unwrap_or_default(),
50            bytecode,
51            deployed_bytecode,
52            link_references,
53            deployed_link_references,
54        }
55    }
56
57    fn standalone_source_file_to_artifact(
58        &self,
59        _path: &Path,
60        _file: &VersionedSourceFile,
61    ) -> Option<Self::Artifact> {
62        None
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::Artifact;
70
71    #[test]
72    fn can_parse_hh_artifact() {
73        let s = include_str!("../../../../test-data/hh-greeter-artifact.json");
74        let artifact = serde_json::from_str::<HardhatArtifact>(s).unwrap();
75        let compact = artifact.into_compact_contract();
76        assert!(compact.abi.is_some());
77        assert!(compact.bin.is_some());
78        assert!(compact.bin_runtime.is_some());
79    }
80}