-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add Sourcify support to TraceIdentifiers #11817
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
267d9d0
Add Sourcify support to TraceIdentifiers
silvekkk 92e7f6d
Merge branch 'master' into helpIssue
grandizzy 34df55b
simply code
silvekkk 2525258
fix typos
silvekkk ed52eb6
add test
silvekkk dcaff0e
fix test and ci
silvekkk b3602fb
Refactor to fix ci
silvekkk 18a1138
Merge branch 'master' into helpIssue
silvekkk d1144b7
using APIv2 & replace test
silvekkk c89fde1
make chain id configurable
silvekkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use super::{IdentifiedAddress, TraceIdentifier}; | ||
use alloy_json_abi::JsonAbi; | ||
use revm_inspectors::tracing::types::CallTraceNode; | ||
use std::borrow::Cow; | ||
|
||
/// A trace identifier that uses Sourcify to identify contract ABIs. | ||
pub struct SourcifyIdentifier; | ||
|
||
impl SourcifyIdentifier { | ||
pub fn new() -> Self { | ||
Self | ||
} | ||
} | ||
|
||
impl Default for SourcifyIdentifier { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl TraceIdentifier for SourcifyIdentifier { | ||
fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>> { | ||
let mut identities = Vec::new(); | ||
let client = reqwest::Client::builder() | ||
.timeout(std::time::Duration::from_secs(5)) | ||
.build() | ||
.expect("Failed to create HTTP client"); | ||
|
||
for &node in nodes { | ||
let address = node.trace.address; | ||
|
||
// Try to get ABI from Sourcify using APIv2 | ||
let abi = foundry_common::block_on(async { | ||
let url = | ||
format!("https://sourcify.dev/server/v2/contract/1/{address}?fields=abi"); | ||
|
||
let response = client.get(&url).send().await.ok()?; | ||
let json: serde_json::Value = response.json().await.ok()?; | ||
let abi_value = json.get("abi")?; | ||
serde_json::from_value::<JsonAbi>(abi_value.clone()).ok() | ||
}); | ||
|
||
if let Some(abi) = abi { | ||
identities.push(IdentifiedAddress { | ||
address, | ||
label: Some("Sourcify".to_string()), | ||
contract: Some("Sourcify".to_string()), | ||
abi: Some(Cow::Owned(abi)), | ||
artifact_id: None, | ||
}); | ||
} | ||
} | ||
|
||
identities | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_sourcify_identifier_creation() { | ||
let _identifier = SourcifyIdentifier::new(); | ||
// Test that creation doesn't panic | ||
} | ||
|
||
#[test] | ||
fn test_sourcify_identifier_default() { | ||
let _identifier = SourcifyIdentifier::new(); // Use new() instead of default() for unit structs | ||
// Test that creation doesn't panic | ||
} | ||
|
||
#[test] | ||
fn test_empty_nodes() { | ||
let mut identifier = SourcifyIdentifier::new(); | ||
let nodes: Vec<&CallTraceNode> = vec![]; | ||
let result = identifier.identify_addresses(&nodes); | ||
assert!(result.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_sourcify_apiv2_response_parsing() { | ||
// Test that we can parse the new APIv2 response format correctly | ||
let response_json = r#"{ | ||
"abi": [ | ||
{"name": "transfer", "type": "function", "inputs": [], "outputs": []} | ||
], | ||
"matchId": "1532018", | ||
"creationMatch": "match", | ||
"runtimeMatch": "match", | ||
"verifiedAt": "2024-08-08T13:20:07Z", | ||
"match": "match", | ||
"chainId": "1", | ||
"address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
}"#; | ||
|
||
let json: serde_json::Value = serde_json::from_str(response_json).unwrap(); | ||
let abi_value = json.get("abi").unwrap(); | ||
let abi: Result<JsonAbi, _> = serde_json::from_value(abi_value.clone()); | ||
|
||
assert!(abi.is_ok()); | ||
let abi = abi.unwrap(); | ||
assert_eq!(abi.len(), 1); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.