From 1baf88138f60248a5ec59c178d2a74fb1d0596f6 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 22 Dec 2022 19:58:35 +0100 Subject: [PATCH] fix(etherscan): source code serde (#1962) --- ethers-etherscan/src/contract.rs | 10 +++++-- ethers-etherscan/src/utils.rs | 51 ++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/ethers-etherscan/src/contract.rs b/ethers-etherscan/src/contract.rs index ae19abf6..2b5adb23 100644 --- a/ethers-etherscan/src/contract.rs +++ b/ethers-etherscan/src/contract.rs @@ -1,6 +1,6 @@ use crate::{ source_tree::{SourceTree, SourceTreeEntry}, - utils::{deserialize_address_opt, deserialize_stringified_source_code}, + utils::{deserialize_address_opt, deserialize_source_code}, Client, EtherscanError, Response, Result, }; use ethers_core::{ @@ -107,7 +107,7 @@ impl SourceCodeMetadata { #[serde(rename_all = "PascalCase")] pub struct Metadata { /// Includes metadata for compiler settings and language. - #[serde(deserialize_with = "deserialize_stringified_source_code")] + #[serde(deserialize_with = "deserialize_source_code")] pub source_code: SourceCodeMetadata, /// The ABI of the contract. @@ -148,7 +148,11 @@ pub struct Metadata { pub proxy: u64, /// If this contract is a proxy, the address of its implementation. - #[serde(deserialize_with = "deserialize_address_opt")] + #[serde( + default, + skip_serializing_if = "Option::is_none", + deserialize_with = "deserialize_address_opt" + )] pub implementation: Option
, /// The swarm source of the contract. diff --git a/ethers-etherscan/src/utils.rs b/ethers-etherscan/src/utils.rs index b76745eb..3107492a 100644 --- a/ethers-etherscan/src/utils.rs +++ b/ethers-etherscan/src/utils.rs @@ -36,20 +36,35 @@ pub fn deserialize_address_opt<'de, D: Deserializer<'de>>( /// Deserializes as JSON: /// -/// `{ "SourceCode": "{{ .. }}", ..}` +/// Object: `{ "SourceCode": { language: "Solidity", .. }, ..}` /// /// or /// -/// `{ "SourceCode": "..", .. }` -pub fn deserialize_stringified_source_code<'de, D: Deserializer<'de>>( +/// Stringified JSON: `{ "SourceCode": "{{\r\n \"language\": \"Solidity\", ..}}", ..}` +/// +/// or +/// +/// Normal source code: `{ "SourceCode": "// SPDX-License-Identifier: ...", .. }` +pub fn deserialize_source_code<'de, D: Deserializer<'de>>( deserializer: D, ) -> std::result::Result