foundry_evm_traces/identifier/
mod.rs
1use alloy_json_abi::JsonAbi;
2use alloy_primitives::Address;
3use foundry_common::ContractsByArtifact;
4use foundry_compilers::ArtifactId;
5use foundry_config::{Chain, Config};
6use revm_inspectors::tracing::types::CallTraceNode;
7use std::borrow::Cow;
8
9mod local;
10pub use local::LocalTraceIdentifier;
11
12mod etherscan;
13pub use etherscan::EtherscanIdentifier;
14
15mod signatures;
16pub use signatures::{SignaturesCache, SignaturesIdentifier};
17
18pub struct IdentifiedAddress<'a> {
20 pub address: Address,
22 pub label: Option<String>,
24 pub contract: Option<String>,
28 pub abi: Option<Cow<'a, JsonAbi>>,
30 pub artifact_id: Option<ArtifactId>,
32}
33
34pub trait TraceIdentifier {
36 fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>>;
38}
39
40pub struct TraceIdentifiers<'a> {
42 pub local: Option<LocalTraceIdentifier<'a>>,
44 pub etherscan: Option<EtherscanIdentifier>,
46}
47
48impl Default for TraceIdentifiers<'_> {
49 fn default() -> Self {
50 Self::new()
51 }
52}
53
54impl TraceIdentifier for TraceIdentifiers<'_> {
55 fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>> {
56 let mut identities = Vec::with_capacity(nodes.len());
57 if let Some(local) = &mut self.local {
58 identities.extend(local.identify_addresses(nodes));
59 if identities.len() >= nodes.len() {
60 return identities;
61 }
62 }
63 if let Some(etherscan) = &mut self.etherscan {
64 identities.extend(etherscan.identify_addresses(nodes));
65 }
66 identities
67 }
68}
69
70impl<'a> TraceIdentifiers<'a> {
71 pub const fn new() -> Self {
73 Self { local: None, etherscan: None }
74 }
75
76 pub fn with_local(mut self, known_contracts: &'a ContractsByArtifact) -> Self {
78 self.local = Some(LocalTraceIdentifier::new(known_contracts));
79 self
80 }
81
82 pub fn with_etherscan(mut self, config: &Config, chain: Option<Chain>) -> eyre::Result<Self> {
84 self.etherscan = EtherscanIdentifier::new(config, chain)?;
85 Ok(self)
86 }
87
88 pub fn is_empty(&self) -> bool {
90 self.local.is_none() && self.etherscan.is_none()
91 }
92}