foundry_evm_networks/
lib.rs

1//! # foundry-evm-networks
2//!
3//! Foundry EVM network configuration.
4
5use crate::celo::transfer::{
6    CELO_TRANSFER_ADDRESS, CELO_TRANSFER_LABEL, PRECOMPILE_ID_CELO_TRANSFER,
7};
8use alloy_chains::NamedChain;
9use alloy_evm::precompiles::PrecompilesMap;
10use alloy_primitives::{Address, map::AddressHashMap};
11use clap::Parser;
12use serde::{Deserialize, Serialize};
13use std::collections::BTreeMap;
14
15pub mod celo;
16
17#[derive(Clone, Debug, Default, Parser, Copy, Serialize, Deserialize)]
18pub struct NetworkConfigs {
19    /// Enable Optimism network features.
20    #[arg(help_heading = "Networks", long, visible_alias = "optimism")]
21    // Skipped from configs (forge) as there is no feature to be added yet.
22    #[serde(skip)]
23    pub optimism: bool,
24    /// Enable Celo network features.
25    #[arg(help_heading = "Networks", long)]
26    pub celo: bool,
27}
28
29impl NetworkConfigs {
30    pub fn with_optimism() -> Self {
31        Self { optimism: true, ..Default::default() }
32    }
33
34    pub fn with_chain_id(mut self, chain_id: u64) -> Self {
35        if let Ok(NamedChain::Celo | NamedChain::CeloSepolia) = NamedChain::try_from(chain_id) {
36            self.celo = true;
37        }
38        self
39    }
40
41    /// Inject precompiles for configured networks.
42    pub fn inject_precompiles(self, precompiles: &mut PrecompilesMap) {
43        if self.celo {
44            precompiles.apply_precompile(&CELO_TRANSFER_ADDRESS, move |_| {
45                Some(celo::transfer::precompile())
46            });
47        }
48    }
49
50    /// Returns precompiles label for configured networks, to be used in traces.
51    pub fn precompiles_label(self) -> AddressHashMap<String> {
52        let mut labels = AddressHashMap::default();
53        if self.celo {
54            labels.insert(CELO_TRANSFER_ADDRESS, CELO_TRANSFER_LABEL.to_string());
55        }
56        labels
57    }
58
59    /// Returns precompiles for configured networks.
60    pub fn precompiles(self) -> BTreeMap<String, Address> {
61        let mut precompiles = BTreeMap::new();
62        if self.celo {
63            precompiles
64                .insert(PRECOMPILE_ID_CELO_TRANSFER.name().to_string(), CELO_TRANSFER_ADDRESS);
65        }
66        precompiles
67    }
68}