|
| 1 | +//! commonly used contract types and functions |
| 2 | +
|
| 3 | +use ethers_core::{ |
| 4 | + abi::{Abi, Event, Function}, |
| 5 | + types::{Address, H256}, |
| 6 | +}; |
| 7 | +use ethers_solc::ArtifactId; |
| 8 | +use std::{ |
| 9 | + collections::BTreeMap, |
| 10 | + ops::{Deref, DerefMut}, |
| 11 | +}; |
| 12 | + |
| 13 | +type ArtifactWithContractRef<'a> = (&'a ArtifactId, &'a (Abi, Vec<u8>)); |
| 14 | + |
| 15 | +/// Wrapper type that maps an artifact to a contract ABI and bytecode. |
| 16 | +#[derive(Default)] |
| 17 | +pub struct ContractsByArtifact(pub BTreeMap<ArtifactId, (Abi, Vec<u8>)>); |
| 18 | + |
| 19 | +impl ContractsByArtifact { |
| 20 | + /// Finds a contract which has a similar bytecode as `code`. |
| 21 | + pub fn find_by_code(&self, code: &[u8]) -> Option<ArtifactWithContractRef> { |
| 22 | + self.iter().find(|(_, (_, known_code))| diff_score(known_code, code) < 0.1) |
| 23 | + } |
| 24 | + /// Finds a contract which has the same contract name or identifier as `id`. If more than one is |
| 25 | + /// found, return error. |
| 26 | + pub fn find_by_name_or_identifier( |
| 27 | + &self, |
| 28 | + id: &str, |
| 29 | + ) -> eyre::Result<Option<ArtifactWithContractRef>> { |
| 30 | + let contracts = self |
| 31 | + .iter() |
| 32 | + .filter(|(artifact, _)| artifact.name == id || artifact.identifier() == id) |
| 33 | + .collect::<Vec<_>>(); |
| 34 | + |
| 35 | + if contracts.len() > 1 { |
| 36 | + eyre::bail!("{id} has more than one implementation."); |
| 37 | + } |
| 38 | + |
| 39 | + Ok(contracts.first().cloned()) |
| 40 | + } |
| 41 | + |
| 42 | + /// Flattens a group of contracts into maps of all events and functions |
| 43 | + pub fn flatten(&self) -> (BTreeMap<[u8; 4], Function>, BTreeMap<H256, Event>, Abi) { |
| 44 | + let flattened_funcs: BTreeMap<[u8; 4], Function> = self |
| 45 | + .iter() |
| 46 | + .flat_map(|(_name, (abi, _code))| { |
| 47 | + abi.functions() |
| 48 | + .map(|func| (func.short_signature(), func.clone())) |
| 49 | + .collect::<BTreeMap<[u8; 4], Function>>() |
| 50 | + }) |
| 51 | + .collect(); |
| 52 | + |
| 53 | + let flattened_events: BTreeMap<H256, Event> = self |
| 54 | + .iter() |
| 55 | + .flat_map(|(_name, (abi, _code))| { |
| 56 | + abi.events() |
| 57 | + .map(|event| (event.signature(), event.clone())) |
| 58 | + .collect::<BTreeMap<H256, Event>>() |
| 59 | + }) |
| 60 | + .collect(); |
| 61 | + |
| 62 | + // We need this for better revert decoding, and want it in abi form |
| 63 | + let mut errors_abi = Abi::default(); |
| 64 | + self.iter().for_each(|(_name, (abi, _code))| { |
| 65 | + abi.errors().for_each(|error| { |
| 66 | + let entry = |
| 67 | + errors_abi.errors.entry(error.name.clone()).or_insert_with(Default::default); |
| 68 | + entry.push(error.clone()); |
| 69 | + }); |
| 70 | + }); |
| 71 | + (flattened_funcs, flattened_events, errors_abi) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Deref for ContractsByArtifact { |
| 76 | + type Target = BTreeMap<ArtifactId, (Abi, Vec<u8>)>; |
| 77 | + |
| 78 | + fn deref(&self) -> &Self::Target { |
| 79 | + &self.0 |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl DerefMut for ContractsByArtifact { |
| 84 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 85 | + &mut self.0 |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Wrapper type that maps an address to a contract identifier and contract ABI. |
| 90 | +pub type ContractsByAddress = BTreeMap<Address, (String, Abi)>; |
| 91 | + |
| 92 | +/// Very simple fuzzy matching of contract bytecode. |
| 93 | +/// |
| 94 | +/// Will fail for small contracts that are essentially all immutable variables. |
| 95 | +pub fn diff_score(a: &[u8], b: &[u8]) -> f64 { |
| 96 | + let cutoff_len = usize::min(a.len(), b.len()); |
| 97 | + if cutoff_len == 0 { |
| 98 | + return 1.0 |
| 99 | + } |
| 100 | + |
| 101 | + let a = &a[..cutoff_len]; |
| 102 | + let b = &b[..cutoff_len]; |
| 103 | + let mut diff_chars = 0; |
| 104 | + for i in 0..cutoff_len { |
| 105 | + if a[i] != b[i] { |
| 106 | + diff_chars += 1; |
| 107 | + } |
| 108 | + } |
| 109 | + diff_chars as f64 / cutoff_len as f64 |
| 110 | +} |
0 commit comments