Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix(solc): proper metadata deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Jun 9, 2022
1 parent 8accb79 commit 3eb8a53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
9 changes: 7 additions & 2 deletions ethers-solc/src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::artifacts::{
bytecode::{
Bytecode, BytecodeObject, CompactBytecode, CompactDeployedBytecode, DeployedBytecode,
},
DevDoc, Evm, Ewasm, LosslessAbi, LosslessMetadata, Offsets, StorageLayout, UserDoc,
serde_helpers, DevDoc, Evm, Ewasm, LosslessAbi, LosslessMetadata, Offsets, StorageLayout,
UserDoc,
};
use ethers_core::{abi::Contract as Abi, types::Bytes};
use serde::{Deserialize, Serialize};
Expand All @@ -17,7 +18,11 @@ pub struct Contract {
/// The Ethereum Contract Metadata.
/// See <https://docs.soliditylang.org/en/develop/metadata.html>
pub abi: Option<LosslessAbi>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "serde_helpers::json_string_opt"
)]
pub metadata: Option<LosslessMetadata>,
#[serde(default)]
pub userdoc: UserDoc,
Expand Down
22 changes: 19 additions & 3 deletions ethers-solc/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,25 @@ impl<'de> Deserialize<'de> for LosslessMetadata {
where
D: Deserializer<'de>,
{
let raw_metadata = String::deserialize(deserializer)?;
let metadata = serde_json::from_str(&raw_metadata).map_err(serde::de::Error::custom)?;
Ok(Self { raw_metadata, metadata })
struct LosslessMetadataVisitor;

impl<'de> Visitor<'de> for LosslessMetadataVisitor {
type Value = LosslessMetadata;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "metadata string")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let raw_metadata = value.to_string();
let metadata = serde_json::from_str(value).map_err(serde::de::Error::custom)?;
Ok(LosslessMetadata { raw_metadata, metadata })
}
}
deserializer.deserialize_str(LosslessMetadataVisitor)
}
}

Expand Down
4 changes: 2 additions & 2 deletions ethers-solc/src/artifacts/serde_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub mod json_string_opt {
if s.is_empty() {
return Ok(None)
}

serde_json::from_str(&s).map_err(de::Error::custom).map(Some)
let value = serde_json::Value::String(s);
serde_json::from_value(value).map_err(de::Error::custom).map(Some)
} else {
Ok(None)
}
Expand Down

0 comments on commit 3eb8a53

Please sign in to comment.