anvil_zksync_common/
address_map.rs

1use std::collections::HashMap;
2
3use once_cell::sync::Lazy;
4use serde::Deserialize;
5use zksync_types::{Address, H160};
6
7#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
8pub enum ContractType {
9    System,
10    Precompile,
11    Popular,
12    Unknown,
13}
14
15#[derive(Debug, Deserialize, Clone)]
16pub struct KnownAddress {
17    pub address: H160,
18    pub name: String,
19    pub contract_type: ContractType,
20}
21
22/// Loads the known contact addresses from the JSON file.
23pub static KNOWN_ADDRESSES: Lazy<HashMap<H160, KnownAddress>> = Lazy::new(|| {
24    let json_value = serde_json::from_slice(include_bytes!("./data/address_map.json")).unwrap();
25    let pairs: Vec<KnownAddress> = serde_json::from_value(json_value).unwrap();
26
27    pairs
28        .into_iter()
29        .map(|entry| (entry.address, entry))
30        .collect()
31});
32
33/// Checks if the given address is a precompile based on `KNOWN_ADDRESSES`.
34pub fn is_precompile(address: &Address) -> bool {
35    if let Some(known) = KNOWN_ADDRESSES.get(address) {
36        matches!(known.contract_type, ContractType::Precompile)
37    } else {
38        false
39    }
40}
41
42/// Checks if the given address is a system contract based on `KNOWN_ADDRESSES`.
43pub fn is_system(address: &Address) -> bool {
44    if let Some(known) = KNOWN_ADDRESSES.get(address) {
45        matches!(known.contract_type, ContractType::System)
46    } else {
47        false
48    }
49}