gmsol_model/pool/
mod.rs

1use self::delta::PoolDelta;
2
3/// Balance.
4pub mod balance;
5
6/// Delta.
7pub mod delta;
8
9pub use self::{
10    balance::{Balance, BalanceExt},
11    delta::Delta,
12};
13
14/// A balance for holding tokens, usd values, or factors
15pub trait Pool: Balance + Sized {
16    /// Apply delta to long amount.
17    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    /// Apply delta to short amount.
23    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    /// Checked apply delta amounts.
29    fn checked_apply_delta(&self, delta: Delta<&Self::Signed>) -> crate::Result<Self>;
30}
31
32/// Extension trait for [`Pool`] with utils.
33pub trait PoolExt: Pool {
34    /// Apply delta.
35    #[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/// Pool kind.
48#[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    /// Primary liquidity pool.
80    #[default]
81    Primary,
82    /// Swap impact.
83    SwapImpact,
84    /// Claimable fee.
85    ClaimableFee,
86    /// Open Interest for long.
87    OpenInterestForLong,
88    /// Open Interest for short.
89    OpenInterestForShort,
90    /// Open Interest in tokens for long.
91    OpenInterestInTokensForLong,
92    /// Open Interest in tokens for short.
93    OpenInterestInTokensForShort,
94    /// Position impact.
95    PositionImpact,
96    /// Borrowing factor.
97    BorrowingFactor,
98    /// Funding amount per size for long.
99    FundingAmountPerSizeForLong,
100    /// Funding amount per size for short.
101    FundingAmountPerSizeForShort,
102    /// Claimable funding amount per size for long.
103    ClaimableFundingAmountPerSizeForLong,
104    /// Claimable funding amount per size for short.
105    ClaimableFundingAmountPerSizeForShort,
106    /// Collateral sum for long.
107    CollateralSumForLong,
108    /// Collateral sum for short.
109    CollateralSumForShort,
110    /// Total borrowing.
111    TotalBorrowing,
112}