use df_hyperliquid_core::client::{Method, Request};
use df_hyperliquid_core::utils::{Address, Json};
use crate::trade::intent::Signed;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Call {
pub method: Method,
pub path: String,
pub body: Option<String>,
}
impl Call {
fn get(path: impl Into<String>) -> Call {
Call { method: Method::Get, path: path.into(), body: None }
}
#[must_use]
pub fn submit(signed: &Signed) -> Call {
Call { method: Method::Post, path: signed.path(), body: Some(signed.body()) }
}
#[must_use]
pub fn trading_state() -> Call {
Call::get("/state/trading")
}
#[must_use]
pub fn account_stream(wallet: Address) -> Call {
Call::get(format!("/state/user/live?wallet={}", wallet.to_hex()))
}
#[must_use]
pub fn summary() -> Call {
Call::get("/query/protocol/summary")
}
#[must_use]
pub fn market_stats() -> Call {
Call::get("/query/protocol/market-stats")
}
#[must_use]
pub fn queue_summary() -> Call {
Call::get("/query/protocol/queue-summary")
}
#[must_use]
pub fn recent_trades() -> Call {
Call::get("/query/protocol/trades/recent")
}
#[must_use]
pub fn queue_rank(wallet: Address) -> Call {
Call::get(format!("/queue-rank/accounts/{}", wallet.to_hex()))
}
#[must_use]
pub fn deposit_check(wallet: Address) -> Call {
let body = Json::Object(vec![("user".to_owned(), Json::String(wallet.to_hex()))]).compact();
Call { method: Method::Post, path: "/funding/deposits/check".to_owned(), body: Some(body) }
}
#[must_use]
pub fn health() -> Call {
Call::get("/query/health")
}
#[must_use]
pub fn request(&self, base: &str) -> Request {
let mut headers = vec![("Accept".to_owned(), "application/json".to_owned())];
if self.body.is_some() {
headers.push(("Content-Type".to_owned(), "application/json".to_owned()));
}
Request {
method: self.method,
url: format!("{}{}", base.trim_end_matches('/'), self.path),
headers,
body: self.body.clone().map(String::into_bytes).unwrap_or_default(),
}
}
}
#[cfg(test)]
#[path = "tests/call.rs"]
mod tests;