use df_hyperliquid_core::client::Http;
use df_hyperliquid_core::client::evm::precompiles;
use df_hyperliquid_core::utils::Address;
use crate::client::{Call, Client, Error, chain};
use crate::markets::{QueueRank, QueueSummary, Summary, Trade, TradingState, stats};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Quote {
pub bid: u128,
pub ask: u128,
pub mid: u128,
}
impl<H: Http + Clone> Client<H> {
async fn text(&self, call: &Call) -> Result<String, Error> {
Ok(self.relayer().send(call).await?)
}
pub async fn trading_state(&self) -> Result<TradingState, Error> {
Ok(TradingState::parse(&self.text(&Call::trading_state()).await?)?)
}
pub async fn summary(&self) -> Result<Summary, Error> {
Ok(Summary::parse(&self.text(&Call::summary()).await?)?)
}
pub async fn volumes(&self) -> Result<Vec<(u32, u128)>, Error> {
Ok(stats::market_volumes(&self.text(&Call::market_stats()).await?)?)
}
pub async fn queue(&self) -> Result<QueueSummary, Error> {
Ok(QueueSummary::parse(&self.text(&Call::queue_summary()).await?)?)
}
pub async fn recent_trades(&self) -> Result<Vec<Trade>, Error> {
Ok(stats::recent_trades(&self.text(&Call::recent_trades()).await?)?)
}
pub async fn queue_rank(&self, owner: Address) -> Result<QueueRank, Error> {
Ok(QueueRank::parse(&self.text(&Call::queue_rank(owner)).await?)?)
}
pub async fn quote(&self, asset_index: u32) -> Result<Quote, Error> {
let bbo = self.rpc().read(&precompiles::bbo(asset_index)).await.map_err(Error::Precompile)?;
let (bid, ask) = (u128::from(bbo.bid), u128::from(bbo.ask));
let mid = crate::utils::units::mid(bid, ask).ok_or(Error::Decode("the book is empty or crossed".into()))?;
Ok(Quote { bid, ask, mid })
}
pub async fn deposit_address(&self, owner: Address) -> Result<Address, Error> {
let read = chain::deposit_address(owner).map_err(|e| Error::Decode(e.to_string()))?;
read.run(&self.rpc()).await.map_err(Error::Chain)
}
pub async fn pending_reward(&self, owner: Address) -> Result<u128, Error> {
let read = chain::pending_reward(owner).map_err(|e| Error::Decode(e.to_string()))?;
read.run(&self.rpc()).await.map_err(Error::Chain)
}
pub async fn win_fee_rate(&self) -> Result<u128, Error> {
let read = chain::win_fee_rate().map_err(|e| Error::Decode(e.to_string()))?;
read.run(&self.rpc()).await.map_err(Error::Chain)
}
#[must_use]
pub fn stream_url(&self, owner: Address) -> String {
Call::account_stream(owner).request(&self.config().relayer).url
}
}