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