1use anchor_lang::prelude::*;
2
3pub const MAX_ORDER_FLAGS: usize = 8;
5
6#[derive(
8 AnchorSerialize,
9 AnchorDeserialize,
10 Clone,
11 InitSpace,
12 Copy,
13 strum::EnumString,
14 strum::Display,
15 num_enum::IntoPrimitive,
16 num_enum::TryFromPrimitive,
17 Debug,
18)]
19#[strum(serialize_all = "snake_case")]
20#[non_exhaustive]
21#[repr(u8)]
22pub enum OrderKind {
23 Liquidation,
25 AutoDeleveraging,
27 MarketSwap,
31 MarketIncrease,
35 MarketDecrease,
39 LimitSwap,
41 LimitIncrease,
43 LimitDecrease,
45 StopLossDecrease,
47}
48
49impl OrderKind {
50 pub fn is_market(&self) -> bool {
52 matches!(
53 self,
54 Self::MarketSwap | Self::MarketIncrease | Self::MarketDecrease
55 )
56 }
57
58 pub fn is_swap(&self) -> bool {
60 matches!(self, Self::MarketSwap | Self::LimitSwap)
61 }
62
63 pub fn is_increase_position(&self) -> bool {
65 matches!(self, Self::LimitIncrease | Self::MarketIncrease)
66 }
67
68 pub fn is_decrease_position(&self) -> bool {
70 matches!(
71 self,
72 Self::LimitDecrease
73 | Self::MarketDecrease
74 | Self::Liquidation
75 | Self::AutoDeleveraging
76 | Self::StopLossDecrease
77 )
78 }
79
80 pub fn is_market_decrease(&self) -> bool {
82 matches!(self, Self::MarketDecrease)
83 }
84}
85
86#[derive(
88 Clone,
89 Copy,
90 strum::EnumString,
91 strum::Display,
92 num_enum::IntoPrimitive,
93 num_enum::TryFromPrimitive,
94)]
95#[strum(serialize_all = "snake_case")]
96#[cfg_attr(feature = "debug", derive(Debug))]
97#[non_exhaustive]
98#[repr(u8)]
99pub enum OrderSide {
100 Long,
102 Short,
104}
105
106impl OrderSide {
107 pub fn is_long(&self) -> bool {
109 matches!(self, Self::Long)
110 }
111}
112
113#[non_exhaustive]
115#[repr(u8)]
116#[derive(
117 Clone,
118 Copy,
119 num_enum::IntoPrimitive,
120 num_enum::TryFromPrimitive,
121 PartialEq,
122 Eq,
123 strum::EnumString,
124 strum::Display,
125)]
126#[strum(serialize_all = "snake_case")]
127#[cfg_attr(feature = "debug", derive(Debug))]
128#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
129#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
130pub enum PositionKind {
131 Uninitialized,
133 Long,
135 Short,
137}
138
139#[derive(Clone)]
141#[cfg_attr(feature = "debug", derive(Debug))]
142pub enum PositionCutKind {
143 Liquidate,
145 AutoDeleverage(u128),
147}
148
149impl PositionCutKind {
150 pub fn size_delta_usd(&self, size_in_usd: u128) -> u128 {
152 match self {
153 Self::Liquidate => size_in_usd,
154 Self::AutoDeleverage(delta) => size_in_usd.min(*delta),
155 }
156 }
157
158 pub fn to_order_kind(&self) -> OrderKind {
160 match self {
161 Self::Liquidate => OrderKind::Liquidation,
162 Self::AutoDeleverage(_) => OrderKind::AutoDeleveraging,
163 }
164 }
165}
166
167#[allow(clippy::enum_variant_names)]
169#[derive(num_enum::IntoPrimitive)]
170#[repr(u8)]
171pub enum TradeFlag {
172 IsLong,
174 IsCollateralLong,
176 IsIncrease,
178 }
180
181crate::flags!(TradeFlag, 8, u8);
182
183#[repr(u8)]
185#[non_exhaustive]
186#[derive(num_enum::IntoPrimitive, num_enum::TryFromPrimitive)]
187pub enum OrderFlag {
188 ShouldKeepPositionAccount,
190 }