foundry_compilers_artifacts_solc/
hh.rs

1//! Hardhat support
2
3use crate::{
4    Bytecode, BytecodeObject, CompactContract, CompactContractBytecode, CompactContractBytecodeCow,
5    ContractBytecode, DeployedBytecode, Offsets,
6};
7use alloy_json_abi::JsonAbi;
8use serde::{Deserialize, Serialize};
9use std::{borrow::Cow, collections::btree_map::BTreeMap};
10
11pub const HH_ARTIFACT_VERSION: &str = "hh-sol-artifact-1";
12
13/// A hardhat artifact
14#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct HardhatArtifact {
17    #[serde(rename = "_format")]
18    pub format: String,
19    /// A string with the contract's name.
20    pub contract_name: String,
21    /// The source name of this contract in the workspace like `contracts/Greeter.sol`
22    pub source_name: String,
23    /// The contract's ABI
24    pub abi: JsonAbi,
25    /// A "0x"-prefixed hex string of the unlinked deployment bytecode. If the contract is not
26    /// deployable, this has the string "0x"
27    pub bytecode: Option<BytecodeObject>,
28    /// A "0x"-prefixed hex string of the unlinked runtime/deployed bytecode. If the contract is
29    /// not deployable, this has the string "0x"
30    pub deployed_bytecode: Option<BytecodeObject>,
31    /// The bytecode's link references object as returned by solc. If the contract doesn't need to
32    /// be linked, this value contains an empty object.
33    #[serde(default)]
34    pub link_references: BTreeMap<String, BTreeMap<String, Vec<Offsets>>>,
35    /// The deployed bytecode's link references object as returned by solc. If the contract doesn't
36    /// need to be linked, this value contains an empty object.
37    #[serde(default)]
38    pub deployed_link_references: BTreeMap<String, BTreeMap<String, Vec<Offsets>>>,
39}
40
41impl<'a> From<&'a HardhatArtifact> for CompactContractBytecodeCow<'a> {
42    fn from(artifact: &'a HardhatArtifact) -> Self {
43        let c: ContractBytecode = artifact.clone().into();
44        CompactContractBytecodeCow {
45            abi: Some(Cow::Borrowed(&artifact.abi)),
46            bytecode: c.bytecode.map(|b| Cow::Owned(b.into())),
47            deployed_bytecode: c.deployed_bytecode.map(|b| Cow::Owned(b.into())),
48        }
49    }
50}
51
52impl From<HardhatArtifact> for CompactContract {
53    fn from(artifact: HardhatArtifact) -> Self {
54        Self {
55            abi: Some(artifact.abi),
56            bin: artifact.bytecode,
57            bin_runtime: artifact.deployed_bytecode,
58        }
59    }
60}
61
62impl From<HardhatArtifact> for ContractBytecode {
63    fn from(artifact: HardhatArtifact) -> Self {
64        let bytecode: Option<Bytecode> = artifact.bytecode.as_ref().map(|t| {
65            let mut bcode: Bytecode = t.clone().into();
66            bcode.link_references = artifact.link_references.clone();
67            bcode
68        });
69
70        let deployed_bytecode: Option<DeployedBytecode> = artifact.bytecode.as_ref().map(|t| {
71            let mut bcode: Bytecode = t.clone().into();
72            bcode.link_references = artifact.deployed_link_references.clone();
73            bcode.into()
74        });
75
76        Self { abi: Some(artifact.abi), bytecode, deployed_bytecode }
77    }
78}
79
80impl From<HardhatArtifact> for CompactContractBytecode {
81    fn from(artifact: HardhatArtifact) -> Self {
82        let c: ContractBytecode = artifact.into();
83
84        c.into()
85    }
86}