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#[event]
21#[cfg_attr(feature = "debug", derive(Debug))]
22pub struct MarketFeesUpdated {
23 pub rev: u64,
25 pub market_token: Pubkey,
27 pub position_impact_distribution: DistributePositionImpactReport<u128>,
29 pub update_borrowing_state: UpdateBorrowingReport<u128>,
31 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 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#[event]
66#[cfg_attr(feature = "debug", derive(Debug))]
67pub struct BorrowingFeesUpdated {
68 pub rev: u64,
70 pub market_token: Pubkey,
72 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 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#[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 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 pub long_token_amount: u128,
111 pub short_token_amount: u128,
113}
114
115static_assertions::const_assert_eq!(EventPool::INIT_SPACE, Pool::INIT_SPACE);
116
117#[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 pub rev: u64,
126 pub price_impact_distribution: i64,
128 pub borrowing: i64,
130 pub funding: i64,
132 pub adl_for_long: i64,
134 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#[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 pub rev: u64,
151 pub trade_count: u64,
153 pub long_token_balance: u64,
155 pub short_token_balance: u64,
157 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#[event]
168#[cfg_attr(feature = "debug", derive(Debug))]
169pub struct MarketStateUpdated {
170 rev: u64,
172 market_token: Pubkey,
174 pool_kinds: Vec<PoolKind>,
176 pools: Vec<EventPool>,
178 clocks: Vec<EventClocks>,
180 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 pub fn market_token(&self) -> Pubkey {
198 self.market_token
199 }
200
201 pub fn pools(&self) -> impl Iterator<Item = (PoolKind, &EventPool)> {
203 self.pool_kinds.iter().copied().zip(self.pools.iter())
204 }
205
206 pub fn clocks(&self) -> Option<&EventClocks> {
208 self.clocks.first()
209 }
210
211 pub fn other(&self) -> Option<&EventOtherState> {
213 self.other.first()
214 }
215}
216
217#[derive(BorshSerialize)]
220pub(crate) struct MarketStateUpdatedRef<'a> {
221 rev: u64,
223 market_token: Pubkey,
225 pool_kinds: Vec<PoolKind>,
227 pools: Vec<&'a Pool>,
229 clocks: Vec<&'a Clocks>,
231 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<'_> {}