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

Commit

Permalink
Etherscan could also have a plain source mapping (#2491)
Browse files Browse the repository at this point in the history
* Etherscan could also have a plain source mapping

Reference: 0x68b26dcf21180d2a8de5a303f8cc5b14c8d99c4c

* Add test

* Use (_) instead
  • Loading branch information
wtdcode authored Jul 1, 2023
1 parent 5faea1b commit 3d6af3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ethers-etherscan/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum SourceCodeMetadata {
#[serde(default, skip_serializing_if = "Option::is_none")]
settings: Option<serde_json::Value>,
},
/// Contains just mapped source code.
Sources(HashMap<String, SourceCodeEntry>),
/// Contains only the source code.
SourceCode(String),
}
Expand All @@ -57,21 +59,26 @@ impl SourceCodeMetadata {
match self {
Self::Metadata { sources, .. } => {
sources.values().map(|s| s.content.clone()).collect::<Vec<_>>().join("\n")
}
},
Self::Sources(sources) => {
sources.values().map(|s| s.content.clone()).collect::<Vec<_>>().join("\n")
},
Self::SourceCode(s) => s.clone(),
}
}

pub fn language(&self) -> Option<SourceCodeLanguage> {
match self {
Self::Metadata { language, .. } => language.clone(),
Self::Sources(_)=> None,
Self::SourceCode(_) => None,
}
}

pub fn sources(&self) -> HashMap<String, SourceCodeEntry> {
match self {
Self::Metadata { sources, .. } => sources.clone(),
Self::Sources(sources) => sources.clone(),
Self::SourceCode(s) => HashMap::from([("Contract".into(), s.into())]),
}
}
Expand All @@ -89,6 +96,7 @@ impl SourceCodeMetadata {
}
None => Ok(None),
},
Self::Sources(_) => Ok(None),
Self::SourceCode(_) => Ok(None),
}
}
Expand All @@ -97,6 +105,7 @@ impl SourceCodeMetadata {
pub fn settings(&self) -> Option<&serde_json::Value> {
match self {
Self::Metadata { settings, .. } => settings.as_ref(),
Self::Sources(_) => None,
Self::SourceCode(_) => None,
}
}
Expand Down
16 changes: 16 additions & 0 deletions ethers-etherscan/tests/it/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,19 @@ async fn can_fetch_contract_source_tree_for_multi_entry_contract() {
})
.await
}

/// Query a contract that has a plain source code mapping instead of tagged structures.
#[tokio::test]
#[serial]
async fn can_fetch_contract_source_tree_for_plain_source_code_mapping() {
run_with_client(Chain::Mainnet, |client| async move {
let meta = client
.contract_source_code("0x68b26dcf21180d2a8de5a303f8cc5b14c8d99c4c".parse().unwrap())
.await
.unwrap();

assert_eq!(meta.items.len(), 1);
assert!(matches!(meta.items[0].source_code, SourceCodeMetadata::Sources(_)));
})
.await
}

0 comments on commit 3d6af3f

Please sign in to comment.