gmsol_store/events/
market.rs

1use anchor_lang::prelude::*;
2use borsh::BorshSerialize;
3
4use gmsol_model::{
5    action::{
6        distribute_position_impact::DistributePositionImpactReport,
7        update_borrowing_state::UpdateBorrowingReport, update_funding_state::UpdateFundingReport,
8    },
9    PoolKind,
10};
11
12use crate::states::{
13    market::{pool::Pool, Clocks},
14    OtherState,
15};
16
17use super::Event;
18
19/// Market fees updated event.
20#[event]
21#[cfg_attr(feature = "debug", derive(Debug))]
22pub struct MarketFeesUpdated {
23    /// Revision.
24    pub rev: u64,
25    /// Market token.
26    pub market_token: Pubkey,
27    /// Position impact distribution report.
28    pub position_impact_distribution: DistributePositionImpactReport<u128>,
29    /// Update borrowing state report.
30    pub update_borrowing_state: UpdateBorrowingReport<u128>,
31    /// Update funding state report.
32    pub update_funding_state: UpdateFundingReport<u128, i128>,
33}
34
35impl gmsol_utils::InitSpace for MarketFeesUpdated {
36    const INIT_SPACE: usize = 8
37        + 32
38        + DistributePositionImpactReport::<u128>::INIT_SPACE
39        + UpdateBorrowingReport::<u128>::INIT_SPACE
40        + UpdateFundingReport::<u128, i128>::INIT_SPACE;
41}
42
43impl Event for MarketFeesUpdated {}
44
45impl MarketFeesUpdated {
46    /// Create from reports.
47    pub fn from_reports(
48        rev: u64,
49        market_token: Pubkey,
50        position_impact_distribution: DistributePositionImpactReport<u128>,
51        update_borrowing_state: UpdateBorrowingReport<u128>,
52        update_funding_state: UpdateFundingReport<u128, i128>,
53    ) -> Self {
54        Self {
55            rev,
56            market_token,
57            position_impact_distribution,
58            update_borrowing_state,
59            update_funding_state,
60        }
61    }
62}
63
64/// Market borrowing fees updated event.
65#[event]
66#[cfg_attr(feature = "debug", derive(Debug))]
67pub struct BorrowingFeesUpdated {
68    /// Revision.
69    pub rev: u64,
70    /// Market token.
71    pub market_token: Pubkey,
72    /// Update borrowing state report.
73    pub update_borrowing_state: UpdateBorrowingReport<u128>,
74}
75
76impl gmsol_utils::InitSpace for BorrowingFeesUpdated {
77    const INIT_SPACE: usize = 8 + 32 + UpdateBorrowingReport::<u128>::INIT_SPACE;
78}
79
80impl Event for BorrowingFeesUpdated {}
81
82impl BorrowingFeesUpdated {
83    /// Create from report.
84    pub fn from_report(
85        rev: u64,
86        market_token: Pubkey,
87        update_borrowing_state: UpdateBorrowingReport<u128>,
88    ) -> Self {
89        Self {
90            rev,
91            market_token,
92            update_borrowing_state,
93        }
94    }
95}
96
97/// A pool for market.
98#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
99#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100#[derive(AnchorSerialize, AnchorDeserialize, InitSpace)]
101pub struct EventPool {
102    /// Whether the pool only contains one kind of token,
103    /// i.e. a pure pool.
104    /// For a pure pool, only the `long_token_amount` field is used.
105    pub is_pure: u8,
106    #[cfg_attr(feature = "serde", serde(skip))]
107    #[cfg_attr(feature = "debug", debug(skip))]
108    pub(crate) padding: [u8; 15],
109    /// Long token amount.
110    pub long_token_amount: u128,
111    /// Short token amount.
112    pub short_token_amount: u128,
113}
114
115static_assertions::const_assert_eq!(EventPool::INIT_SPACE, Pool::INIT_SPACE);
116
117/// Market clocks.
118#[derive(AnchorSerialize, AnchorDeserialize, InitSpace)]
119#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
120#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
121pub struct EventClocks {
122    #[cfg_attr(feature = "debug", debug(skip))]
123    pub(crate) padding: [u8; 8],
124    /// Revision.
125    pub rev: u64,
126    /// Price impact distribution clock.
127    pub price_impact_distribution: i64,
128    /// Borrowing clock.
129    pub borrowing: i64,
130    /// Funding clock.
131    pub funding: i64,
132    /// ADL updated clock for long.
133    pub adl_for_long: i64,
134    /// ADL updated clock for short.
135    pub adl_for_short: i64,
136    #[cfg_attr(feature = "debug", debug(skip))]
137    pub(crate) reserved: [i64; 3],
138}
139
140static_assertions::const_assert_eq!(EventClocks::INIT_SPACE, Clocks::INIT_SPACE);
141
142/// Market State.
143#[derive(AnchorSerialize, AnchorDeserialize, InitSpace)]
144#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
145#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
146pub struct EventOtherState {
147    #[cfg_attr(feature = "debug", debug(skip))]
148    pub(crate) padding: [u8; 16],
149    /// Revision.
150    pub rev: u64,
151    /// Trade count.
152    pub trade_count: u64,
153    /// Long token balance.
154    pub long_token_balance: u64,
155    /// Short token balance.
156    pub short_token_balance: u64,
157    /// Funding factor per second.
158    pub funding_factor_per_second: i128,
159    #[cfg_attr(feature = "debug", debug(skip))]
160    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
161    pub(crate) reserved: [u8; 256],
162}
163
164static_assertions::const_assert_eq!(EventOtherState::INIT_SPACE, OtherState::INIT_SPACE);
165
166/// Market State Updated Event.
167#[event]
168#[cfg_attr(feature = "debug", derive(Debug))]
169pub struct MarketStateUpdated {
170    /// Revision.
171    rev: u64,
172    /// Market token.
173    market_token: Pubkey,
174    /// Updated pool kinds.
175    pool_kinds: Vec<PoolKind>,
176    /// Updated pools.
177    pools: Vec<EventPool>,
178    /// Clocks.
179    clocks: Vec<EventClocks>,
180    /// Other states.
181    other: Vec<EventOtherState>,
182}
183
184impl MarketStateUpdated {
185    const fn space(num_pools: usize, num_clocks: usize, num_other: usize) -> usize {
186        8 + 32
187            + (4 + PoolKind::INIT_SPACE * num_pools)
188            + (4 + Pool::INIT_SPACE * num_pools)
189            + (4 + Clocks::INIT_SPACE * num_clocks)
190            + (4 + OtherState::INIT_SPACE * num_other)
191    }
192}
193
194#[cfg(feature = "utils")]
195impl MarketStateUpdated {
196    /// Get market token.
197    pub fn market_token(&self) -> Pubkey {
198        self.market_token
199    }
200
201    /// Get updated pools.
202    pub fn pools(&self) -> impl Iterator<Item = (PoolKind, &EventPool)> {
203        self.pool_kinds.iter().copied().zip(self.pools.iter())
204    }
205
206    /// Get updated clocks.
207    pub fn clocks(&self) -> Option<&EventClocks> {
208        self.clocks.first()
209    }
210
211    /// Get updated other state.
212    pub fn other(&self) -> Option<&EventOtherState> {
213        self.other.first()
214    }
215}
216
217/// This is a cheaper variant of [`MarketStateUpdated`] event, sharing the same format
218/// for serialization.
219#[derive(BorshSerialize)]
220pub(crate) struct MarketStateUpdatedRef<'a> {
221    /// Revision.
222    rev: u64,
223    /// Market token.
224    market_token: Pubkey,
225    /// Updated pool kinds.
226    pool_kinds: Vec<PoolKind>,
227    /// Updated pools.
228    pools: Vec<&'a Pool>,
229    /// Clocks.
230    clocks: Vec<&'a Clocks>,
231    /// Other states.
232    other: Vec<&'a OtherState>,
233}
234
235impl<'a> MarketStateUpdatedRef<'a> {
236    pub(crate) fn new(
237        rev: u64,
238        market_token: Pubkey,
239        pool_kinds: Vec<PoolKind>,
240        pools: Vec<&'a Pool>,
241        clocks: Option<&'a Clocks>,
242        other: Option<&'a OtherState>,
243    ) -> Self {
244        assert_eq!(pool_kinds.len(), pools.len());
245        Self {
246            rev,
247            market_token,
248            pool_kinds,
249            pools,
250            clocks: clocks.into_iter().collect(),
251            other: other.into_iter().collect(),
252        }
253    }
254
255    pub(crate) fn space(&self) -> usize {
256        MarketStateUpdated::space(self.pools.len(), self.clocks.len(), self.other.len())
257    }
258}
259
260impl anchor_lang::Discriminator for MarketStateUpdatedRef<'_> {
261    const DISCRIMINATOR: [u8; 8] = MarketStateUpdated::DISCRIMINATOR;
262}
263
264impl Event for MarketStateUpdatedRef<'_> {}