cma_rust_parser/
helpers.rs

1use hex;
2use once_cell::sync::Lazy;
3
4pub fn hex_to_string(hex: &str) -> Result<String, Box<dyn std::error::Error>> {
5    let hexstr = hex.strip_prefix("0x").unwrap_or(hex);
6    let bytes = hex::decode(hexstr).map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
7    let s = String::from_utf8(bytes).map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
8    Ok(s)
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Portals {
13    ERC1155BatchPortal,
14    ERC1155SinglePortal,
15    ERC20Portal,
16    ERC721Portal,
17    EtherPortal,
18    None,
19}
20
21pub struct CartesiAddresses {
22    pub erc1155_batch_portal: String,
23    pub erc1155_single_portal: String,
24    pub erc20_portal: String,
25    pub erc721_portal: String,
26    pub ether_portal: String,
27}
28
29pub static CARTESI_ADDRESSES: Lazy<CartesiAddresses> = Lazy::new(|| CartesiAddresses {
30    erc1155_batch_portal: "0xc700A2e5531E720a2434433b6ccf4c0eA2400051".to_string(),
31    erc1155_single_portal: "0xc700A261279aFC6F755A3a67D86ae43E2eBD0051".to_string(),
32    erc20_portal: "0xc700D6aDd016eECd59d989C028214Eaa0fCC0051".to_string(),
33    erc721_portal: "0xc700d52F5290e978e9CAe7D1E092935263b60051".to_string(),
34    ether_portal: "0xc70076a466789B595b50959cdc261227F0D70051".to_string(),
35});
36
37pub trait PortalMatcher {
38    fn match_portal(&self, addr: &str) -> Portals;
39    fn get_portal_address(&self, portal: Portals) -> Option<&str>;
40}
41
42impl PortalMatcher for CartesiAddresses {
43    fn match_portal(&self, addr: &str) -> Portals {
44        let caller_address = addr.trim().to_lowercase();
45
46        if caller_address == self.erc1155_batch_portal.to_lowercase() {
47            Portals::ERC1155BatchPortal
48        } else if caller_address == self.erc1155_single_portal.to_lowercase() {
49            Portals::ERC1155SinglePortal
50        } else if caller_address == self.erc20_portal.to_lowercase() {
51            Portals::ERC20Portal
52        } else if caller_address == self.erc721_portal.to_lowercase() {
53            Portals::ERC721Portal
54        } else if caller_address == self.ether_portal.to_lowercase() {
55            Portals::EtherPortal
56        } else {
57            Portals::None
58        }
59    }
60
61    fn get_portal_address(&self, portal: Portals) -> Option<&str> {
62        match portal {
63            Portals::ERC1155BatchPortal => Some(&self.erc1155_batch_portal),
64            Portals::ERC1155SinglePortal => Some(&self.erc1155_single_portal),
65            Portals::ERC20Portal => Some(&self.erc20_portal),
66            Portals::ERC721Portal => Some(&self.erc721_portal),
67            Portals::EtherPortal => Some(&self.ether_portal),
68            Portals::None => None,
69        }
70    }
71}