foundry_evm_networks/
lib.rs1use crate::celo::transfer::{
6 CELO_TRANSFER_ADDRESS, CELO_TRANSFER_LABEL, PRECOMPILE_ID_CELO_TRANSFER,
7};
8use alloy_chains::{
9 NamedChain,
10 NamedChain::{Chiado, Gnosis, Moonbase, Moonbeam, MoonbeamDev, Moonriver, Rsk, RskTestnet},
11};
12use alloy_evm::precompiles::PrecompilesMap;
13use alloy_primitives::{Address, map::AddressHashMap};
14use clap::Parser;
15use serde::{Deserialize, Serialize};
16use std::collections::BTreeMap;
17
18pub mod celo;
19
20#[derive(Clone, Debug, Default, Parser, Copy, Serialize, Deserialize, PartialEq)]
21pub struct NetworkConfigs {
22 #[arg(help_heading = "Networks", long, visible_alias = "optimism", conflicts_with = "celo")]
24 #[serde(skip)]
26 optimism: bool,
27 #[arg(help_heading = "Networks", long, conflicts_with = "optimism")]
29 #[serde(default)]
30 celo: bool,
31 #[arg(skip)]
33 #[serde(default)]
34 bypass_prevrandao: bool,
35}
36
37impl NetworkConfigs {
38 pub fn with_optimism() -> Self {
39 Self { optimism: true, ..Default::default() }
40 }
41
42 pub fn with_celo() -> Self {
43 Self { celo: true, ..Default::default() }
44 }
45
46 pub fn is_optimism(&self) -> bool {
47 self.optimism
48 }
49
50 pub fn bypass_prevrandao(&self, chain_id: u64) -> bool {
51 if let Ok(
52 Moonbeam | Moonbase | Moonriver | MoonbeamDev | Rsk | RskTestnet | Gnosis | Chiado,
53 ) = NamedChain::try_from(chain_id)
54 {
55 return true;
56 }
57 self.bypass_prevrandao
58 }
59
60 pub fn is_celo(&self) -> bool {
61 self.celo
62 }
63
64 pub fn with_chain_id(mut self, chain_id: u64) -> Self {
65 if let Ok(NamedChain::Celo | NamedChain::CeloSepolia) = NamedChain::try_from(chain_id) {
66 self.celo = true;
67 }
68 self
69 }
70
71 pub fn inject_precompiles(self, precompiles: &mut PrecompilesMap) {
73 if self.celo {
74 precompiles.apply_precompile(&CELO_TRANSFER_ADDRESS, move |_| {
75 Some(celo::transfer::precompile())
76 });
77 }
78 }
79
80 pub fn precompiles_label(self) -> AddressHashMap<String> {
82 let mut labels = AddressHashMap::default();
83 if self.celo {
84 labels.insert(CELO_TRANSFER_ADDRESS, CELO_TRANSFER_LABEL.to_string());
85 }
86 labels
87 }
88
89 pub fn precompiles(self) -> BTreeMap<String, Address> {
91 let mut precompiles = BTreeMap::new();
92 if self.celo {
93 precompiles
94 .insert(PRECOMPILE_ID_CELO_TRANSFER.name().to_string(), CELO_TRANSFER_ADDRESS);
95 }
96 precompiles
97 }
98}