use anchor_lang::prelude::*;
#[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,
AutoDeleveraging,
MarketSwap,
MarketIncrease,
MarketDecrease,
LimitSwap,
LimitIncrease,
LimitDecrease,
StopLossDecrease,
}
impl OrderKind {
pub fn is_market(&self) -> bool {
matches!(
self,
Self::MarketSwap | Self::MarketIncrease | Self::MarketDecrease
)
}
pub fn is_swap(&self) -> bool {
matches!(self, Self::MarketSwap | Self::LimitSwap)
}
pub fn is_increase_position(&self) -> bool {
matches!(self, Self::LimitIncrease | Self::MarketIncrease)
}
pub fn is_decrease_position(&self) -> bool {
matches!(
self,
Self::LimitDecrease
| Self::MarketDecrease
| Self::Liquidation
| Self::AutoDeleveraging
| Self::StopLossDecrease
)
}
pub fn is_market_decrease(&self) -> bool {
matches!(self, Self::MarketDecrease)
}
}
#[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,
Short,
}
impl OrderSide {
pub fn is_long(&self) -> bool {
matches!(self, Self::Long)
}
}
#[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,
Long,
Short,
}
#[derive(Clone)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub enum PositionCutKind {
Liquidate,
AutoDeleverage(u128),
}
impl PositionCutKind {
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),
}
}
pub fn to_order_kind(&self) -> OrderKind {
match self {
Self::Liquidate => OrderKind::Liquidation,
Self::AutoDeleverage(_) => OrderKind::AutoDeleveraging,
}
}
}
#[allow(clippy::enum_variant_names)]
#[derive(num_enum::IntoPrimitive)]
#[repr(u8)]
pub enum TradeFlag {
IsLong,
IsCollateralLong,
IsIncrease,
}
crate::flags!(TradeFlag, 8, u8);