df_papertrade_core/client/
reads.rs1use df_hyperliquid_core::client::Http;
5use df_hyperliquid_core::client::evm::precompiles;
6use df_hyperliquid_core::utils::Address;
7
8use crate::client::{Call, Client, Error, chain};
9use crate::markets::{QueueRank, QueueSummary, Summary, Trade, TradingState, stats};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub struct Quote {
14 pub bid: u128,
15 pub ask: u128,
16 pub mid: u128,
18}
19
20impl<H: Http + Clone> Client<H> {
21 async fn text(&self, call: &Call) -> Result<String, Error> {
22 Ok(self.relayer().send(call).await?)
23 }
24
25 pub async fn trading_state(&self) -> Result<TradingState, Error> {
28 Ok(TradingState::parse(&self.text(&Call::trading_state()).await?)?)
29 }
30
31 pub async fn summary(&self) -> Result<Summary, Error> {
33 Ok(Summary::parse(&self.text(&Call::summary()).await?)?)
34 }
35
36 pub async fn volumes(&self) -> Result<Vec<(u32, u128)>, Error> {
38 Ok(stats::market_volumes(&self.text(&Call::market_stats()).await?)?)
39 }
40
41 pub async fn queue(&self) -> Result<QueueSummary, Error> {
43 Ok(QueueSummary::parse(&self.text(&Call::queue_summary()).await?)?)
44 }
45
46 pub async fn recent_trades(&self) -> Result<Vec<Trade>, Error> {
48 Ok(stats::recent_trades(&self.text(&Call::recent_trades()).await?)?)
49 }
50
51 pub async fn queue_rank(&self, owner: Address) -> Result<QueueRank, Error> {
53 Ok(QueueRank::parse(&self.text(&Call::queue_rank(owner)).await?)?)
54 }
55
56 pub async fn quote(&self, asset_index: u32) -> Result<Quote, Error> {
59 let bbo = self.rpc().read(&precompiles::bbo(asset_index)).await.map_err(Error::Precompile)?;
60 let (bid, ask) = (u128::from(bbo.bid), u128::from(bbo.ask));
61 let mid = crate::utils::units::mid(bid, ask).ok_or(Error::Decode("the book is empty or crossed".into()))?;
62 Ok(Quote { bid, ask, mid })
63 }
64
65 pub async fn deposit_address(&self, owner: Address) -> Result<Address, Error> {
68 let read = chain::deposit_address(owner).map_err(|e| Error::Decode(e.to_string()))?;
69 read.run(&self.rpc()).await.map_err(Error::Chain)
70 }
71
72 pub async fn pending_reward(&self, owner: Address) -> Result<u128, Error> {
74 let read = chain::pending_reward(owner).map_err(|e| Error::Decode(e.to_string()))?;
75 read.run(&self.rpc()).await.map_err(Error::Chain)
76 }
77
78 pub async fn win_fee_rate(&self) -> Result<u128, Error> {
80 let read = chain::win_fee_rate().map_err(|e| Error::Decode(e.to_string()))?;
81 read.run(&self.rpc()).await.map_err(Error::Chain)
82 }
83
84 #[must_use]
88 pub fn stream_url(&self, owner: Address) -> String {
89 Call::account_stream(owner).request(&self.config().relayer).url
90 }
91}