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

Commit

Permalink
fix(solc): serialize metadata as raw string
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Jul 12, 2022
1 parent 7566c3d commit 6e44281
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
9 changes: 2 additions & 7 deletions ethers-solc/src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::artifacts::{
bytecode::{
Bytecode, BytecodeObject, CompactBytecode, CompactDeployedBytecode, DeployedBytecode,
},
serde_helpers, DevDoc, Evm, Ewasm, LosslessAbi, LosslessMetadata, Offsets, StorageLayout,
UserDoc,
DevDoc, Evm, Ewasm, LosslessAbi, LosslessMetadata, Offsets, StorageLayout, UserDoc,
};
use ethers_core::{abi::Contract as Abi, types::Bytes};
use serde::{Deserialize, Serialize};
Expand All @@ -18,11 +17,7 @@ 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",
with = "serde_helpers::json_string_opt"
)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<LosslessMetadata>,
#[serde(default)]
pub userdoc: UserDoc,
Expand Down
23 changes: 21 additions & 2 deletions ethers-solc/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ impl Serialize for LosslessMetadata {
where
S: Serializer,
{
self.raw_metadata.serialize(serializer)
serializer.serialize_str(&self.raw_metadata)
}
}

Expand All @@ -920,8 +920,8 @@ impl<'de> Deserialize<'de> for LosslessMetadata {
where
E: serde::de::Error,
{
let raw_metadata = value.to_string();
let metadata = serde_json::from_str(value).map_err(serde::de::Error::custom)?;
let raw_metadata = value.to_string();
Ok(LosslessMetadata { raw_metadata, metadata })
}
}
Expand Down Expand Up @@ -2097,4 +2097,23 @@ mod tests {
])
);
}

#[test]
fn test_lossless_metadata() {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Contract {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<LosslessMetadata>,
}

let s = r#"{"metadata":"{\"compiler\":{\"version\":\"0.4.18+commit.9cf6e910\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}],\"devdoc\":{\"methods\":{\"transferOwnership(address)\":{\"details\":\"Allows the current owner to transfer control of the contract to a newOwner.\",\"params\":{\"newOwner\":\"The address to transfer ownership to.\"}}},\"title\":\"Ownable\"},\"userdoc\":{\"methods\":{}}},\"settings\":{\"compilationTarget\":{\"src/Contract.sol\":\"Ownable\"},\"libraries\":{},\"optimizer\":{\"enabled\":true,\"runs\":1000000},\"remappings\":[\":src/=src/\"]},\"sources\":{\"src/Contract.sol\":{\"keccak256\":\"0x3e0d611f53491f313ae035797ed7ecfd1dfd8db8fef8f82737e6f0cd86d71de7\",\"urls\":[\"bzzr://9c33025fa9d1b8389e4c7c9534a1d70fad91c6c2ad70eb5e4b7dc3a701a5f892\"]}},\"version\":1}"}"#;

let value: serde_json::Value = serde_json::from_str(s).unwrap();
let c: Contract = serde_json::from_value(value).unwrap();
assert_eq!(c.metadata.as_ref().unwrap().raw_metadata, "{\"compiler\":{\"version\":\"0.4.18+commit.9cf6e910\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}],\"devdoc\":{\"methods\":{\"transferOwnership(address)\":{\"details\":\"Allows the current owner to transfer control of the contract to a newOwner.\",\"params\":{\"newOwner\":\"The address to transfer ownership to.\"}}},\"title\":\"Ownable\"},\"userdoc\":{\"methods\":{}}},\"settings\":{\"compilationTarget\":{\"src/Contract.sol\":\"Ownable\"},\"libraries\":{},\"optimizer\":{\"enabled\":true,\"runs\":1000000},\"remappings\":[\":src/=src/\"]},\"sources\":{\"src/Contract.sol\":{\"keccak256\":\"0x3e0d611f53491f313ae035797ed7ecfd1dfd8db8fef8f82737e6f0cd86d71de7\",\"urls\":[\"bzzr://9c33025fa9d1b8389e4c7c9534a1d70fad91c6c2ad70eb5e4b7dc3a701a5f892\"]}},\"version\":1}");

let value = serde_json::to_string(&c).unwrap();
pretty_assertions::assert_eq!(s, value);
}
}
2 changes: 1 addition & 1 deletion ethers-solc/src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ where
CURRENT_STATE
.try_with(|state| {
let scoped = state.scoped.borrow_mut();
f(&*scoped)
f(&scoped)
})
.unwrap_or_else(|_| f(&Report::none()))
}
Expand Down

0 comments on commit 6e44281

Please sign in to comment.