polyte_clob/
types.rs

1use std::fmt;
2
3use alloy::primitives::Address;
4use serde::{Deserialize, Serialize};
5
6/// API credentials for L2 authentication
7#[derive(Clone, Serialize, Deserialize)]
8pub struct Credentials {
9    pub key: String,
10    pub secret: String,
11    pub passphrase: String,
12}
13
14impl fmt::Debug for Credentials {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        f.debug_struct("Credentials")
17            .field("key", &"<redacted>")
18            .field("secret", &"<redacted>")
19            .field("passphrase", &"<redacted>")
20            .finish()
21    }
22}
23
24/// Order side (buy or sell)
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "UPPERCASE")]
27pub enum OrderSide {
28    Buy,
29    Sell,
30}
31
32impl fmt::Display for OrderSide {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::Buy => write!(f, "BUY"),
36            Self::Sell => write!(f, "SELL"),
37        }
38    }
39}
40
41/// Order type/kind
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
43#[serde(rename_all = "UPPERCASE")]
44pub enum OrderKind {
45    /// Good-till-Cancelled
46    Gtc,
47    /// Fill-or-Kill
48    Fok,
49    /// Good-till-Date
50    Gtd,
51    /// Fill-and-Kill
52    Fak,
53}
54
55/// Signature type
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
57#[serde(rename_all = "kebab-case")]
58pub enum SignatureType {
59    Eoa,
60    PolyProxy,
61    PolyGnosisSafe,
62}
63
64impl Default for SignatureType {
65    fn default() -> Self {
66        Self::Eoa
67    }
68}
69
70/// Tick size (minimum price increment)
71#[derive(Debug, Clone, Copy, PartialEq)]
72pub enum TickSize {
73    /// 0.1
74    Tenth,
75    /// 0.01
76    Hundredth,
77    /// 0.001
78    Thousandth,
79    /// 0.0001
80    TenThousandth,
81}
82
83impl TickSize {
84    pub fn as_f64(&self) -> f64 {
85        match self {
86            Self::Tenth => 0.1,
87            Self::Hundredth => 0.01,
88            Self::Thousandth => 0.001,
89            Self::TenThousandth => 0.0001,
90        }
91    }
92
93    pub fn decimals(&self) -> u32 {
94        match self {
95            Self::Tenth => 1,
96            Self::Hundredth => 2,
97            Self::Thousandth => 3,
98            Self::TenThousandth => 4,
99        }
100    }
101}
102
103impl From<&str> for TickSize {
104    fn from(s: &str) -> Self {
105        match s {
106            "0.1" => Self::Tenth,
107            "0.01" => Self::Hundredth,
108            "0.001" => Self::Thousandth,
109            "0.0001" => Self::TenThousandth,
110            _ => Self::Hundredth, // Default
111        }
112    }
113}
114
115impl From<f64> for TickSize {
116    fn from(n: f64) -> Self {
117        const EPSILON: f64 = 1e-10;
118        if (n - 0.1).abs() < EPSILON {
119            Self::Tenth
120        } else if (n - 0.01).abs() < EPSILON {
121            Self::Hundredth
122        } else if (n - 0.001).abs() < EPSILON {
123            Self::Thousandth
124        } else if (n - 0.0001).abs() < EPSILON {
125            Self::TenThousandth
126        } else {
127            Self::Hundredth // Default
128        }
129    }
130}
131
132/// Unsigned order
133#[derive(Debug, Clone, Serialize, Deserialize)]
134#[serde(rename_all = "camelCase")]
135pub struct Order {
136    pub salt: String,
137    pub maker: Address,
138    pub signer: Address,
139    pub taker: Address,
140    pub token_id: String,
141    pub maker_amount: String,
142    pub taker_amount: String,
143    pub expiration: String,
144    pub nonce: String,
145    pub fee_rate_bps: String,
146    pub side: OrderSide,
147    pub signature_type: SignatureType,
148}
149
150/// Signed order
151#[derive(Debug, Clone, Serialize, Deserialize)]
152#[serde(rename_all = "camelCase")]
153pub struct SignedOrder {
154    #[serde(flatten)]
155    pub order: Order,
156    pub signature: String,
157}