gmsol_store/events/
order.rs

1use anchor_lang::prelude::*;
2use borsh::BorshSerialize;
3
4use gmsol_model::action::{
5    decrease_position::DecreasePositionReport, increase_position::IncreasePositionReport,
6};
7use gmsol_utils::InitSpace;
8
9use crate::states::{common::action::ActionState, order::OrderKind};
10
11use super::Event;
12
13/// Order created event.
14#[event]
15#[cfg_attr(feature = "debug", derive(Debug))]
16pub struct OrderCreated {
17    /// Event time.
18    pub ts: i64,
19    /// Store account.
20    pub store: Pubkey,
21    /// Order account.
22    pub order: Pubkey,
23    /// Position account.
24    pub position: Option<Pubkey>,
25}
26
27impl OrderCreated {
28    pub(crate) fn new(store: Pubkey, order: Pubkey, position: Option<Pubkey>) -> Result<Self> {
29        Ok(Self {
30            ts: Clock::get()?.unix_timestamp,
31            store,
32            order,
33            position,
34        })
35    }
36}
37
38/// Position increased event.
39#[event]
40#[cfg_attr(feature = "debug", derive(Debug))]
41pub struct PositionIncreased {
42    /// Revision.
43    pub rev: u64,
44    /// Market token.
45    pub market_token: Pubkey,
46    /// Report.
47    pub report: IncreasePositionReport<u128, i128>,
48}
49
50impl gmsol_utils::InitSpace for PositionIncreased {
51    const INIT_SPACE: usize = 8 + 32 + IncreasePositionReport::<u128, i128>::INIT_SPACE;
52}
53
54impl Event for PositionIncreased {}
55
56impl PositionIncreased {
57    pub(crate) fn from_report(
58        rev: u64,
59        market_token: Pubkey,
60        report: IncreasePositionReport<u128, i128>,
61    ) -> Self {
62        Self {
63            rev,
64            market_token,
65            report,
66        }
67    }
68}
69
70/// Position decrease event.
71#[event]
72#[cfg_attr(feature = "debug", derive(Debug))]
73pub struct PositionDecreased {
74    /// Revision.
75    pub rev: u64,
76    /// Market token.
77    pub market_token: Pubkey,
78    /// Report.
79    pub report: Box<DecreasePositionReport<u128, i128>>,
80}
81
82impl gmsol_utils::InitSpace for PositionDecreased {
83    const INIT_SPACE: usize = 8 + 32 + DecreasePositionReport::<u128, i128>::INIT_SPACE;
84}
85
86impl Event for PositionDecreased {}
87
88impl PositionDecreased {
89    pub(crate) fn from_report(
90        rev: u64,
91        market_token: Pubkey,
92        report: Box<DecreasePositionReport<u128, i128>>,
93    ) -> Self {
94        Self {
95            rev,
96            market_token,
97            report,
98        }
99    }
100}
101
102/// Order removed event.
103#[event]
104#[cfg_attr(feature = "debug", derive(Debug))]
105#[derive(Clone, InitSpace)]
106pub struct OrderRemoved {
107    /// Action id.
108    pub id: u64,
109    /// Timestamp.
110    pub ts: i64,
111    /// Slot.
112    pub slot: u64,
113    /// Store.
114    pub store: Pubkey,
115    /// Order.
116    pub order: Pubkey,
117    /// Kind.
118    pub kind: OrderKind,
119    /// Market token.
120    pub market_token: Pubkey,
121    /// Owner.
122    pub owner: Pubkey,
123    /// Final state.
124    pub state: ActionState,
125    /// Reason.
126    #[max_len(32)]
127    pub reason: String,
128}
129
130impl OrderRemoved {
131    pub(crate) fn new(
132        id: u64,
133        store: Pubkey,
134        order: Pubkey,
135        kind: OrderKind,
136        market_token: Pubkey,
137        owner: Pubkey,
138        state: ActionState,
139        reason: impl ToString,
140    ) -> Result<Self> {
141        let clock = Clock::get()?;
142        Ok(Self {
143            id,
144            ts: clock.unix_timestamp,
145            slot: clock.slot,
146            store,
147            kind,
148            order,
149            market_token,
150            owner,
151            state,
152            reason: reason.to_string(),
153        })
154    }
155}
156
157impl InitSpace for OrderRemoved {
158    const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
159}
160
161impl Event for OrderRemoved {}