chains/
lib.rs

1//! # chains
2//!
3//! Rust bindings for the [Ethereum Chain List](https://github.com/ethereum-lists/chains).
4
5#![cfg_attr(not(feature = "std"), no_std)]
6#![warn(missing_docs, unreachable_pub)]
7#![deny(rustdoc::broken_intra_doc_links)]
8
9extern crate alloc;
10
11use alloc::vec::Vec;
12use once_cell::sync::Lazy;
13
14#[doc(hidden)]
15mod common;
16
17#[cfg(not(feature = "mini"))]
18mod normal;
19#[cfg(not(feature = "mini"))]
20pub use normal::*;
21
22#[cfg(feature = "mini")]
23mod mini;
24#[cfg(feature = "mini")]
25pub use mini::*;
26
27/// A [lazily-initialized][Lazy] [Vector][Vec] which contains all the [EIP-155 chains](https://github.com/ethereum-lists/chains)
28/// deserialized into [Chain] structs.
29pub static CHAINS: Lazy<Vec<Chain>> = Lazy::new(|| {
30    #[cfg(not(feature = "zip"))]
31    {
32        #[cfg(not(feature = "mini"))]
33        const JSON: &str = include_str!("../data/chains.json");
34        #[cfg(feature = "mini")]
35        const JSON: &str = include_str!("../data/chains_mini.json");
36
37        serde_json::from_str(JSON).unwrap()
38    }
39    #[cfg(feature = "zip")]
40    {
41        #[cfg(not(feature = "mini"))]
42        const ZIP: &[u8] = include_bytes!("../data/chains.zip");
43        #[cfg(feature = "mini")]
44        const ZIP: &[u8] = include_bytes!("../data/chains_mini.zip");
45
46        let reader = std::io::Cursor::new(ZIP);
47        let mut archive = zip::ZipArchive::new(reader).unwrap();
48        let file = archive.by_index(0).unwrap();
49        serde_json::from_reader(file).unwrap()
50    }
51});
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn it_works() {
59        let len = CHAINS.len();
60        #[cfg(feature = "std")]
61        eprintln!("{len}");
62        assert!(len >= 600);
63
64        for (_i, chain) in CHAINS.iter().enumerate() {
65            #[cfg(feature = "std")]
66            eprintln!("{_i}: {chain:?}\n");
67            assert!(!chain.name.is_empty());
68            assert!(chain.chain_id > 0);
69            assert!(!chain.short_name.is_empty());
70            // assert!(chain.network_id > 0);
71            assert!(!chain.native_currency.name.is_empty());
72            assert!(!chain.native_currency.symbol.is_empty());
73            assert!(chain.native_currency.decimals > 0);
74            // assert!(!chain.rpc.is_empty());
75            // assert!(!chain.faucets.is_empty());
76            // assert!(!chain.info_url.is_empty());
77        }
78    }
79}