diff --git a/src/artifact_output/configurable.rs b/src/artifact_output/configurable.rs index 70222266..2ba5e2a7 100644 --- a/src/artifact_output/configurable.rs +++ b/src/artifact_output/configurable.rs @@ -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}; @@ -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 - pub abi: Option, + pub abi: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub bytecode: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -120,7 +120,7 @@ impl From 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), } @@ -361,7 +361,7 @@ impl ArtifactOutput for ConfigurableArtifacts { file: &VersionedSourceFile, ) -> Option { 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()), diff --git a/src/artifact_output/mod.rs b/src/artifact_output/mod.rs index 94f44d5c..c6c48162 100644 --- a/src/artifact_output/mod.rs +++ b/src/artifact_output/mod.rs @@ -424,7 +424,7 @@ impl Artifacts { /// 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, Option); /// Turns the artifact into a container type for abi, compact bytecode and deployed bytecode diff --git a/src/artifacts/contract.rs b/src/artifacts/contract.rs index 7461ecbe..ee176415 100644 --- a/src/artifacts/contract.rs +++ b/src/artifacts/contract.rs @@ -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}; @@ -18,7 +17,7 @@ use std::{borrow::Cow, collections::BTreeMap, convert::TryFrom}; pub struct Contract { /// The Ethereum Contract Metadata. /// See - pub abi: Option, + pub abi: Option, #[serde( default, skip_serializing_if = "Option::is_none", @@ -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, } @@ -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 - pub abi: Option, + pub abi: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub bytecode: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -139,7 +138,7 @@ impl From for ContractBytecode { pub struct CompactContractBytecode { /// The Ethereum Contract ABI. If empty, it is represented as an empty /// array. See - pub abi: Option, + pub abi: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub bytecode: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -214,7 +213,7 @@ impl From for ContractBytecode { #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct CompactContractBytecodeCow<'a> { - pub abi: Option>, + pub abi: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] pub bytecode: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -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, } @@ -248,7 +247,7 @@ impl TryFrom for ContractBytecodeSome { pub struct CompactContractSome { /// The Ethereum Contract ABI. If empty, it is represented as an empty /// array. See - pub abi: Abi, + pub abi: JsonAbi, pub bin: BytecodeObject, #[serde(rename = "bin-runtime")] pub bin_runtime: BytecodeObject, @@ -272,7 +271,7 @@ impl TryFrom for CompactContractSome { pub struct CompactContract { /// The Ethereum Contract ABI. If empty, it is represented as an empty /// array. See - pub abi: Option, + pub abi: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub bin: Option, #[serde(default, rename = "bin-runtime", skip_serializing_if = "Option::is_none")] @@ -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, Option, Option) { + pub fn into_parts(self) -> (Option, Option, Option) { ( self.abi, self.bin.and_then(|bin| bin.into_bytes()), @@ -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(), @@ -429,7 +428,7 @@ impl<'a> From> 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, @@ -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() } } @@ -458,7 +457,7 @@ impl<'a> TryFrom> 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")] @@ -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, Option, Option) { + pub fn into_parts(self) -> (Option, Option, Option) { 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() } @@ -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 } } } diff --git a/src/artifacts/mod.rs b/src/artifacts/mod.rs index 0755acce..3243a4d8 100644 --- a/src/artifacts/mod.rs +++ b/src/artifacts/mod.rs @@ -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}; @@ -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 for Abi { - fn from(abi: LosslessAbi) -> Self { - abi.abi - } -} - -impl Serialize for LosslessAbi { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - self.abi_value.serialize(serializer) - } -} - -impl<'de> Deserialize<'de> for LosslessAbi { - fn deserialize(deserializer: D) -> Result - 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")] diff --git a/src/hh.rs b/src/hh.rs index a67fb2f2..dea53546 100644 --- a/src/hh.rs +++ b/src/hh.rs @@ -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}; @@ -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, @@ -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())), } @@ -55,7 +56,7 @@ impl<'a> From<&'a HardhatArtifact> for CompactContractBytecodeCow<'a> { impl From 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, } @@ -76,7 +77,7 @@ impl From for ContractBytecode { bcode.into() }); - ContractBytecode { abi: Some(artifact.abi.abi), bytecode, deployed_bytecode } + ContractBytecode { abi: Some(artifact.abi), bytecode, deployed_bytecode } } }