erpc_sdk/hyperliquid.rs
1use std::sync::Arc;
2
3use serde::Serialize;
4use serde_json::Value;
5use url::Url;
6
7use crate::{
8 PendingRpcRequest, Result, RpcNamespace,
9 rpc::{BatchPolicy, HttpJsonRpcTransport},
10};
11
12/// Hyperliquid `HyperEVM` mainnet read-only JSON-RPC methods represented by this release.
13pub const HYPERLIQUID_RPC_METHODS: &[&str] = &["eth_call", "eth_chainId", "eth_getBalance"];
14
15/// Hyperliquid `HyperEVM` mainnet read-only JSON-RPC service client.
16///
17/// This client is `HyperEVM`-only (chain ID `999` / `0x3e7`) and treats HYPE as
18/// the native gas balance.
19#[derive(Clone)]
20pub struct HyperliquidClient {
21 /// Hyperliquid `HyperEVM` mainnet read-only JSON-RPC namespace.
22 pub rpc: HyperliquidRpcClient,
23}
24
25impl HyperliquidClient {
26 pub(crate) const fn new(transport: Arc<HttpJsonRpcTransport>) -> Self {
27 Self {
28 rpc: HyperliquidRpcClient::new(transport),
29 }
30 }
31}
32
33/// Hyperliquid `HyperEVM` mainnet read-only JSON-RPC namespace.
34///
35/// It targets `HyperEVM` mainnet chain ID `999` (`0x3e7`); HYPE is represented
36/// only as the native balance in this read-only surface.
37///
38/// The deliberately narrow surface has compile-fail coverage for forbidden
39/// escape hatches and state-changing operations:
40///
41/// ```compile_fail
42/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
43/// # async fn forbidden() -> erpc_sdk::Result<()> {
44/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.hyperliquid.rpc;
45/// let _ = rpc.raw::<String>("eth_chainId", None);
46/// # Ok(())
47/// # }
48/// ```
49///
50/// ```compile_fail
51/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
52/// # async fn forbidden() -> erpc_sdk::Result<()> {
53/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.hyperliquid.rpc;
54/// let _ = rpc.request::<String>("eth_chainId", Vec::<serde_json::Value>::new());
55/// # Ok(())
56/// # }
57/// ```
58///
59/// ```compile_fail
60/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
61/// # async fn forbidden() -> erpc_sdk::Result<()> {
62/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.hyperliquid.rpc;
63/// let _ = rpc.batch(Vec::new());
64/// # Ok(())
65/// # }
66/// ```
67///
68/// ```compile_fail
69/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
70/// # async fn forbidden() -> erpc_sdk::Result<()> {
71/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.hyperliquid.rpc;
72/// let _ = rpc.subscribe("newHeads", Vec::new());
73/// # Ok(())
74/// # }
75/// ```
76///
77/// ```compile_fail
78/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
79/// # async fn forbidden() -> erpc_sdk::Result<()> {
80/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.hyperliquid.rpc;
81/// let _ = rpc.eth_send_raw_transaction(["0xdeadbeef"]);
82/// # Ok(())
83/// # }
84/// ```
85#[derive(Clone)]
86pub struct HyperliquidRpcClient {
87 namespace: RpcNamespace,
88}
89
90impl HyperliquidRpcClient {
91 pub(crate) const fn new(transport: Arc<HttpJsonRpcTransport>) -> Self {
92 Self {
93 namespace: RpcNamespace::new(transport, BatchPolicy::Unsupported),
94 }
95 }
96
97 /// Returns the credential-free Hyperliquid `HyperEVM` mainnet read-only JSON-RPC origin and root.
98 #[must_use]
99 pub fn endpoint(&self) -> String {
100 let endpoint = self.namespace.endpoint();
101 if endpoint.is_empty() {
102 return endpoint;
103 }
104 let Ok(mut url) = Url::parse(&endpoint) else {
105 return String::new();
106 };
107 url.set_path("/");
108 url.set_query(None);
109 url.set_fragment(None);
110 let _ = url.set_username("");
111 let _ = url.set_password(None);
112 url.to_string()
113 }
114
115 /// Creates a pending Hyperliquid `HyperEVM` mainnet read-only JSON-RPC `eth_chainId` read.
116 ///
117 /// # Panics
118 ///
119 /// The empty parameter array is infallible to serialize.
120 #[must_use]
121 pub fn eth_chain_id(&self) -> PendingRpcRequest<String> {
122 self.namespace
123 .request("eth_chainId", Vec::<Value>::new())
124 .expect("empty parameters serialize")
125 }
126
127 /// Creates a pending positional Hyperliquid `HyperEVM` mainnet read-only JSON-RPC `eth_getBalance` read.
128 pub fn eth_get_balance<P: Serialize>(&self, params: P) -> Result<PendingRpcRequest<String>> {
129 self.namespace.request("eth_getBalance", params)
130 }
131
132 /// Creates a pending positional Hyperliquid `HyperEVM` mainnet read-only JSON-RPC `eth_call` simulation.
133 pub fn eth_call<P: Serialize>(&self, params: P) -> Result<PendingRpcRequest<String>> {
134 self.namespace.request("eth_call", params)
135 }
136}