1use self::delta::PoolDelta;
2
3pub mod balance;
5
6pub mod delta;
8
9pub use self::{
10 balance::{Balance, BalanceExt},
11 delta::Delta,
12};
13
14pub trait Pool: Balance + Sized {
16 fn apply_delta_to_long_amount(&mut self, delta: &Self::Signed) -> crate::Result<()> {
18 *self = self.checked_apply_delta(Delta::new_with_long(delta))?;
19 Ok(())
20 }
21
22 fn apply_delta_to_short_amount(&mut self, delta: &Self::Signed) -> crate::Result<()> {
24 *self = self.checked_apply_delta(Delta::new_with_short(delta))?;
25 Ok(())
26 }
27
28 fn checked_apply_delta(&self, delta: Delta<&Self::Signed>) -> crate::Result<Self>;
30}
31
32pub trait PoolExt: Pool {
34 #[inline]
36 fn apply_delta_amount(&mut self, is_long: bool, delta: &Self::Signed) -> crate::Result<()> {
37 if is_long {
38 self.apply_delta_to_long_amount(delta)
39 } else {
40 self.apply_delta_to_short_amount(delta)
41 }
42 }
43}
44
45impl<P: Pool> PoolExt for P {}
46
47#[derive(
49 Debug,
50 Clone,
51 Copy,
52 Default,
53 num_enum::TryFromPrimitive,
54 num_enum::IntoPrimitive,
55 PartialEq,
56 Eq,
57 PartialOrd,
58 Ord,
59 Hash,
60)]
61#[cfg_attr(
62 feature = "strum",
63 derive(strum::EnumIter, strum::EnumString, strum::Display)
64)]
65#[cfg_attr(feature = "strum", strum(serialize_all = "snake_case"))]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
68#[cfg_attr(
69 feature = "anchor-lang",
70 derive(
71 anchor_lang::AnchorDeserialize,
72 anchor_lang::AnchorSerialize,
73 anchor_lang::InitSpace
74 )
75)]
76#[repr(u8)]
77#[non_exhaustive]
78pub enum PoolKind {
79 #[default]
81 Primary,
82 SwapImpact,
84 ClaimableFee,
86 OpenInterestForLong,
88 OpenInterestForShort,
90 OpenInterestInTokensForLong,
92 OpenInterestInTokensForShort,
94 PositionImpact,
96 BorrowingFactor,
98 FundingAmountPerSizeForLong,
100 FundingAmountPerSizeForShort,
102 ClaimableFundingAmountPerSizeForLong,
104 ClaimableFundingAmountPerSizeForShort,
106 CollateralSumForLong,
108 CollateralSumForShort,
110 TotalBorrowing,
112}