Skip to main content

strike_sdk/
types.rs

1//! Core SDK types: Side, OrderType, OrderParam, and event types.
2
3use alloy::primitives::U256;
4use serde::{Deserialize, Serialize};
5
6use crate::contracts::OrderBook;
7
8/// LOT_SIZE = 1e16 wei = $0.01 per lot.
9pub const LOT_SIZE: u64 = 10_000_000_000_000_000;
10
11/// Order side on the 4-sided orderbook.
12///
13/// - `Bid` / `Ask` — standard buy/sell with USDT collateral
14/// - `SellYes` / `SellNo` — sell existing outcome tokens back into the book
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16#[repr(u8)]
17pub enum Side {
18    Bid = 0,
19    Ask = 1,
20    SellYes = 2,
21    SellNo = 3,
22}
23
24impl From<Side> for u8 {
25    fn from(s: Side) -> u8 {
26        s as u8
27    }
28}
29
30impl TryFrom<u8> for Side {
31    type Error = &'static str;
32    fn try_from(v: u8) -> Result<Self, Self::Error> {
33        match v {
34            0 => Ok(Self::Bid),
35            1 => Ok(Self::Ask),
36            2 => Ok(Self::SellYes),
37            3 => Ok(Self::SellNo),
38            _ => Err("invalid side value (must be 0-3)"),
39        }
40    }
41}
42
43/// Order type: Good-Til-Batch or Good-Til-Cancelled.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
45#[repr(u8)]
46pub enum OrderType {
47    /// Expires at end of current batch if unfilled.
48    GoodTilBatch = 0,
49    /// Rolls unfilled remainder to the next batch.
50    GoodTilCancelled = 1,
51}
52
53impl From<OrderType> for u8 {
54    fn from(o: OrderType) -> u8 {
55        o as u8
56    }
57}
58
59/// Parameters for placing a single order.
60///
61/// Use the convenience constructors [`OrderParam::bid`], [`OrderParam::ask`],
62/// [`OrderParam::sell_yes`], [`OrderParam::sell_no`] for common cases.
63#[derive(Debug, Clone, Copy)]
64pub struct OrderParam {
65    /// Order side (Bid, Ask, SellYes, SellNo).
66    pub side: Side,
67    /// Order type (GoodTilBatch or GoodTilCancelled).
68    pub order_type: OrderType,
69    /// Price tick (1–99), representing $0.01 to $0.99 probability.
70    pub tick: u8,
71    /// Number of lots. Each lot = $0.01 (LOT_SIZE = 1e16 wei).
72    pub lots: u64,
73}
74
75impl OrderParam {
76    /// Create a new order parameter.
77    pub fn new(side: Side, order_type: OrderType, tick: u8, lots: u64) -> Self {
78        Self {
79            side,
80            order_type,
81            tick,
82            lots,
83        }
84    }
85
86    /// GTC bid at `tick` for `lots`.
87    pub fn bid(tick: u8, lots: u64) -> Self {
88        Self::new(Side::Bid, OrderType::GoodTilCancelled, tick, lots)
89    }
90
91    /// GTC ask at `tick` for `lots`.
92    pub fn ask(tick: u8, lots: u64) -> Self {
93        Self::new(Side::Ask, OrderType::GoodTilCancelled, tick, lots)
94    }
95
96    /// GTC sell-yes at `tick` for `lots`.
97    pub fn sell_yes(tick: u8, lots: u64) -> Self {
98        Self::new(Side::SellYes, OrderType::GoodTilCancelled, tick, lots)
99    }
100
101    /// GTC sell-no at `tick` for `lots`.
102    pub fn sell_no(tick: u8, lots: u64) -> Self {
103        Self::new(Side::SellNo, OrderType::GoodTilCancelled, tick, lots)
104    }
105
106    /// Convert to the on-chain `OrderBook::OrderParam` struct.
107    pub(crate) fn to_contract_param(self) -> OrderBook::OrderParam {
108        OrderBook::OrderParam {
109            side: self.side as u8,
110            orderType: self.order_type as u8,
111            tick: self.tick,
112            lots: self.lots,
113        }
114    }
115}
116
117/// An order that was placed on-chain, with its assigned ID.
118#[derive(Debug, Clone)]
119pub struct PlacedOrder {
120    /// On-chain order ID (from OrderPlaced event).
121    pub order_id: U256,
122    /// The side this order was placed on.
123    pub side: Side,
124    /// Market ID.
125    pub market_id: u64,
126}
127
128/// On-chain event types emitted by Strike contracts.
129#[derive(Debug, Clone)]
130pub enum StrikeEvent {
131    /// A new market was created.
132    MarketCreated {
133        market_id: u64,
134        price_id: [u8; 32],
135        strike_price: i64,
136        expiry_time: u64,
137    },
138    /// A batch was cleared (auction resolved).
139    BatchCleared {
140        market_id: u64,
141        batch_id: u64,
142        clearing_tick: u64,
143        matched_lots: u64,
144    },
145    /// An order was settled after batch clearing.
146    OrderSettled {
147        order_id: U256,
148        owner: alloy::primitives::Address,
149        filled_lots: u64,
150    },
151    /// A GTC order was auto-cancelled.
152    GtcAutoCancelled {
153        order_id: U256,
154        owner: alloy::primitives::Address,
155    },
156    /// An order was placed.
157    OrderPlaced {
158        order_id: U256,
159        market_id: u64,
160        side: u8,
161        tick: u8,
162        lots: u64,
163        owner: alloy::primitives::Address,
164    },
165    /// An order was cancelled.
166    OrderCancelled {
167        order_id: U256,
168        market_id: u64,
169        owner: alloy::primitives::Address,
170    },
171}