polymarket_rs/client/data.rs
1use crate::error::Result;
2use crate::http::HttpClient;
3use crate::request::{ActivityQueryParams, TradeQueryParams};
4use crate::types::{Activity, ClosedPosition, Position, PositionValue, Trade};
5
6/// Client for accessing position and portfolio data
7///
8/// This client provides access to user positions and portfolio values.
9/// It does not require authentication.
10pub struct DataClient {
11 http_client: HttpClient,
12}
13
14impl DataClient {
15 /// Create a new DataClient
16 ///
17 /// # Arguments
18 /// * `host` - The base URL for the data API (typically different from main CLOB API)
19 pub fn new(host: impl Into<String>) -> Self {
20 Self {
21 http_client: HttpClient::new(host),
22 }
23 }
24
25 /// Get all positions for a user
26 ///
27 /// # Arguments
28 /// * `user` - The user's wallet address
29 ///
30 /// # Returns
31 /// A list of positions owned by the user
32 pub async fn get_positions(&self, user: &str) -> Result<Vec<Position>> {
33 let path = format!("/positions?user={}", user);
34 self.http_client.get(&path, None).await
35 }
36
37 /// Get the total value of positions for a user
38 ///
39 /// # Arguments
40 /// * `user` - The user's wallet address
41 ///
42 /// # Returns
43 /// A list of position values for the user
44 pub async fn get_positions_value(&self, user: &str) -> Result<Vec<PositionValue>> {
45 let path = format!("/value?user={}", user);
46 self.http_client.get(&path, None).await
47 }
48
49 /// Get recent trades
50 ///
51 /// # Arguments
52 /// * `user` - User wallet address to filter trades
53 /// * `params` - Optional query parameters (limit, offset, taker_only)
54 ///
55 /// # Returns
56 /// A list of recent trades
57 pub async fn get_trades(
58 &self,
59 user: &str,
60 params: Option<TradeQueryParams>,
61 ) -> Result<Vec<Trade>> {
62 let mut path = format!("/trades?user={}", user);
63
64 if let Some(params) = params {
65 path.push_str(¶ms.to_query_string());
66 }
67
68 println!("{}", path);
69
70 self.http_client.get(&path, None).await
71 }
72
73 /// Get recent activity
74 ///
75 /// # Arguments
76 /// * `user` - User wallet address to filter activity
77 /// * `params` - Optional query parameters (limit, offset, sort_by, sort_direction)
78 ///
79 /// # Returns
80 /// A list of recent activity events
81 pub async fn get_activity(
82 &self,
83 user: &str,
84 params: Option<ActivityQueryParams>,
85 ) -> Result<Vec<Activity>> {
86 let mut path = format!("/activity?user={}", user);
87
88 if let Some(params) = params {
89 path.push_str(¶ms.to_query_string());
90 }
91
92 self.http_client.get(&path, None).await
93 }
94
95 /// Get closed positions
96 ///
97 /// # Arguments
98 /// * `user` - User wallet address
99 ///
100 /// # Returns
101 /// A list of closed positions for the user
102 pub async fn get_closed_positions(&self, user: &str) -> Result<Vec<ClosedPosition>> {
103 let path = format!("/closed-positions?user={}", user);
104 self.http_client.get(&path, None).await
105 }
106}