anvil_zksync_common/
address_map.rs

1use std::collections::HashMap;
2
3use lazy_static::lazy_static;
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
22lazy_static! {
23    /// Loads the known contact addresses from the JSON file.
24    pub static ref KNOWN_ADDRESSES: HashMap<H160, KnownAddress> = {
25        let json_value = serde_json::from_slice(include_bytes!("./data/address_map.json")).unwrap();
26        let pairs: Vec<KnownAddress> = serde_json::from_value(json_value).unwrap();
27
28        pairs
29            .into_iter()
30            .map(|entry| (entry.address, entry))
31            .collect()
32    };
33}
34
35/// Checks if the given address is a precompile based on `KNOWN_ADDRESSES`.
36pub fn is_precompile(address: &Address) -> bool {
37    if let Some(known) = KNOWN_ADDRESSES.get(address) {
38        matches!(known.contract_type, ContractType::Precompile)
39    } else {
40        false
41    }
42}
43
44/// Checks if the given address is a system contract based on `KNOWN_ADDRESSES`.
45pub fn is_system(address: &Address) -> bool {
46    if let Some(known) = KNOWN_ADDRESSES.get(address) {
47        matches!(known.contract_type, ContractType::System)
48    } else {
49        false
50    }
51}