Skip to main content

gmsol_store/events/
order.rs

1use anchor_lang::prelude::*;
2use borsh::BorshSerialize;
3
4use gmsol_model::action::{
5    decrease_position::{DecreasePositionReport, DecreasePositionSwapType},
6    increase_position::IncreasePositionReport,
7};
8use gmsol_utils::InitSpace;
9
10use crate::states::{
11    common::action::{Action, ActionState},
12    order::OrderKind,
13    Order,
14};
15
16use super::Event;
17
18/// Order created event.
19#[event]
20#[cfg_attr(feature = "debug", derive(Debug))]
21pub struct OrderCreated {
22    /// Event time.
23    pub ts: i64,
24    /// Store account.
25    pub store: Pubkey,
26    /// Order account.
27    pub order: Pubkey,
28    /// Position account.
29    pub position: Option<Pubkey>,
30}
31
32impl OrderCreated {
33    pub(crate) fn new(store: Pubkey, order: Pubkey, position: Option<Pubkey>) -> Result<Self> {
34        Ok(Self {
35            ts: Clock::get()?.unix_timestamp,
36            store,
37            order,
38            position,
39        })
40    }
41}
42
43/// Position increased event.
44#[event]
45#[cfg_attr(feature = "debug", derive(Debug))]
46pub struct PositionIncreased {
47    /// Revision.
48    pub rev: u64,
49    /// Market token.
50    pub market_token: Pubkey,
51    /// Report.
52    pub report: IncreasePositionReport<u128, i128>,
53}
54
55impl gmsol_utils::InitSpace for PositionIncreased {
56    const INIT_SPACE: usize = 8 + 32 + IncreasePositionReport::<u128, i128>::INIT_SPACE;
57}
58
59impl Event for PositionIncreased {}
60
61impl PositionIncreased {
62    pub(crate) fn from_report(
63        rev: u64,
64        market_token: Pubkey,
65        report: IncreasePositionReport<u128, i128>,
66    ) -> Self {
67        Self {
68            rev,
69            market_token,
70            report,
71        }
72    }
73}
74
75/// Position decrease event.
76#[event]
77#[cfg_attr(feature = "debug", derive(Debug))]
78pub struct PositionDecreased {
79    /// Revision.
80    pub rev: u64,
81    /// Market token.
82    pub market_token: Pubkey,
83    /// Report.
84    pub report: Box<DecreasePositionReport<u128, i128>>,
85}
86
87impl gmsol_utils::InitSpace for PositionDecreased {
88    const INIT_SPACE: usize = 8 + 32 + DecreasePositionReport::<u128, i128>::INIT_SPACE;
89}
90
91impl Event for PositionDecreased {}
92
93impl PositionDecreased {
94    pub(crate) fn from_report(
95        rev: u64,
96        market_token: Pubkey,
97        report: Box<DecreasePositionReport<u128, i128>>,
98    ) -> Self {
99        Self {
100            rev,
101            market_token,
102            report,
103        }
104    }
105}
106
107/// Order removed event.
108#[event]
109#[cfg_attr(feature = "debug", derive(Debug))]
110#[derive(Clone, InitSpace)]
111pub struct OrderRemoved {
112    /// Action id.
113    pub id: u64,
114    /// Timestamp.
115    pub ts: i64,
116    /// Slot.
117    pub slot: u64,
118    /// Store.
119    pub store: Pubkey,
120    /// Order.
121    pub order: Pubkey,
122    /// Kind.
123    pub kind: OrderKind,
124    /// Market token.
125    pub market_token: Pubkey,
126    /// Owner.
127    pub owner: Pubkey,
128    /// Final state.
129    pub state: ActionState,
130    /// Reason.
131    #[max_len(32)]
132    pub reason: String,
133}
134
135impl OrderRemoved {
136    pub(crate) fn new(
137        id: u64,
138        store: Pubkey,
139        order: Pubkey,
140        kind: OrderKind,
141        market_token: Pubkey,
142        owner: Pubkey,
143        state: ActionState,
144        reason: impl ToString,
145    ) -> Result<Self> {
146        let clock = Clock::get()?;
147        Ok(Self {
148            id,
149            ts: clock.unix_timestamp,
150            slot: clock.slot,
151            store,
152            kind,
153            order,
154            market_token,
155            owner,
156            state,
157            reason: reason.to_string(),
158        })
159    }
160}
161
162impl InitSpace for OrderRemoved {
163    const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
164}
165
166impl Event for OrderRemoved {}
167
168/// An event indicating that insufficient funding fee payment has occurred.
169#[event]
170#[cfg_attr(feature = "debug", derive(Debug))]
171#[derive(Clone, InitSpace)]
172pub struct InsufficientFundingFeePayment {
173    /// Timestamp.
174    pub ts: i64,
175    /// Slot.
176    pub slot: u64,
177    /// Store.
178    pub store: Pubkey,
179    /// Market token.
180    pub market_token: Pubkey,
181    /// Funding fee amount to pay.
182    pub cost_amount: u128,
183    /// Paid collateral token amount.
184    pub paid_in_collateral_amount: u128,
185    /// Paid secondary token amount.
186    pub paid_in_secondary_output_amount: u128,
187    /// Whether the collateral token is long token.
188    pub is_collateral_token_long: bool,
189}
190
191impl InsufficientFundingFeePayment {
192    pub(crate) fn new(
193        store: &Pubkey,
194        market_token: &Pubkey,
195        cost_amount: u128,
196        paid_in_collateral_amount: u128,
197        paid_in_secondary_output_amount: u128,
198        is_collateral_token_long: bool,
199    ) -> Result<Self> {
200        let clock = Clock::get()?;
201        Ok(Self {
202            ts: clock.unix_timestamp,
203            slot: clock.slot,
204            store: *store,
205            market_token: *market_token,
206            cost_amount,
207            paid_in_collateral_amount,
208            paid_in_secondary_output_amount,
209            is_collateral_token_long,
210        })
211    }
212}
213
214impl InitSpace for InsufficientFundingFeePayment {
215    const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
216}
217
218impl Event for InsufficientFundingFeePayment {}
219
220/// Order parameters for event.
221#[cfg_attr(feature = "debug", derive(Debug))]
222#[derive(AnchorSerialize, AnchorDeserialize, Clone, InitSpace)]
223pub struct OrderParamsForEvent {
224    /// Order kind.
225    pub kind: OrderKind,
226    /// Order side.
227    pub is_long: bool,
228    /// Decrease position swap type.
229    pub decrease_position_swap_type: DecreasePositionSwapType,
230    /// Position address.
231    pub position: Option<Pubkey>,
232    /// Collateral token.
233    pub collateral_token: Pubkey,
234    /// Initial collateral token.
235    pub initial_collateral_token: Option<Pubkey>,
236    /// Initial collateral delta amount.
237    pub initial_collateral_delta_amount: u64,
238    /// Size delta value.
239    pub size_delta_value: u128,
240    /// Min output.
241    pub min_output: u128,
242    /// Trigger price (in unit price).
243    pub trigger_price: u128,
244    /// Acceptable price (in unit price).
245    pub acceptable_price: u128,
246    /// Valid from this timestamp.
247    pub valid_from_ts: i64,
248}
249
250impl TryFrom<&Order> for OrderParamsForEvent {
251    type Error = Error;
252
253    fn try_from(order: &Order) -> std::result::Result<Self, Self::Error> {
254        let params = order.params();
255        Ok(Self {
256            kind: params.kind()?,
257            is_long: params.side()?.is_long(),
258            decrease_position_swap_type: params.decrease_position_swap_type()?,
259            position: params.position().copied(),
260            collateral_token: params.collateral_token,
261            initial_collateral_token: order.tokens.initial_collateral.token(),
262            initial_collateral_delta_amount: params.initial_collateral_delta_amount,
263            size_delta_value: params.size_delta_value,
264            min_output: params.min_output(),
265            trigger_price: params.trigger_price,
266            acceptable_price: params.acceptable_price,
267            valid_from_ts: params.valid_from_ts,
268        })
269    }
270}
271
272/// An event indicating that an order is created or updated.
273///
274/// # Notes
275/// - For compatibility reasons, the [`OrderUpdated`] event is not emitted
276///   by the (deprecated) `create_order` and `update_order` instructions.
277///   As a result, there is no guarantee that every order will have
278///   corresponding [`OrderUpdated`] events.
279#[event]
280#[cfg_attr(feature = "debug", derive(Debug))]
281#[derive(Clone, InitSpace)]
282pub struct OrderUpdated {
283    /// Whether it is a create event.
284    pub is_create: bool,
285    /// Action id.
286    pub id: u64,
287    /// Timestamp.
288    pub ts: i64,
289    /// Slot.
290    pub slot: u64,
291    /// Store.
292    pub store: Pubkey,
293    /// Order.
294    pub order: Pubkey,
295    /// Market token.
296    pub market_token: Pubkey,
297    /// Owner.
298    pub owner: Pubkey,
299    /// Parameters.
300    pub params: OrderParamsForEvent,
301}
302
303impl OrderUpdated {
304    pub(crate) fn new(is_create: bool, address: &Pubkey, order: &Order) -> Result<Self> {
305        let clock = Clock::get()?;
306        Ok(Self {
307            is_create,
308            id: order.header().id(),
309            ts: clock.unix_timestamp,
310            slot: clock.slot,
311            store: *order.header().store(),
312            order: *address,
313            market_token: *order.market_token(),
314            owner: *order.header().owner(),
315            params: order.try_into()?,
316        })
317    }
318}
319
320impl InitSpace for OrderUpdated {
321    const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
322}
323
324impl Event for OrderUpdated {}