lit_node_core/constants/
chain.rs

1use std::{fmt, str::FromStr};
2
3pub const CHAIN_ETHEREUM: &str = "ethereum";
4pub const CHAIN_SOLANA: &str = "solana";
5pub const CHAIN_COSMOS: &str = "cosmos";
6pub const CHAIN_KYVE: &str = "kyve";
7pub const CHAIN_CHEQD: &str = "cheqd";
8pub const CHAIN_CHEQD_MAINNET: &str = "cheqdMainnet";
9pub const CHAIN_CHEQD_TESTNET: &str = "cheqdTestnet";
10pub const CHAIN_JUNO: &str = "juno";
11pub const CHAIN_EVMOS: &str = "evmos";
12pub const CHAIN_EVMOS_COSMOS: &str = "evmosCosmos";
13pub const CHAIN_EVMOS_COSMOS_TESTNET: &str = "evmosCosmosTestnet";
14pub const CHAIN_LOCALCHAIN: &str = "localchain";
15
16#[allow(dead_code)]
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum Chain {
19    Ethereum,
20    Solana,
21    Cosmos,
22    Kyve,
23    Cheqd,
24    CheqdMainnet,
25    CheqdTestnet,
26    Juno,
27    Evmos,
28    Localchain,
29    EvmosCosmos,
30    EvmosCosmosTestnet,
31}
32
33impl fmt::Display for Chain {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match self {
36            Chain::Ethereum => write!(f, "{}", CHAIN_ETHEREUM),
37            Chain::Solana => write!(f, "{}", CHAIN_SOLANA),
38            Chain::Cosmos => write!(f, "{}", CHAIN_COSMOS),
39            Chain::Kyve => write!(f, "{}", CHAIN_KYVE),
40            Chain::Cheqd => write!(f, "{}", CHAIN_CHEQD),
41            Chain::CheqdMainnet => write!(f, "{}", CHAIN_CHEQD_MAINNET),
42            Chain::CheqdTestnet => write!(f, "{}", CHAIN_CHEQD_TESTNET),
43            Chain::Juno => write!(f, "{}", CHAIN_JUNO),
44            Chain::Evmos => write!(f, "{}", CHAIN_EVMOS),
45            Chain::Localchain => write!(f, "{}", CHAIN_LOCALCHAIN),
46            Chain::EvmosCosmos => write!(f, "{}", CHAIN_EVMOS_COSMOS),
47            Chain::EvmosCosmosTestnet => write!(f, "{}", CHAIN_EVMOS_COSMOS_TESTNET),
48        }
49    }
50}
51
52#[derive(Debug, PartialEq, Eq)]
53pub struct ParseChainError;
54
55impl FromStr for Chain {
56    type Err = ParseChainError;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        match s {
60            CHAIN_ETHEREUM => Ok(Chain::Ethereum),
61            CHAIN_SOLANA => Ok(Chain::Solana),
62            CHAIN_COSMOS => Ok(Chain::Cosmos),
63            CHAIN_KYVE => Ok(Chain::Kyve),
64            CHAIN_CHEQD => Ok(Chain::Cheqd),
65            CHAIN_CHEQD_MAINNET => Ok(Chain::CheqdMainnet),
66            CHAIN_CHEQD_TESTNET => Ok(Chain::CheqdTestnet),
67            CHAIN_JUNO => Ok(Chain::Juno),
68            CHAIN_EVMOS => Ok(Chain::Evmos),
69            CHAIN_LOCALCHAIN => Ok(Chain::Localchain),
70            CHAIN_EVMOS_COSMOS => Ok(Chain::EvmosCosmos),
71            CHAIN_EVMOS_COSMOS_TESTNET => Ok(Chain::EvmosCosmosTestnet),
72            _ => Ok(Chain::Ethereum), // until the below todo is done, assume EVM chain
73                                      // TODO: check rpc_config.yaml and return error if chain is not supported
74                                      // _ => Err(ParseChainError),
75        }
76    }
77}
78
79// since we are always adding new EVM chains, this test actually
80// looks for matches against the non EVM chains.
81// generally we anticipate that the number of non EVM chains will
82// grow much, much slower than EVM chains so this is the easiest solution
83// compared to trying to enumerate all EVM chains.
84pub fn is_evm_compatible_chain(chain: &Chain) -> bool {
85    !matches!(
86        chain,
87        Chain::Cosmos
88            | Chain::Kyve
89            | Chain::Cheqd
90            | Chain::CheqdMainnet
91            | Chain::CheqdTestnet
92            | Chain::Juno
93            | Chain::Solana
94            | Chain::EvmosCosmos
95            | Chain::EvmosCosmosTestnet
96    )
97}