gmsol_utils/
order.rs

1use anchor_lang::prelude::*;
2
3/// Order Kind.
4#[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: allows liquidation of positions if the criteria for liquidation are met.
21    Liquidation,
22    /// Auto-deleveraging Order.
23    AutoDeleveraging,
24    /// Swap token A to token B at the current market price.
25    ///
26    /// The order will be cancelled if the `min_output_amount` cannot be fulfilled.
27    MarketSwap,
28    /// Increase position at the current market price.
29    ///
30    /// The order will be cancelled if the position cannot be increased at the acceptable price.
31    MarketIncrease,
32    /// Decrease position at the current market price.
33    ///
34    /// The order will be cancelled if the position cannot be decreased at the acceptable price.
35    MarketDecrease,
36    /// Limit Swap.
37    LimitSwap,
38    /// Limit Increase.
39    LimitIncrease,
40    /// Limit Decrease.
41    LimitDecrease,
42    /// Stop-Loss Decrease.
43    StopLossDecrease,
44}
45
46impl OrderKind {
47    /// Is market order.
48    pub fn is_market(&self) -> bool {
49        matches!(
50            self,
51            Self::MarketSwap | Self::MarketIncrease | Self::MarketDecrease
52        )
53    }
54
55    /// Is swap order.
56    pub fn is_swap(&self) -> bool {
57        matches!(self, Self::MarketSwap | Self::LimitSwap)
58    }
59
60    /// Is increase position order.
61    pub fn is_increase_position(&self) -> bool {
62        matches!(self, Self::LimitIncrease | Self::MarketIncrease)
63    }
64
65    /// Is decrease position order.
66    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    /// Is market decrease.
78    pub fn is_market_decrease(&self) -> bool {
79        matches!(self, Self::MarketDecrease)
80    }
81}
82
83/// Order side.
84#[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.
98    Long,
99    /// Short.
100    Short,
101}
102
103impl OrderSide {
104    /// Return whether the side is long.
105    pub fn is_long(&self) -> bool {
106        matches!(self, Self::Long)
107    }
108}
109
110/// Position Kind.
111#[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.
129    Uninitialized,
130    /// Long position.
131    Long,
132    /// Short position.
133    Short,
134}
135
136/// Position Cut Kind.
137#[derive(Clone)]
138#[cfg_attr(feature = "debug", derive(Debug))]
139pub enum PositionCutKind {
140    /// Liquidate.
141    Liquidate,
142    /// AutoDeleverage.
143    AutoDeleverage(u128),
144}
145
146impl PositionCutKind {
147    /// Get size delta.
148    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    /// Convert into [`OrderKind`].
156    pub fn to_order_kind(&self) -> OrderKind {
157        match self {
158            Self::Liquidate => OrderKind::Liquidation,
159            Self::AutoDeleverage(_) => OrderKind::AutoDeleveraging,
160        }
161    }
162}