1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Metrics sub-client — platform, market, orderbook, category, deposit-token,
//! leaderboard, and history metrics.
use crate::client::LightconeClient;
use crate::domain::metrics::wire::{
CategoriesMetrics, CategoryMetricsQuery, CategoryVolumeMetrics, DepositTokensMetrics,
Leaderboard, MarketDetailMetrics, MarketMetricsQuery, MarketsMetrics, MarketsMetricsQuery,
MetricsHistory, MetricsHistoryQuery, OrderbookMetricsQuery, OrderbookTickersResponse,
OrderbookVolumeMetrics, PlatformMetrics, UserMetrics,
};
use crate::error::SdkError;
use crate::http::RetryPolicy;
use crate::shared::{OrderBookId, PubkeyStr};
fn append_query(url: &mut String, qs: &str) {
if !qs.is_empty() {
url.push(if url.contains('?') { '&' } else { '?' });
url.push_str(qs);
}
}
/// Metrics sub-client. Obtain via [`LightconeClient::metrics`].
pub struct Metrics<'a> {
pub(crate) client: &'a LightconeClient,
}
impl<'a> Metrics<'a> {
/// Fetch platform-wide metrics: total volume, trader counts, active market/orderbook
/// counts, and per-deposit-token breakdowns.
///
/// `GET /api/metrics/platform`
pub async fn platform(&self) -> Result<PlatformMetrics, SdkError> {
let url = format!("{}/api/metrics/platform", self.client.http.base_url());
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// List metrics for all active markets.
///
/// `GET /api/metrics/markets`
pub async fn markets(&self, query: &MarketsMetricsQuery) -> Result<MarketsMetrics, SdkError> {
let mut url = format!("{}/api/metrics/markets", self.client.http.base_url());
if let Ok(qs) = serde_urlencoded::to_string(query) {
append_query(&mut url, &qs);
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Fetch detailed metrics for a single market — including per-outcome, per-orderbook,
/// and per-deposit-token breakdowns.
///
/// `GET /api/metrics/markets/{market_pubkey}`
pub async fn market(
&self,
market_pubkey: &PubkeyStr,
query: &MarketMetricsQuery,
) -> Result<MarketDetailMetrics, SdkError> {
let mut url = format!(
"{}/api/metrics/markets/{}",
self.client.http.base_url(),
urlencoding::encode(market_pubkey.as_str())
);
if let Ok(qs) = serde_urlencoded::to_string(query) {
append_query(&mut url, &qs);
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Batch BBO + midpoint per active orderbook (same shape as the WS
/// `Ticker` stream, delivered in one REST call). Optionally filter to
/// orderbooks whose base conditional-token is backed by `deposit_asset`.
/// Prices per orderbook are scaled using that orderbook's own decimals.
///
/// `GET /api/metrics/orderbooks/tickers[?deposit_asset=<mint>]`
pub async fn orderbook_tickers(
&self,
deposit_asset: Option<&str>,
) -> Result<OrderbookTickersResponse, SdkError> {
let mut url = format!(
"{}/api/metrics/orderbooks/tickers",
self.client.http.base_url()
);
if let Some(mint) = deposit_asset.map(str::trim).filter(|s| !s.is_empty()) {
append_query(
&mut url,
&format!("deposit_asset={}", urlencoding::encode(mint)),
);
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Fetch metrics for a single orderbook, broken down by base/quote/USD volume.
///
/// `GET /api/metrics/orderbooks/{orderbook_id}`
pub async fn orderbook(
&self,
orderbook_id: &OrderBookId,
query: &OrderbookMetricsQuery,
) -> Result<OrderbookVolumeMetrics, SdkError> {
let mut url = format!(
"{}/api/metrics/orderbooks/{}",
self.client.http.base_url(),
urlencoding::encode(orderbook_id.as_str())
);
if let Ok(qs) = serde_urlencoded::to_string(query) {
append_query(&mut url, &qs);
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// List metrics for every market category (e.g. Politics, Sports).
///
/// `GET /api/metrics/categories`
pub async fn categories(&self) -> Result<CategoriesMetrics, SdkError> {
let url = format!("{}/api/metrics/categories", self.client.http.base_url());
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Fetch metrics for a single category.
///
/// `GET /api/metrics/categories/{category}`
pub async fn category(
&self,
category: &str,
query: &CategoryMetricsQuery,
) -> Result<CategoryVolumeMetrics, SdkError> {
let mut url = format!(
"{}/api/metrics/categories/{}",
self.client.http.base_url(),
urlencoding::encode(category)
);
if let Ok(qs) = serde_urlencoded::to_string(query) {
append_query(&mut url, &qs);
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// List metrics per deposit token across the entire platform.
///
/// `GET /api/metrics/deposit-tokens`
pub async fn deposit_tokens(&self) -> Result<DepositTokensMetrics, SdkError> {
let url = format!("{}/api/metrics/deposit-tokens", self.client.http.base_url());
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Fetch the market leaderboard (top markets by 24h volume).
///
/// `GET /api/metrics/leaderboard/markets`
pub async fn leaderboard(&self, limit: Option<u32>) -> Result<Leaderboard, SdkError> {
let mut url = format!(
"{}/api/metrics/leaderboard/markets",
self.client.http.base_url()
);
if let Some(limit) = limit {
append_query(&mut url, &format!("limit={limit}"));
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Fetch a time-series of volume buckets for the given scope and scope key.
///
/// `scope` is one of `"orderbook" | "market" | "category" | "deposit_token" | "platform"`.
/// `scope_key` is the corresponding identifier (e.g. an orderbook ID for
/// `scope = "orderbook"`). `MetricsHistoryQuery::default()` yields `"1h"` resolution
/// with no time bounds.
///
/// `GET /api/metrics/history/{scope}/{scope_key}`
pub async fn history(
&self,
scope: &str,
scope_key: &str,
query: &MetricsHistoryQuery,
) -> Result<MetricsHistory, SdkError> {
let mut url = format!(
"{}/api/metrics/history/{}/{}",
self.client.http.base_url(),
urlencoding::encode(scope),
urlencoding::encode(scope_key)
);
if let Ok(qs) = serde_urlencoded::to_string(query) {
append_query(&mut url, &qs);
}
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Fetch per-wallet trading + referral aggregates for the authenticated
/// user: distinct outcomes traded, total USD volume across all the
/// wallet's trades, and the number of times the wallet's referral codes
/// have been redeemed. The wallet is resolved server-side from the
/// `auth_token` cookie.
///
/// `GET /api/metrics/user`
pub async fn user(&self) -> Result<UserMetrics, SdkError> {
let url = format!("{}/api/metrics/user", self.client.http.base_url());
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
/// Same as [`Self::user`] but uses the supplied `auth_token` for this
/// call instead of the SDK's process-wide token store.
///
/// Intended for server-side cookie forwarding (SSR / Dioxus server
/// functions) where the per-request browser cookie can't propagate to
/// the shared client.
pub async fn user_with_auth(&self, auth_token: &str) -> Result<UserMetrics, SdkError> {
let url = format!("{}/api/metrics/user", self.client.http.base_url());
self.client
.http
.get_with_auth(&url, RetryPolicy::Idempotent, auth_token)
.await
}
/// Public variant of [`Self::user`]. Takes the user's wallet via the URL
/// path (`GET /api/metrics/user/{wallet_address}`) and requires no auth.
pub async fn user_by_wallet(&self, wallet_address: &str) -> Result<UserMetrics, SdkError> {
let url = format!(
"{}/api/metrics/user/{}",
self.client.http.base_url(),
urlencoding::encode(wallet_address)
);
self.client.http.get(&url, RetryPolicy::Idempotent).await
}
}