use alloy::primitives::U256;
use serde::{Deserialize, Serialize};
use crate::contracts::OrderBook;
pub const LOT_SIZE: u64 = 10_000_000_000_000_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum Side {
Bid = 0,
Ask = 1,
SellYes = 2,
SellNo = 3,
}
impl From<Side> for u8 {
fn from(s: Side) -> u8 {
s as u8
}
}
impl TryFrom<u8> for Side {
type Error = &'static str;
fn try_from(v: u8) -> Result<Self, Self::Error> {
match v {
0 => Ok(Self::Bid),
1 => Ok(Self::Ask),
2 => Ok(Self::SellYes),
3 => Ok(Self::SellNo),
_ => Err("invalid side value (must be 0-3)"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum OrderType {
GoodTilBatch = 0,
GoodTilCancelled = 1,
}
impl From<OrderType> for u8 {
fn from(o: OrderType) -> u8 {
o as u8
}
}
#[derive(Debug, Clone, Copy)]
pub struct OrderParam {
pub side: Side,
pub order_type: OrderType,
pub tick: u8,
pub lots: u64,
}
impl OrderParam {
pub fn new(side: Side, order_type: OrderType, tick: u8, lots: u64) -> Self {
Self {
side,
order_type,
tick,
lots,
}
}
pub fn bid(tick: u8, lots: u64) -> Self {
Self::new(Side::Bid, OrderType::GoodTilCancelled, tick, lots)
}
pub fn ask(tick: u8, lots: u64) -> Self {
Self::new(Side::Ask, OrderType::GoodTilCancelled, tick, lots)
}
pub fn sell_yes(tick: u8, lots: u64) -> Self {
Self::new(Side::SellYes, OrderType::GoodTilCancelled, tick, lots)
}
pub fn sell_no(tick: u8, lots: u64) -> Self {
Self::new(Side::SellNo, OrderType::GoodTilCancelled, tick, lots)
}
pub(crate) fn to_contract_param(self) -> OrderBook::OrderParam {
OrderBook::OrderParam {
side: self.side as u8,
orderType: self.order_type as u8,
tick: self.tick,
lots: self.lots,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct AmendOrderParam {
pub order_id: U256,
pub new_tick: u8,
pub new_lots: u64,
}
impl AmendOrderParam {
pub fn new(order_id: U256, new_tick: u8, new_lots: u64) -> Self {
Self {
order_id,
new_tick,
new_lots,
}
}
pub(crate) fn to_contract_param(self) -> OrderBook::AmendOrderParam {
OrderBook::AmendOrderParam {
orderId: self.order_id,
newTick: self.new_tick,
newLots: self.new_lots,
}
}
}
#[derive(Debug, Clone)]
pub struct PlacedOrder {
pub order_id: U256,
pub side: Side,
pub market_id: u64,
pub orderbook_market_id: u64,
}
#[derive(Debug, Clone)]
pub enum StrikeEvent {
MarketCreated {
market_id: u64,
price_id: [u8; 32],
strike_price: i64,
expiry_time: u64,
},
BatchCleared {
market_id: u64,
batch_id: u64,
clearing_tick: u64,
matched_lots: u64,
},
OrderSettled {
order_id: U256,
owner: alloy::primitives::Address,
filled_lots: u64,
},
GtcAutoCancelled {
order_id: U256,
owner: alloy::primitives::Address,
},
OrderPlaced {
order_id: U256,
market_id: u64,
side: u8,
tick: u8,
lots: u64,
owner: alloy::primitives::Address,
},
OrderCancelled {
order_id: U256,
market_id: u64,
owner: alloy::primitives::Address,
},
}