ethers_addressbook/
lib.rs1#![doc = include_str!("../README.md")]
2#![deny(unsafe_code, rustdoc::broken_intra_doc_links)]
3#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
4
5pub use ethers_core::types::{Address, Chain};
6
7use once_cell::sync::Lazy;
8use serde::Deserialize;
9use std::collections::HashMap;
10
11const CONTRACTS_JSON: &str = include_str!("./contracts/contracts.json");
12
13static ADDRESSBOOK: Lazy<HashMap<String, Contract>> =
14 Lazy::new(|| serde_json::from_str(CONTRACTS_JSON).unwrap());
15
16#[derive(Clone, Debug, Deserialize)]
18pub struct Contract {
19 addresses: HashMap<Chain, Address>,
20}
21
22impl Contract {
23 pub fn address(&self, chain: Chain) -> Option<Address> {
26 self.addresses.get(&chain).cloned()
27 }
28}
29
30pub fn contract<S: Into<String>>(name: S) -> Option<Contract> {
33 ADDRESSBOOK.get(&name.into()).cloned()
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn test_tokens() {
42 assert!(contract("dai").is_some());
43 assert!(contract("usdc").is_some());
44 assert!(contract("usdt").is_some());
45 assert!(contract("rand").is_none());
46 }
47
48 #[test]
49 fn test_addrs() {
50 assert!(contract("dai").unwrap().address(Chain::Mainnet).is_some());
51 assert!(contract("dai").unwrap().address(Chain::MoonbeamDev).is_none());
52 }
53}