Skip to main content

nautilus_lighter/common/
urls.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Base URL resolution for Lighter REST and WebSocket endpoints.
17
18use super::{
19    consts::{LIGHTER_MAINNET_CHAIN_ID, LIGHTER_TESTNET_CHAIN_ID},
20    enums::LighterEnvironment,
21};
22
23const LIGHTER_MAINNET_HTTP_URL: &str = "https://mainnet.zklighter.elliot.ai";
24const LIGHTER_MAINNET_WS_URL: &str = "wss://mainnet.zklighter.elliot.ai/stream";
25
26const LIGHTER_TESTNET_HTTP_URL: &str = "https://testnet.zklighter.elliot.ai";
27const LIGHTER_TESTNET_WS_URL: &str = "wss://testnet.zklighter.elliot.ai/stream";
28
29/// Returns the REST base URL for the given environment.
30#[must_use]
31pub const fn lighter_http_base_url(environment: LighterEnvironment) -> &'static str {
32    match environment {
33        LighterEnvironment::Mainnet => LIGHTER_MAINNET_HTTP_URL,
34        LighterEnvironment::Testnet => LIGHTER_TESTNET_HTTP_URL,
35    }
36}
37
38/// Returns the WebSocket URL for the given environment.
39#[must_use]
40pub const fn lighter_ws_url(environment: LighterEnvironment) -> &'static str {
41    match environment {
42        LighterEnvironment::Mainnet => LIGHTER_MAINNET_WS_URL,
43        LighterEnvironment::Testnet => LIGHTER_TESTNET_WS_URL,
44    }
45}
46
47/// Returns the L2 chain id for the given environment.
48///
49/// The chain id is the first element of every signed transaction's hash
50/// preimage and must match the value the sequencer expects, otherwise
51/// signatures verify against a different message and are rejected.
52#[must_use]
53pub const fn lighter_chain_id(environment: LighterEnvironment) -> u32 {
54    match environment {
55        LighterEnvironment::Mainnet => LIGHTER_MAINNET_CHAIN_ID,
56        LighterEnvironment::Testnet => LIGHTER_TESTNET_CHAIN_ID,
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use rstest::rstest;
63
64    use super::*;
65
66    #[rstest]
67    fn test_http_base_url() {
68        assert_eq!(
69            lighter_http_base_url(LighterEnvironment::Mainnet),
70            LIGHTER_MAINNET_HTTP_URL,
71        );
72        assert_eq!(
73            lighter_http_base_url(LighterEnvironment::Testnet),
74            LIGHTER_TESTNET_HTTP_URL,
75        );
76    }
77
78    #[rstest]
79    fn test_ws_url() {
80        assert_eq!(
81            lighter_ws_url(LighterEnvironment::Mainnet),
82            LIGHTER_MAINNET_WS_URL,
83        );
84        assert_eq!(
85            lighter_ws_url(LighterEnvironment::Testnet),
86            LIGHTER_TESTNET_WS_URL,
87        );
88    }
89
90    #[rstest]
91    fn test_chain_id() {
92        assert_eq!(lighter_chain_id(LighterEnvironment::Mainnet), 304);
93        assert_eq!(lighter_chain_id(LighterEnvironment::Testnet), 300);
94    }
95}