clockwork_utils/
explorer.rs

1const EXPLORER_URL: &str = "https://explorer.solana.com";
2const CK_EXPLORER_URL: &str = "https://explorer.clockwork.xyz";
3
4
5#[derive(Default)]
6pub struct Explorer {
7    cluster: String,
8    custom_rpc: Option<String>,
9}
10
11impl From<String> for Explorer {
12    fn from(json_rpc_url: String) -> Self {
13        match &json_rpc_url.to_lowercase() {
14            url if url.contains("devnet") => Explorer::devnet(),
15            url if url.contains("testnet") => Explorer::testnet(),
16            url if url.contains("mainnet") => Explorer::mainnet(),
17            _ => {
18                Explorer::custom(json_rpc_url)
19            }
20        }
21    }
22}
23
24impl Explorer {
25    pub fn mainnet() -> Self {
26        Self {
27            cluster: "mainnet-beta".into(),
28            ..Default::default()
29        }
30    }
31
32    pub fn testnet() -> Self {
33        Self {
34            cluster: "testnet".into(),
35            ..Default::default()
36        }
37    }
38
39    pub fn devnet() -> Self {
40        Self {
41            cluster: "devnet".into(),
42            ..Default::default()
43        }
44    }
45
46    pub fn custom(custom_rpc: String) -> Self {
47        Self {
48            cluster: "custom".into(),
49            custom_rpc: Some(custom_rpc),
50        }
51    }
52
53    /// Ex: https://explorer.solana.com/tx/{tx}
54    ///     ?cluster=custom
55    ///     &customUrl=http://localhost:8899
56    pub fn tx_url<T: std::fmt::Display>(&self, tx: T) -> String {
57        let url = format!("{}/tx/{}?cluster={}", EXPLORER_URL, tx, self.cluster);
58        if self.cluster == "custom" {
59            url + "&customUrl=" + self.custom_rpc.as_ref().unwrap()
60        } else {
61            url
62        }
63    }
64
65    /// Ex: https://explorer.clockwork.xyz/thread/{thread}
66    ///     ?network=custom
67    ///     &customRPC=http://localhost:8899
68    pub fn thread_url<T: std::fmt::Display, U: std::fmt::Display>(&self, thread: T, program_id: U) -> String {
69        let url = format!("{}/address/{}?programID={}&network={}", CK_EXPLORER_URL,
70                          thread, program_id, self
71            .cluster);
72        if self.cluster == "custom" {
73            url + "&customRPC=" + self.custom_rpc.as_ref().unwrap()
74        } else {
75            url
76        }
77    }
78}