Skip to main content

vertex_sdk/utils/
client_mode.rs

1use crate::utils::deployment::Deployment;
2use include_dir::{include_dir, Dir};
3use std::fs;
4use std::path::Path;
5
6pub static CONFIGS: Dir = include_dir!("$CARGO_MANIFEST_DIR/src/vertex_utils/configs");
7#[derive(Clone, PartialEq)]
8pub enum ClientMode {
9    Prod,
10    BlastProd,
11    MantleProd,
12    SepoliaTest,
13    BlastTest,
14    MantleTest,
15    SeiTest,
16    SeiProd,
17    BaseTest,
18    BaseProd,
19    SonicTest,
20    SonicProd,
21    AbstractTest,
22    AbstractProd,
23    BeraProd,
24    AvaxTest,
25    AvaxProd,
26    XrplTest,
27    KatanaTest,
28    Local,
29    LocalAlt,
30}
31
32impl ClientMode {
33    pub fn default_gateway_url(&self) -> String {
34        let envtag = self.vertex_envtag();
35        match self {
36            Self::Local | Self::LocalAlt => {
37                format!("http://gateway.{envtag}.vertexprotocol.com:80/v1")
38            }
39            _ => {
40                format!("https://gateway.{envtag}.vertexprotocol.com/v1")
41            }
42        }
43    }
44
45    pub fn default_trigger_url(&self) -> String {
46        let envtag = self.vertex_envtag();
47        match self {
48            Self::Local | Self::LocalAlt => {
49                format!("http://trigger.{envtag}.vertexprotocol.com:8080/v1")
50            }
51            _ => {
52                format!("https://trigger.{envtag}.vertexprotocol.com/v1")
53            }
54        }
55    }
56
57    pub fn default_archive_url(&self) -> String {
58        let envtag = self.vertex_envtag();
59        match self {
60            Self::Local | Self::LocalAlt => {
61                format!("http://archive.{envtag}.vertexprotocol.com:8000/v1")
62            }
63            _ => {
64                format!("https://archive.{envtag}.vertexprotocol.com/v1")
65            }
66        }
67    }
68
69    pub fn default_gateway_ws_url(&self) -> String {
70        let envtag = self.vertex_envtag();
71        match self {
72            Self::Local | Self::LocalAlt => {
73                format!("ws://gateway.{envtag}.vertexprotocol.com:80/ws")
74            }
75            _ => {
76                format!("wss://gateway.{envtag}.vertexprotocol.com/ws")
77            }
78        }
79    }
80
81    pub fn private_gateway_ws_url(&self) -> String {
82        let envtag = self.vertex_envtag();
83        match self {
84            Self::Local | Self::LocalAlt => {
85                format!("ws://{envtag}-mm.vertexprotocol-backend.com:80/ws")
86            }
87            _ => {
88                format!("wss://{envtag}-mm.vertexprotocol-backend.com/ws")
89            }
90        }
91    }
92
93    pub fn private_gateway_subscription_url(&self) -> String {
94        let envtag = self.vertex_envtag();
95        match self {
96            Self::Local | Self::LocalAlt => {
97                format!("ws://{envtag}-mm.vertexprotocol-backend.com:80/subscribe")
98            }
99            _ => {
100                format!("wss://{envtag}-mm.vertexprotocol-backend.com/subscribe")
101            }
102        }
103    }
104
105    pub fn private_gateway_url(&self) -> String {
106        let envtag = self.vertex_envtag();
107        match self {
108            Self::Local | Self::LocalAlt => {
109                format!("http://{envtag}-mm.vertexprotocol-backend.com:80")
110            }
111            _ => {
112                format!("https://{envtag}-mm.vertexprotocol-backend.com")
113            }
114        }
115    }
116
117    pub fn vertex_envtag(&self) -> String {
118        match self {
119            Self::Local => "local",
120            Self::LocalAlt => "local-alt",
121            Self::SepoliaTest => "sepolia-test",
122            Self::MantleTest => "mantle-test",
123            Self::BlastTest => "blast-test",
124            Self::SeiTest => "sei-test",
125            Self::BaseTest => "base-test",
126            Self::SonicTest => "sonic-test",
127            Self::AbstractTest => "abstract-test",
128            Self::Prod => "prod",
129            Self::BlastProd => "blast-prod",
130            Self::MantleProd => "mantle-prod",
131            Self::SeiProd => "sei-prod",
132            Self::BaseProd => "base-prod",
133            Self::SonicProd => "sonic-prod",
134            Self::AbstractProd => "abstract-prod",
135            Self::BeraProd => "bera-prod",
136            Self::AvaxTest => "avax-test",
137            Self::AvaxProd => "avax-prod",
138            Self::XrplTest => "xrpl-test",
139            Self::KatanaTest => "katana-test",
140        }
141        .to_string()
142    }
143
144    pub fn from_envtag(envtag: &str) -> Self {
145        match envtag {
146            "local" => Self::Local,
147            "local-alt" => Self::LocalAlt,
148            "sepolia-test" => Self::SepoliaTest,
149            "blast-test" => Self::BlastTest,
150            "sei-test" => Self::SeiTest,
151            "base-test" => Self::BaseTest,
152            "sonic-test" => Self::SonicTest,
153            "abstract-test" => Self::AbstractTest,
154            "avax-test" => Self::AvaxTest,
155            "xrpl-test" => Self::XrplTest,
156            "katana-test" => Self::KatanaTest,
157            "prod" => Self::Prod,
158            "blast-prod" => Self::BlastProd,
159            "mantle-test" => Self::MantleTest,
160            "mantle-prod" => Self::MantleProd,
161            "sei-prod" => Self::SeiProd,
162            "base-prod" => Self::BaseProd,
163            "sonic-prod" => Self::SonicProd,
164            "abstract-prod" => Self::AbstractProd,
165            "bera-prod" => Self::BeraProd,
166            "avax-prod" => Self::AvaxProd,
167            _ => panic!("Unknown envtag: {}", envtag),
168        }
169    }
170
171    pub fn deployment(&self) -> Deployment {
172        let network = self.vertex_envtag();
173        let deployment_path = Path::new(&network).join("deployment.json");
174
175        // Order of loading
176        // 1. try ENVVAR override
177        // 2. try locally configured directory
178        // 3. use deployment.json included in binary
179        let deployment_str = match std::env::var("VERTEX_CONFIGS_DIR") {
180            Ok(path) => {
181                let deployment_path = Path::new(&path).join(&network).join("deployment.json");
182                fs::read_to_string(&deployment_path).expect(&format!(
183                    "Failed to read {}",
184                    &deployment_path.to_string_lossy()
185                ))
186            }
187            Err(_) => {
188                let package_dir = env!("CARGO_MANIFEST_DIR");
189                let default_configs_path = Path::new(package_dir)
190                    .join("src/vertex_utils/configs")
191                    .join(&network)
192                    .join("deployment.json");
193
194                fs::read_to_string(&default_configs_path).unwrap_or_else(|_| {
195                    CONFIGS
196                        .get_file(deployment_path.to_str().unwrap())
197                        .expect("Deployment file not found")
198                        .contents_utf8()
199                        .expect("Failed to read deployment file")
200                        .to_string()
201                })
202            }
203        };
204
205        serde_json::from_str(&deployment_str).unwrap()
206    }
207}