df_hyperliquid_core/client/
network.rs1use std::{fmt, str::FromStr};
6
7pub const L1_CHAIN_ID: u64 = 1337;
10
11pub const USDC_INDEX: u32 = 0;
13
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
17pub enum Network {
18 #[default]
20 Mainnet,
21 Testnet,
23}
24
25#[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 pub const ALL: [Network; 2] = [Network::Mainnet, Network::Testnet];
33
34 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
181pub struct Token {
182 pub name: &'static str,
184 pub index: u32,
186 pub token_id: &'static str,
188 pub wire: &'static str,
190 pub wei_decimals: u32,
192}
193
194pub 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
203pub 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
212pub 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
221pub 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;