1use anchor_lang::prelude::*;
2
3#[derive(
5 AnchorSerialize,
6 AnchorDeserialize,
7 Clone,
8 InitSpace,
9 Copy,
10 strum::EnumString,
11 strum::Display,
12 num_enum::IntoPrimitive,
13 num_enum::TryFromPrimitive,
14 Debug,
15)]
16#[strum(serialize_all = "snake_case")]
17#[non_exhaustive]
18#[repr(u8)]
19pub enum OrderKind {
20 Liquidation,
22 AutoDeleveraging,
24 MarketSwap,
28 MarketIncrease,
32 MarketDecrease,
36 LimitSwap,
38 LimitIncrease,
40 LimitDecrease,
42 StopLossDecrease,
44}
45
46impl OrderKind {
47 pub fn is_market(&self) -> bool {
49 matches!(
50 self,
51 Self::MarketSwap | Self::MarketIncrease | Self::MarketDecrease
52 )
53 }
54
55 pub fn is_swap(&self) -> bool {
57 matches!(self, Self::MarketSwap | Self::LimitSwap)
58 }
59
60 pub fn is_increase_position(&self) -> bool {
62 matches!(self, Self::LimitIncrease | Self::MarketIncrease)
63 }
64
65 pub fn is_decrease_position(&self) -> bool {
67 matches!(
68 self,
69 Self::LimitDecrease
70 | Self::MarketDecrease
71 | Self::Liquidation
72 | Self::AutoDeleveraging
73 | Self::StopLossDecrease
74 )
75 }
76
77 pub fn is_market_decrease(&self) -> bool {
79 matches!(self, Self::MarketDecrease)
80 }
81}
82
83#[derive(
85 Clone,
86 Copy,
87 strum::EnumString,
88 strum::Display,
89 num_enum::IntoPrimitive,
90 num_enum::TryFromPrimitive,
91)]
92#[strum(serialize_all = "snake_case")]
93#[cfg_attr(feature = "debug", derive(Debug))]
94#[non_exhaustive]
95#[repr(u8)]
96pub enum OrderSide {
97 Long,
99 Short,
101}
102
103impl OrderSide {
104 pub fn is_long(&self) -> bool {
106 matches!(self, Self::Long)
107 }
108}
109
110#[non_exhaustive]
112#[repr(u8)]
113#[derive(
114 Clone,
115 Copy,
116 num_enum::IntoPrimitive,
117 num_enum::TryFromPrimitive,
118 PartialEq,
119 Eq,
120 strum::EnumString,
121 strum::Display,
122)]
123#[strum(serialize_all = "snake_case")]
124#[cfg_attr(feature = "debug", derive(Debug))]
125#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
126#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
127pub enum PositionKind {
128 Uninitialized,
130 Long,
132 Short,
134}
135
136#[derive(Clone)]
138#[cfg_attr(feature = "debug", derive(Debug))]
139pub enum PositionCutKind {
140 Liquidate,
142 AutoDeleverage(u128),
144}
145
146impl PositionCutKind {
147 pub fn size_delta_usd(&self, size_in_usd: u128) -> u128 {
149 match self {
150 Self::Liquidate => size_in_usd,
151 Self::AutoDeleverage(delta) => size_in_usd.min(*delta),
152 }
153 }
154
155 pub fn to_order_kind(&self) -> OrderKind {
157 match self {
158 Self::Liquidate => OrderKind::Liquidation,
159 Self::AutoDeleverage(_) => OrderKind::AutoDeleveraging,
160 }
161 }
162}