Skip to main content

df_hyperliquid_core/client/
network.rs

1//! Mainnet and testnet. Everything that differs between them is a method on
2//! [`Network`]; an action signed for one is never valid on the other, so the
3//! network is never assumed.
4
5use std::{fmt, str::FromStr};
6
7/// The chain id an L1 action's domain carries on both networks: a literal,
8/// not a real chain.
9pub const L1_CHAIN_ID: u64 = 1337;
10
11/// Spot USDC's index on both networks.
12pub const USDC_INDEX: u32 = 0;
13
14/// A Hyperliquid network. Parses from `mainnet` or `main`, `testnet` or `test`,
15/// in any case.
16#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
17pub enum Network {
18    /// The production network, `api.hyperliquid.xyz`. The default.
19    #[default]
20    Mainnet,
21    /// The test network, `api.hyperliquid-testnet.xyz`.
22    Testnet,
23}
24
25/// A network name that is neither mainnet nor testnet; holds the text given, trimmed.
26#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
27#[error("`{0}` is not a network; expected mainnet or testnet")]
28pub struct UnknownNetwork(pub String);
29
30impl Network {
31    /// Both networks, mainnet first.
32    pub const ALL: [Network; 2] = [Network::Mainnet, Network::Testnet];
33
34    /// Both endpoints live under it: `/info` and `/exchange`.
35    #[must_use]
36    pub const fn api_url(self) -> &'static str {
37        match self {
38            Network::Mainnet => "https://api.hyperliquid.xyz",
39            Network::Testnet => "https://api.hyperliquid-testnet.xyz",
40        }
41    }
42
43    /// The public websocket.
44    #[must_use]
45    pub const fn ws_url(self) -> &'static str {
46        match self {
47            Network::Mainnet => "wss://api.hyperliquid.xyz/ws",
48            Network::Testnet => "wss://api.hyperliquid-testnet.xyz/ws",
49        }
50    }
51
52    /// HyperEVM's JSON-RPC.
53    #[must_use]
54    pub const fn evm_rpc_url(self) -> &'static str {
55        match self {
56            Network::Mainnet => "https://rpc.hyperliquid.xyz/evm",
57            Network::Testnet => "https://rpc.hyperliquid-testnet.xyz/evm",
58        }
59    }
60
61    /// HyperEVM's chain id.
62    #[must_use]
63    pub const fn evm_chain_id(self) -> u64 {
64        match self {
65            Network::Mainnet => 999,
66            Network::Testnet => 998,
67        }
68    }
69
70    /// The chain every user-signed action is signed under and names as its
71    /// `signatureChainId`: Arbitrum One on mainnet, Arbitrum Sepolia on
72    /// testnet. The venue tells the networks apart by `hyperliquidChain`.
73    #[must_use]
74    pub const fn signature_chain_id(self) -> u64 {
75        match self {
76            Network::Mainnet => 42_161,
77            Network::Testnet => 421_614,
78        }
79    }
80
81    /// The Arbitrum chain the bridge is on, where a deposit goes. Separate
82    /// from [`Network::signature_chain_id`], though the two agree today.
83    #[must_use]
84    pub const fn arbitrum_chain_id(self) -> u64 {
85        match self {
86            Network::Mainnet => 42_161,
87            Network::Testnet => 421_614,
88        }
89    }
90
91    /// [`Network::signature_chain_id`] as an action spells it.
92    #[must_use]
93    pub const fn signature_chain_id_hex(self) -> &'static str {
94        match self {
95            Network::Mainnet => "0xa4b1",
96            Network::Testnet => "0x66eee",
97        }
98    }
99
100    /// What a user-signed action names as its `hyperliquidChain`.
101    #[must_use]
102    pub const fn hyperliquid_chain(self) -> &'static str {
103        match self {
104            Network::Mainnet => "Mainnet",
105            Network::Testnet => "Testnet",
106        }
107    }
108
109    /// The phantom agent's `source` for an L1 action.
110    #[must_use]
111    pub const fn agent_source(self) -> &'static str {
112        match self {
113            Network::Mainnet => "a",
114            Network::Testnet => "b",
115        }
116    }
117
118    /// Spot USDC, whose token id differs between the networks.
119    #[must_use]
120    pub const fn usdc(self) -> Token {
121        match self {
122            Network::Mainnet => MAINNET_USDC,
123            Network::Testnet => TESTNET_USDC,
124        }
125    }
126
127    /// Spot HYPE, whose index and token id differ between the networks.
128    #[must_use]
129    pub const fn hype(self) -> Token {
130        match self {
131            Network::Mainnet => MAINNET_HYPE,
132            Network::Testnet => TESTNET_HYPE,
133        }
134    }
135
136    /// `mainnet` or `testnet`, as [`fmt::Display`] writes it.
137    #[must_use]
138    pub const fn name(self) -> &'static str {
139        match self {
140            Network::Mainnet => "mainnet",
141            Network::Testnet => "testnet",
142        }
143    }
144
145    /// The venue's own web app, for a link a person follows.
146    #[must_use]
147    pub const fn app_url(self) -> &'static str {
148        match self {
149            Network::Mainnet => "https://app.hyperliquid.xyz",
150            Network::Testnet => "https://app.hyperliquid-testnet.xyz",
151        }
152    }
153
154    /// Whether this is [`Network::Mainnet`].
155    #[must_use]
156    pub const fn is_mainnet(self) -> bool {
157        matches!(self, Network::Mainnet)
158    }
159}
160
161impl fmt::Display for Network {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        f.write_str(self.name())
164    }
165}
166
167impl FromStr for Network {
168    type Err = UnknownNetwork;
169
170    fn from_str(text: &str) -> Result<Self, Self::Err> {
171        match text.trim().to_ascii_lowercase().as_str() {
172            "mainnet" | "main" => Ok(Network::Mainnet),
173            "testnet" | "test" => Ok(Network::Testnet),
174            _ => Err(UnknownNetwork(text.trim().to_owned())),
175        }
176    }
177}
178
179/// A spot token as the venue names it in each place it appears.
180#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
181pub struct Token {
182    /// The token's name in `spotMeta`, such as `USDC`.
183    pub name: &'static str,
184    /// `token` in `spotClearinghouseState`, and the index in `spotMeta`.
185    pub index: u32,
186    /// `tokenId` in `spotMeta`: 16 bytes as `0x`-prefixed hex.
187    pub token_id: &'static str,
188    /// `NAME:0x<tokenId>`, how a transfer names the token.
189    pub wire: &'static str,
190    /// The decimals the venue counts the token in.
191    pub wei_decimals: u32,
192}
193
194/// Spot USDC on mainnet, as `spotMeta` lists it.
195pub const MAINNET_USDC: Token = Token {
196    name: "USDC",
197    index: USDC_INDEX,
198    token_id: "0x6d1e7cde53ba9467b783cb7c530ce054",
199    wire: "USDC:0x6d1e7cde53ba9467b783cb7c530ce054",
200    wei_decimals: 8,
201};
202
203/// Spot USDC on testnet, as its `spotMeta` lists it.
204pub const TESTNET_USDC: Token = Token {
205    name: "USDC",
206    index: USDC_INDEX,
207    token_id: "0xeb62eee3685fc4c43992febcd9e75443",
208    wire: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
209    wei_decimals: 8,
210};
211
212/// HYPE on mainnet, as `spotMeta` lists it.
213pub const MAINNET_HYPE: Token = Token {
214    name: "HYPE",
215    index: 150,
216    token_id: "0x0d01dc56dcaaca66ad901c959b4011ec",
217    wire: "HYPE:0x0d01dc56dcaaca66ad901c959b4011ec",
218    wei_decimals: 8,
219};
220
221/// HYPE on testnet, as its `spotMeta` lists it. Testnet's token 150 is a
222/// different token.
223pub const TESTNET_HYPE: Token = Token {
224    name: "HYPE",
225    index: 1105,
226    token_id: "0x7317beb7cceed72ef0b346074cc8e7ab",
227    wire: "HYPE:0x7317beb7cceed72ef0b346074cc8e7ab",
228    wei_decimals: 8,
229};
230
231#[cfg(test)]
232#[path = "tests/network.rs"]
233mod tests;