foundry_compilers_artifacts_solc/
hh.rs1use 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#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct HardhatArtifact {
17 #[serde(rename = "_format")]
18 pub format: String,
19 pub contract_name: String,
21 pub source_name: String,
23 pub abi: JsonAbi,
25 pub bytecode: Option<BytecodeObject>,
28 pub deployed_bytecode: Option<BytecodeObject>,
31 #[serde(default)]
34 pub link_references: BTreeMap<String, BTreeMap<String, Vec<Offsets>>>,
35 #[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}