Skip to main content

strike_sdk/chain/
markets.rs

1//! On-chain market state reads.
2
3use alloy::providers::DynProvider;
4
5use crate::config::StrikeConfig;
6use crate::contracts::MarketFactory;
7use crate::error::{Result, StrikeError};
8
9/// Client for reading on-chain market state.
10pub struct MarketsClient<'a> {
11    provider: &'a DynProvider,
12    config: &'a StrikeConfig,
13}
14
15impl<'a> MarketsClient<'a> {
16    pub(crate) fn new(provider: &'a DynProvider, config: &'a StrikeConfig) -> Self {
17        Self { provider, config }
18    }
19
20    fn factory(&self) -> MarketFactory::MarketFactoryInstance<&DynProvider> {
21        MarketFactory::new(self.config.addresses.market_factory, self.provider)
22    }
23
24    /// Get the number of active markets.
25    pub async fn active_market_count(&self) -> Result<u64> {
26        let count = self
27            .factory()
28            .getActiveMarketCount()
29            .call()
30            .await
31            .map_err(|e| StrikeError::Contract(e.to_string()))?;
32        Ok(count.to::<u64>())
33    }
34
35    /// Get the next factory market ID (total markets created).
36    pub async fn next_market_id(&self) -> Result<u64> {
37        let id = self
38            .factory()
39            .nextFactoryMarketId()
40            .call()
41            .await
42            .map_err(|e| StrikeError::Contract(e.to_string()))?;
43        Ok(id.to::<u64>())
44    }
45
46    /// Get the next order ID from the OrderBook.
47    pub async fn next_order_id(&self) -> Result<u64> {
48        let ob = crate::contracts::OrderBook::new(self.config.addresses.order_book, self.provider);
49        let id = ob
50            .nextOrderId()
51            .call()
52            .await
53            .map_err(|e| StrikeError::Contract(e.to_string()))?;
54        Ok(id)
55    }
56}