df_hyperliquid_core/client/
mod.rs1pub mod evm;
6pub mod http;
7#[cfg(feature = "native")]
8pub mod native;
9pub mod network;
10#[cfg(feature = "web")]
11pub mod web;
12
13use crate::account::SignError;
14#[cfg(feature = "sign")]
15use crate::account::Signer;
16use crate::client::http::Api;
17use crate::client::http::INFO;
18#[cfg(feature = "sign")]
19use crate::utils::sign::{Domain, L1Action, UserAction};
20use crate::utils::{Address, NonceClock};
21use std::sync::{Mutex, PoisonError};
22
23pub use http::{
24 Accepted, ApiError, ExchangeResponse, Http, Method, OFFLINE, Request, Response, Spent, Status, Transport,
25 TransportError, exchange, info, info_request, user_request,
26};
27pub use network::{
28 L1_CHAIN_ID, MAINNET_HYPE, MAINNET_USDC, Network, TESTNET_HYPE, TESTNET_USDC, Token, USDC_INDEX, UnknownNetwork,
29};
30
31pub type Clock = fn() -> u64;
34
35#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
38pub enum SendError {
39 #[error(transparent)]
41 Sign(#[from] SignError),
42 #[error(transparent)]
44 Api(#[from] ApiError),
45}
46
47pub struct Client<H, S = ()> {
50 api: Api<H>,
51 signer: S,
52 vault: Option<Address>,
53 clock: Clock,
54 nonces: Mutex<NonceClock>,
55}
56
57impl<H: Http, S> std::fmt::Debug for Client<H, S> {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.debug_struct("Client").field("network", &self.network()).field("vault", &self.vault).finish_non_exhaustive()
61 }
62}
63
64impl<H: Http> Client<H> {
65 pub fn reader(http: H, network: Network, clock: Clock) -> Self {
67 Client::new(http, network, clock, ())
68 }
69}
70
71impl<H: Http, S> Client<H, S> {
72 pub fn new(http: H, network: Network, clock: Clock, signer: S) -> Self {
75 Client {
76 api: Api::new(http, network, clock),
77 signer,
78 vault: None,
79 clock,
80 nonces: Mutex::new(NonceClock::new()),
81 }
82 }
83
84 #[must_use]
87 pub fn acting_for(mut self, vault: Address) -> Self {
88 self.vault = Some(vault);
89 self
90 }
91
92 #[must_use]
94 pub const fn network(&self) -> Network {
95 self.api.network()
96 }
97
98 #[must_use]
100 pub const fn http(&self) -> &H {
101 self.api.http()
102 }
103
104 #[must_use]
106 pub const fn signer(&self) -> &S {
107 &self.signer
108 }
109
110 pub fn spent(&self) -> Spent {
112 self.api.spent()
113 }
114
115 pub async fn warm(&self) {
120 let _ = self.api.post(INFO, info_request("exchangeStatus", &[])).await;
121 }
122
123 pub fn next_nonce(&self) -> u64 {
126 self.nonces.lock().unwrap_or_else(PoisonError::into_inner).next((self.clock)())
127 }
128}
129
130#[cfg(feature = "sign")]
134impl<H: Http, S: Signer> Client<H, S> {
135 pub async fn send(&self, action: &impl L1Action) -> Result<Accepted, SendError> {
137 self.send_at(action, self.next_nonce()).await
138 }
139
140 pub async fn send_at(&self, action: &impl L1Action, nonce: u64) -> Result<Accepted, SendError> {
143 let typed = action.typed(nonce, self.vault, self.network());
144 let signature = self.signer.sign_typed(&Domain::EXCHANGE, &typed).await?;
145 self.check(&typed.digest(&Domain::EXCHANGE), &signature)?;
146 Ok(exchange(&self.api, action.body(nonce, &signature, self.vault)).await?)
147 }
148
149 pub async fn send_user(&self, action: &impl UserAction) -> Result<Accepted, SendError> {
153 let network = self.network();
154 let domain = Domain::user_signed(network);
155 let typed = action.typed(network);
156 let signature = self.signer.sign_typed(&domain, &typed).await?;
157 self.check(&typed.digest(&domain), &signature)?;
158 Ok(exchange(&self.api, action.body(&signature, network)).await?)
159 }
160
161 fn check(&self, digest: &[u8; 32], signature: &crate::account::Signature) -> Result<(), SignError> {
163 crate::account::verify(digest, signature, self.signer.address())
164 }
165}
166
167impl<H: Http, S> Transport for Client<H, S> {
168 fn post(&self, path: &str, body: String) -> impl Future<Output = Result<String, TransportError>> {
169 self.api.post(path, body)
170 }
171}