Skip to main content

lightcone_sdk/api/types/
trade.rs

1//! Trade-related types for the Lightcone REST API.
2
3use serde::{Deserialize, Serialize};
4
5/// Trade side enum for API responses (uses uppercase string format: "BID"/"ASK").
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7#[serde(rename_all = "UPPERCASE")]
8pub enum ApiTradeSide {
9    /// Buy side
10    Bid,
11    /// Sell side
12    Ask,
13}
14
15/// Executed trade information.
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct Trade {
18    /// Trade ID
19    pub id: i64,
20    /// Orderbook ID
21    pub orderbook_id: String,
22    /// Taker's pubkey
23    pub taker_pubkey: String,
24    /// Maker's pubkey
25    pub maker_pubkey: String,
26    /// Trade side
27    pub side: ApiTradeSide,
28    /// Trade size as decimal string
29    pub size: String,
30    /// Trade price as decimal string
31    pub price: String,
32    /// Taker fee as decimal string
33    pub taker_fee: String,
34    /// Maker fee as decimal string
35    pub maker_fee: String,
36    /// Execution timestamp (milliseconds since epoch)
37    pub executed_at: i64,
38}
39
40/// Query parameters for GET /api/trades.
41#[derive(Debug, Clone, Default, Serialize, Deserialize)]
42pub struct TradesParams {
43    /// Orderbook identifier (required)
44    pub orderbook_id: String,
45    /// Filter by user pubkey
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub user_pubkey: Option<String>,
48    /// Start timestamp (milliseconds)
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub from: Option<i64>,
51    /// End timestamp (milliseconds)
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub to: Option<i64>,
54    /// Pagination cursor (trade ID)
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub cursor: Option<i64>,
57    /// Max results (1-500)
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub limit: Option<u32>,
60}
61
62impl TradesParams {
63    /// Create new params with required orderbook_id.
64    pub fn new(orderbook_id: impl Into<String>) -> Self {
65        Self {
66            orderbook_id: orderbook_id.into(),
67            ..Default::default()
68        }
69    }
70
71    /// Set user pubkey filter.
72    pub fn with_user(mut self, user_pubkey: impl Into<String>) -> Self {
73        self.user_pubkey = Some(user_pubkey.into());
74        self
75    }
76
77    /// Set time range.
78    pub fn with_time_range(mut self, from: i64, to: i64) -> Self {
79        self.from = Some(from);
80        self.to = Some(to);
81        self
82    }
83
84    /// Set pagination cursor.
85    pub fn with_cursor(mut self, cursor: i64) -> Self {
86        self.cursor = Some(cursor);
87        self
88    }
89
90    /// Set result limit.
91    pub fn with_limit(mut self, limit: u32) -> Self {
92        self.limit = Some(limit);
93        self
94    }
95}
96
97/// Response for GET /api/trades.
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct TradesResponse {
100    /// Orderbook ID
101    pub orderbook_id: String,
102    /// Trade list
103    pub trades: Vec<Trade>,
104    /// Next pagination cursor
105    pub next_cursor: Option<i64>,
106    /// Whether more results exist
107    pub has_more: bool,
108}