polyte_clob/
types.rs

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