Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/artifact_output/configurable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use crate::{
EvmOutputSelection, EwasmOutputSelection,
},
Ast, CompactContractBytecodeCow, DevDoc, Evm, Ewasm, FunctionDebugData, GasEstimates,
GeneratedSource, LosslessAbi, LosslessMetadata, Metadata, Offsets, Settings, StorageLayout,
UserDoc,
GeneratedSource, LosslessMetadata, Metadata, Offsets, Settings, StorageLayout, UserDoc,
},
sources::VersionedSourceFile,
Artifact, ArtifactOutput, SolcConfig, SolcError, SourceFile,
};
use alloy_json_abi::JsonAbi;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::BTreeMap, fs, path::Path};

Expand All @@ -34,7 +34,7 @@ use std::{borrow::Cow, collections::BTreeMap, fs, path::Path};
pub struct ConfigurableContractArtifact {
/// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See <https://docs.soliditylang.org/en/develop/abi-spec.html>
pub abi: Option<LosslessAbi>,
pub abi: Option<JsonAbi>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bytecode: Option<CompactBytecode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -120,7 +120,7 @@ impl From<ConfigurableContractArtifact> for CompactContract {
impl<'a> From<&'a ConfigurableContractArtifact> for CompactContractBytecodeCow<'a> {
fn from(artifact: &'a ConfigurableContractArtifact) -> Self {
CompactContractBytecodeCow {
abi: artifact.abi.as_ref().map(|abi| Cow::Borrowed(&abi.abi)),
abi: artifact.abi.as_ref().map(Cow::Borrowed),
bytecode: artifact.bytecode.as_ref().map(Cow::Borrowed),
deployed_bytecode: artifact.deployed_bytecode.as_ref().map(Cow::Borrowed),
}
Expand Down Expand Up @@ -361,7 +361,7 @@ impl ArtifactOutput for ConfigurableArtifacts {
file: &VersionedSourceFile,
) -> Option<Self::Artifact> {
file.source_file.ast.clone().map(|ast| ConfigurableContractArtifact {
abi: Some(LosslessAbi::default()),
abi: Some(JsonAbi::default()),
id: Some(file.source_file.id),
ast: Some(ast),
bytecode: Some(CompactBytecode::empty()),
Expand Down
2 changes: 1 addition & 1 deletion src/artifact_output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl<T> Artifacts<T> {

/// A trait representation for a [`crate::Contract`] artifact
pub trait Artifact {
/// Returns the artifact's `Abi` and bytecode
/// Returns the artifact's [`JsonAbi`] and bytecode.
fn into_inner(self) -> (Option<JsonAbi>, Option<Bytes>);

/// Turns the artifact into a container type for abi, compact bytecode and deployed bytecode
Expand Down
37 changes: 18 additions & 19 deletions src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::artifacts::{
bytecode::{
Bytecode, BytecodeObject, CompactBytecode, CompactDeployedBytecode, DeployedBytecode,
},
serde_helpers, DevDoc, Evm, Ewasm, LosslessAbi, LosslessMetadata, Offsets, StorageLayout,
UserDoc,
serde_helpers, DevDoc, Evm, Ewasm, LosslessMetadata, Offsets, StorageLayout, UserDoc,
};
use alloy_json_abi::JsonAbi as Abi;
use alloy_json_abi::JsonAbi;
use alloy_primitives::Bytes;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::BTreeMap, convert::TryFrom};
Expand All @@ -18,7 +17,7 @@ use std::{borrow::Cow, collections::BTreeMap, convert::TryFrom};
pub struct Contract {
/// The Ethereum Contract Metadata.
/// See <https://docs.soliditylang.org/en/develop/metadata.html>
pub abi: Option<LosslessAbi>,
pub abi: Option<JsonAbi>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
Expand Down Expand Up @@ -54,7 +53,7 @@ impl<'a> From<&'a Contract> for CompactContractBytecodeCow<'a> {
(None, None)
};
CompactContractBytecodeCow {
abi: artifact.abi.as_ref().map(|abi| Cow::Borrowed(&abi.abi)),
abi: artifact.abi.as_ref().map(Cow::Borrowed),
bytecode,
deployed_bytecode,
}
Expand All @@ -69,7 +68,7 @@ impl<'a> From<&'a Contract> for CompactContractBytecodeCow<'a> {
pub struct ContractBytecode {
/// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See <https://docs.soliditylang.org/en/develop/abi-spec.html>
pub abi: Option<Abi>,
pub abi: Option<JsonAbi>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bytecode: Option<Bytecode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -139,7 +138,7 @@ impl From<Contract> for ContractBytecode {
pub struct CompactContractBytecode {
/// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See <https://docs.soliditylang.org/en/develop/abi-spec.html>
pub abi: Option<Abi>,
pub abi: Option<JsonAbi>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bytecode: Option<CompactBytecode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -214,7 +213,7 @@ impl From<CompactContractBytecode> for ContractBytecode {
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CompactContractBytecodeCow<'a> {
pub abi: Option<Cow<'a, Abi>>,
pub abi: Option<Cow<'a, JsonAbi>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bytecode: Option<Cow<'a, CompactBytecode>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand All @@ -227,7 +226,7 @@ pub struct CompactContractBytecodeCow<'a> {
/// `Bytecode` object.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct ContractBytecodeSome {
pub abi: Abi,
pub abi: JsonAbi,
pub bytecode: Bytecode,
pub deployed_bytecode: DeployedBytecode,
}
Expand All @@ -248,7 +247,7 @@ impl TryFrom<ContractBytecode> for ContractBytecodeSome {
pub struct CompactContractSome {
/// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See <https://docs.soliditylang.org/en/develop/abi-spec.html>
pub abi: Abi,
pub abi: JsonAbi,
pub bin: BytecodeObject,
#[serde(rename = "bin-runtime")]
pub bin_runtime: BytecodeObject,
Expand All @@ -272,7 +271,7 @@ impl TryFrom<CompactContract> for CompactContractSome {
pub struct CompactContract {
/// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See <https://docs.soliditylang.org/en/develop/abi-spec.html>
pub abi: Option<Abi>,
pub abi: Option<JsonAbi>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bin: Option<BytecodeObject>,
#[serde(default, rename = "bin-runtime", skip_serializing_if = "Option::is_none")]
Expand All @@ -281,7 +280,7 @@ pub struct CompactContract {

impl CompactContract {
/// Returns the contents of this type as a single tuple of abi, bytecode and deployed bytecode
pub fn into_parts(self) -> (Option<Abi>, Option<Bytes>, Option<Bytes>) {
pub fn into_parts(self) -> (Option<JsonAbi>, Option<Bytes>, Option<Bytes>) {
(
self.abi,
self.bin.and_then(|bin| bin.into_bytes()),
Expand All @@ -292,7 +291,7 @@ impl CompactContract {
/// Returns the individual parts of this contract.
///
/// If the values are `None`, then `Default` is returned.
pub fn into_parts_or_default(self) -> (Abi, Bytes, Bytes) {
pub fn into_parts_or_default(self) -> (JsonAbi, Bytes, Bytes) {
(
self.abi.unwrap_or_default(),
self.bin.and_then(|bin| bin.into_bytes()).unwrap_or_default(),
Expand Down Expand Up @@ -429,7 +428,7 @@ impl<'a> From<CompactContractRefSome<'a>> for CompactContract {
/// Minimal representation of a contract with a present abi and bytecode that borrows.
#[derive(Copy, Clone, Debug, Serialize)]
pub struct CompactContractRefSome<'a> {
pub abi: &'a Abi,
pub abi: &'a JsonAbi,
pub bin: &'a BytecodeObject,
#[serde(rename = "bin-runtime")]
pub bin_runtime: &'a BytecodeObject,
Expand All @@ -439,7 +438,7 @@ impl<'a> CompactContractRefSome<'a> {
/// Returns the individual parts of this contract.
///
/// If the values are `None`, then `Default` is returned.
pub fn into_parts(self) -> (Abi, Bytes, Bytes) {
pub fn into_parts(self) -> (JsonAbi, Bytes, Bytes) {
CompactContract::from(self).into_parts_or_default()
}
}
Expand All @@ -458,7 +457,7 @@ impl<'a> TryFrom<CompactContractRef<'a>> for CompactContractRefSome<'a> {
/// Helper type to serialize while borrowing from `Contract`
#[derive(Copy, Clone, Debug, Serialize)]
pub struct CompactContractRef<'a> {
pub abi: Option<&'a Abi>,
pub abi: Option<&'a JsonAbi>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bin: Option<&'a BytecodeObject>,
#[serde(default, rename = "bin-runtime", skip_serializing_if = "Option::is_none")]
Expand All @@ -467,14 +466,14 @@ pub struct CompactContractRef<'a> {

impl<'a> CompactContractRef<'a> {
/// Clones the referenced values and returns as tuples
pub fn into_parts(self) -> (Option<Abi>, Option<Bytes>, Option<Bytes>) {
pub fn into_parts(self) -> (Option<JsonAbi>, Option<Bytes>, Option<Bytes>) {
CompactContract::from(self).into_parts()
}

/// Returns the individual parts of this contract.
///
/// If the values are `None`, then `Default` is returned.
pub fn into_parts_or_default(self) -> (Abi, Bytes, Bytes) {
pub fn into_parts_or_default(self) -> (JsonAbi, Bytes, Bytes) {
CompactContract::from(self).into_parts_or_default()
}

Expand Down Expand Up @@ -525,6 +524,6 @@ impl<'a> From<&'a Contract> for CompactContractRef<'a> {
(None, None)
};

Self { abi: c.abi.as_ref().map(|abi| &abi.abi), bin, bin_runtime }
Self { abi: c.abi.as_ref(), bin, bin_runtime }
}
}
46 changes: 2 additions & 44 deletions src/artifacts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Solc artifact types
//! Solc artifact types.

use crate::{
compile::*, error::SolcIoError, remappings::Remapping, utils, ProjectPathsConfig, SolcError,
};
use alloy_json_abi::JsonAbi as Abi;
use md5::Digest;
use semver::Version;
use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -1647,48 +1647,6 @@ impl OutputContracts {
}
}

/// This type keeps a copy of the [`serde_json::Value`] when deserialized from the `solc` json
/// compiler output and uses it to serialize the `abi` without loss.
#[derive(Clone, Debug, PartialEq)]
pub struct LosslessAbi {
/// The complete abi as json value
pub abi_value: serde_json::Value,
/// The deserialised version of `abi_value`
pub abi: Abi,
}

impl Default for LosslessAbi {
fn default() -> Self {
LosslessAbi { abi_value: serde_json::json!([]), abi: Default::default() }
}
}

impl From<LosslessAbi> for Abi {
fn from(abi: LosslessAbi) -> Self {
abi.abi
}
}

impl Serialize for LosslessAbi {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.abi_value.serialize(serializer)
}
}

impl<'de> Deserialize<'de> for LosslessAbi {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let abi_value = serde_json::Value::deserialize(deserializer)?;
let abi = Abi::from_json_str(&abi_value.to_string()).map_err(serde::de::Error::custom)?;
Ok(Self { abi_value, abi })
}
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct UserDoc {
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down
11 changes: 6 additions & 5 deletions src/hh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::{
artifacts::{
bytecode::{Bytecode, BytecodeObject, DeployedBytecode},
contract::{CompactContract, CompactContractBytecode, Contract, ContractBytecode},
CompactContractBytecodeCow, LosslessAbi, Offsets,
CompactContractBytecodeCow, Offsets,
},
ArtifactOutput, SourceFile, VersionedSourceFile,
};
use alloy_json_abi::JsonAbi;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::btree_map::BTreeMap};

Expand All @@ -24,7 +25,7 @@ pub struct HardhatArtifact {
/// The source name of this contract in the workspace like `contracts/Greeter.sol`
pub source_name: String,
/// The contract's ABI
pub abi: LosslessAbi,
pub abi: JsonAbi,
/// A "0x"-prefixed hex string of the unlinked deployment bytecode. If the contract is not
/// deployable, this has the string "0x"
pub bytecode: Option<BytecodeObject>,
Expand All @@ -45,7 +46,7 @@ impl<'a> From<&'a HardhatArtifact> for CompactContractBytecodeCow<'a> {
fn from(artifact: &'a HardhatArtifact) -> Self {
let c: ContractBytecode = artifact.clone().into();
CompactContractBytecodeCow {
abi: Some(Cow::Borrowed(&artifact.abi.abi)),
abi: Some(Cow::Borrowed(&artifact.abi)),
bytecode: c.bytecode.map(|b| Cow::Owned(b.into())),
deployed_bytecode: c.deployed_bytecode.map(|b| Cow::Owned(b.into())),
}
Expand All @@ -55,7 +56,7 @@ impl<'a> From<&'a HardhatArtifact> for CompactContractBytecodeCow<'a> {
impl From<HardhatArtifact> for CompactContract {
fn from(artifact: HardhatArtifact) -> Self {
CompactContract {
abi: Some(artifact.abi.abi),
abi: Some(artifact.abi),
bin: artifact.bytecode,
bin_runtime: artifact.deployed_bytecode,
}
Expand All @@ -76,7 +77,7 @@ impl From<HardhatArtifact> for ContractBytecode {
bcode.into()
});

ContractBytecode { abi: Some(artifact.abi.abi), bytecode, deployed_bytecode }
ContractBytecode { abi: Some(artifact.abi), bytecode, deployed_bytecode }
}
}

Expand Down