|
1 | 1 | //! Commonly used contract types and functions.
|
2 | 2 |
|
3 | 3 | use alloy_json_abi::{Event, Function, JsonAbi};
|
4 |
| -use alloy_primitives::{hex, Address, B256}; |
| 4 | +use alloy_primitives::{hex, Address, Selector, B256}; |
5 | 5 | use foundry_compilers::{
|
6 | 6 | artifacts::{CompactContractBytecode, ContractBytecodeSome},
|
7 | 7 | ArtifactId, ProjectPathsConfig,
|
@@ -43,36 +43,34 @@ impl ContractsByArtifact {
|
43 | 43 | Ok(contracts.first().cloned())
|
44 | 44 | }
|
45 | 45 |
|
46 |
| - /// Flattens a group of contracts into maps of all events and functions |
47 |
| - pub fn flatten(&self) -> (BTreeMap<[u8; 4], Function>, BTreeMap<B256, Event>, JsonAbi) { |
48 |
| - let flattened_funcs: BTreeMap<[u8; 4], Function> = self |
49 |
| - .iter() |
50 |
| - .flat_map(|(_name, (abi, _code))| { |
51 |
| - abi.functions() |
52 |
| - .map(|func| (func.selector().into(), func.clone())) |
53 |
| - .collect::<BTreeMap<[u8; 4], Function>>() |
54 |
| - }) |
55 |
| - .collect(); |
| 46 | + /// Flattens the contracts into functions, events and errors. |
| 47 | + pub fn flatten(&self) -> (BTreeMap<Selector, Function>, BTreeMap<B256, Event>, JsonAbi) { |
| 48 | + let mut funcs = BTreeMap::new(); |
| 49 | + let mut events = BTreeMap::new(); |
| 50 | + let mut errors_abi = JsonAbi::new(); |
| 51 | + for (_name, (abi, _code)) in self.iter() { |
| 52 | + for func in abi.functions() { |
| 53 | + funcs.insert(func.selector(), func.clone()); |
| 54 | + } |
| 55 | + for event in abi.events() { |
| 56 | + events.insert(event.selector(), event.clone()); |
| 57 | + } |
| 58 | + for error in abi.errors() { |
| 59 | + errors_abi.errors.entry(error.name.clone()).or_default().push(error.clone()); |
| 60 | + } |
| 61 | + } |
| 62 | + (funcs, events, errors_abi) |
| 63 | + } |
56 | 64 |
|
57 |
| - let flattened_events: BTreeMap<B256, Event> = self |
58 |
| - .iter() |
59 |
| - .flat_map(|(_name, (abi, _code))| { |
60 |
| - abi.events() |
61 |
| - .map(|event| (event.selector(), event.clone())) |
62 |
| - .collect::<BTreeMap<B256, Event>>() |
63 |
| - }) |
64 |
| - .collect(); |
65 |
| - |
66 |
| - // We need this for better revert decoding, and want it in abi form |
67 |
| - let mut errors_abi = JsonAbi::default(); |
68 |
| - self.iter().for_each(|(_name, (abi, _code))| { |
69 |
| - abi.errors().for_each(|error| { |
70 |
| - let entry = |
71 |
| - errors_abi.errors.entry(error.name.clone()).or_insert_with(Default::default); |
72 |
| - entry.push(error.clone()); |
73 |
| - }); |
74 |
| - }); |
75 |
| - (flattened_funcs, flattened_events, errors_abi) |
| 65 | + /// Flattens the errors into a single JsonAbi. |
| 66 | + pub fn flatten_errors(&self) -> JsonAbi { |
| 67 | + let mut errors_abi = JsonAbi::new(); |
| 68 | + for (_name, (abi, _code)) in self.iter() { |
| 69 | + for error in abi.errors() { |
| 70 | + errors_abi.errors.entry(error.name.clone()).or_default().push(error.clone()); |
| 71 | + } |
| 72 | + } |
| 73 | + errors_abi |
76 | 74 | }
|
77 | 75 | }
|
78 | 76 |
|
|
0 commit comments