Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Etherscan could also have a plain source mapping #2491

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading