anvil/eth/
util.rs

1use alloy_primitives::Address;
2use foundry_evm::revm::{
3    precompile::{PrecompileSpecId, Precompiles},
4    primitives::SpecId,
5};
6use std::fmt;
7
8pub fn get_precompiles_for(spec_id: SpecId) -> Vec<Address> {
9    Precompiles::new(PrecompileSpecId::from_spec_id(spec_id)).addresses().copied().collect()
10}
11
12/// wrapper type that displays byte as hex
13pub struct HexDisplay<'a>(&'a [u8]);
14
15pub fn hex_fmt_many<I, T>(i: I) -> String
16where
17    I: IntoIterator<Item = T>,
18    T: AsRef<[u8]>,
19{
20    i.into_iter()
21        .map(|item| HexDisplay::from(item.as_ref()).to_string())
22        .collect::<Vec<_>>()
23        .join(", ")
24}
25
26impl<'a> HexDisplay<'a> {
27    pub fn from(b: &'a [u8]) -> Self {
28        HexDisplay(b)
29    }
30}
31
32impl fmt::Display for HexDisplay<'_> {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        if self.0.len() < 1027 {
35            for byte in self.0 {
36                f.write_fmt(format_args!("{byte:02x}"))?;
37            }
38        } else {
39            for byte in &self.0[0..512] {
40                f.write_fmt(format_args!("{byte:02x}"))?;
41            }
42            f.write_str("...")?;
43            for byte in &self.0[self.0.len() - 512..] {
44                f.write_fmt(format_args!("{byte:02x}"))?;
45            }
46        }
47        Ok(())
48    }
49}
50
51impl fmt::Debug for HexDisplay<'_> {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        for byte in self.0 {
54            f.write_fmt(format_args!("{byte:02x}"))?;
55        }
56        Ok(())
57    }
58}