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)]
38#[non_exhaustive]
39pub enum SendError {
40 #[error(transparent)]
42 Sign(#[from] SignError),
43 #[error(transparent)]
45 Api(#[from] ApiError),
46}
47
48pub struct Client<H, S = ()> {
51 api: Api<H>,
52 signer: S,
53 vault: Option<Address>,
54 clock: Clock,
55 nonces: Mutex<NonceClock>,
56}
57
58impl<H: Http, S> std::fmt::Debug for Client<H, S> {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 f.debug_struct("Client").field("network", &self.network()).field("vault", &self.vault).finish_non_exhaustive()
62 }
63}
64
65impl<H: Http> Client<H> {
66 pub fn reader(http: H, network: Network, clock: Clock) -> Self {
68 Client::new(http, network, clock, ())
69 }
70}
71
72impl<H: Http, S> Client<H, S> {
73 pub fn new(http: H, network: Network, clock: Clock, signer: S) -> Self {
76 Client {
77 api: Api::new(http, network, clock),
78 signer,
79 vault: None,
80 clock,
81 nonces: Mutex::new(NonceClock::new()),
82 }
83 }
84
85 #[must_use]
88 pub fn acting_for(mut self, vault: Address) -> Self {
89 self.vault = Some(vault);
90 self
91 }
92
93 #[must_use]
95 pub const fn network(&self) -> Network {
96 self.api.network()
97 }
98
99 #[must_use]
101 pub const fn http(&self) -> &H {
102 self.api.http()
103 }
104
105 #[must_use]
107 pub const fn signer(&self) -> &S {
108 &self.signer
109 }
110
111 pub fn spent(&self) -> Spent {
113 self.api.spent()
114 }
115
116 pub async fn warm(&self) {
121 let _ = self.api.post(INFO, info_request("exchangeStatus", &[])).await;
122 }
123
124 pub fn next_nonce(&self) -> u64 {
127 self.nonces.lock().unwrap_or_else(PoisonError::into_inner).next((self.clock)())
128 }
129}
130
131#[cfg(feature = "sign")]
135impl<H: Http, S: Signer> Client<H, S> {
136 pub async fn send(&self, action: &impl L1Action) -> Result<Accepted, SendError> {
138 self.send_at(action, self.next_nonce()).await
139 }
140
141 pub async fn send_at(&self, action: &impl L1Action, nonce: u64) -> Result<Accepted, SendError> {
144 let typed = action.typed(nonce, self.vault, self.network());
145 let signature = self.signer.sign_typed(&Domain::EXCHANGE, &typed).await?;
146 self.check(&typed.digest(&Domain::EXCHANGE), &signature)?;
147 Ok(exchange(&self.api, action.body(nonce, &signature, self.vault)).await?)
148 }
149
150 pub async fn send_user(&self, action: &impl UserAction) -> Result<Accepted, SendError> {
154 let network = self.network();
155 let domain = Domain::user_signed(network);
156 let typed = action.typed(network);
157 let signature = self.signer.sign_typed(&domain, &typed).await?;
158 self.check(&typed.digest(&domain), &signature)?;
159 Ok(exchange(&self.api, action.body(&signature, network)).await?)
160 }
161
162 fn check(&self, digest: &[u8; 32], signature: &crate::account::Signature) -> Result<(), SignError> {
164 crate::account::verify(digest, signature, self.signer.address())
165 }
166}
167
168impl<H: Http, S> Transport for Client<H, S> {
169 fn post(&self, path: &str, body: String) -> impl Future<Output = Result<String, TransportError>> {
170 self.api.post(path, body)
171 }
172}