1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use anchor_lang::prelude::*;
/// Max number of order flags.
pub const MAX_ORDER_FLAGS: usize = 8;
/// Order Kind.
#[derive(
AnchorSerialize,
AnchorDeserialize,
Clone,
InitSpace,
Copy,
strum::EnumString,
strum::Display,
num_enum::IntoPrimitive,
num_enum::TryFromPrimitive,
Debug,
)]
#[strum(serialize_all = "snake_case")]
#[non_exhaustive]
#[repr(u8)]
pub enum OrderKind {
/// Liquidation: allows liquidation of positions if the criteria for liquidation are met.
Liquidation,
/// Auto-deleveraging Order.
AutoDeleveraging,
/// Swap token A to token B at the current market price.
///
/// The order will be cancelled if the `min_output_amount` cannot be fulfilled.
MarketSwap,
/// Increase position at the current market price.
///
/// The order will be cancelled if the position cannot be increased at the acceptable price.
MarketIncrease,
/// Decrease position at the current market price.
///
/// The order will be cancelled if the position cannot be decreased at the acceptable price.
MarketDecrease,
/// Limit Swap.
LimitSwap,
/// Limit Increase.
LimitIncrease,
/// Limit Decrease.
LimitDecrease,
/// Stop-Loss Decrease.
StopLossDecrease,
}
impl OrderKind {
/// Is market order.
pub fn is_market(&self) -> bool {
matches!(
self,
Self::MarketSwap | Self::MarketIncrease | Self::MarketDecrease
)
}
/// Is swap order.
pub fn is_swap(&self) -> bool {
matches!(self, Self::MarketSwap | Self::LimitSwap)
}
/// Is increase position order.
pub fn is_increase_position(&self) -> bool {
matches!(self, Self::LimitIncrease | Self::MarketIncrease)
}
/// Is decrease position order.
pub fn is_decrease_position(&self) -> bool {
matches!(
self,
Self::LimitDecrease
| Self::MarketDecrease
| Self::Liquidation
| Self::AutoDeleveraging
| Self::StopLossDecrease
)
}
/// Is market decrease.
pub fn is_market_decrease(&self) -> bool {
matches!(self, Self::MarketDecrease)
}
}
/// Order side.
#[derive(
Clone,
Copy,
strum::EnumString,
strum::Display,
num_enum::IntoPrimitive,
num_enum::TryFromPrimitive,
)]
#[strum(serialize_all = "snake_case")]
#[cfg_attr(feature = "debug", derive(Debug))]
#[non_exhaustive]
#[repr(u8)]
pub enum OrderSide {
/// Long.
Long,
/// Short.
Short,
}
impl OrderSide {
/// Return whether the side is long.
pub fn is_long(&self) -> bool {
matches!(self, Self::Long)
}
}
/// Position Kind.
#[non_exhaustive]
#[repr(u8)]
#[derive(
Clone,
Copy,
num_enum::IntoPrimitive,
num_enum::TryFromPrimitive,
PartialEq,
Eq,
strum::EnumString,
strum::Display,
)]
#[strum(serialize_all = "snake_case")]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum PositionKind {
/// Uninitialized.
Uninitialized,
/// Long position.
Long,
/// Short position.
Short,
}
/// Position Cut Kind.
#[derive(Clone)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub enum PositionCutKind {
/// Liquidate.
Liquidate,
/// AutoDeleverage.
AutoDeleverage(u128),
}
impl PositionCutKind {
/// Get size delta.
pub fn size_delta_usd(&self, size_in_usd: u128) -> u128 {
match self {
Self::Liquidate => size_in_usd,
Self::AutoDeleverage(delta) => size_in_usd.min(*delta),
}
}
/// Convert into [`OrderKind`].
pub fn to_order_kind(&self) -> OrderKind {
match self {
Self::Liquidate => OrderKind::Liquidation,
Self::AutoDeleverage(_) => OrderKind::AutoDeleveraging,
}
}
}
/// Trade Data Flags.
#[allow(clippy::enum_variant_names)]
#[derive(num_enum::IntoPrimitive)]
#[repr(u8)]
pub enum TradeFlag {
/// Is long.
IsLong,
/// Is collateral long.
IsCollateralLong,
/// Is increase.
IsIncrease,
// CHECK: cannot have more than `8` flags.
}
crate::flags!(TradeFlag, 8, u8);
/// Order Flags.
#[repr(u8)]
#[non_exhaustive]
#[derive(num_enum::IntoPrimitive, num_enum::TryFromPrimitive)]
pub enum OrderFlag {
/// Whether to keep position account when empty.
ShouldKeepPositionAccount,
// CHECK: should have no more than `MAX_ORDER_FLAGS` of flags.
}