gmsol_store/events/
mod.rs

1/// Deposit events.
2mod deposit;
3
4/// Withdrawal events.
5mod withdrawal;
6
7/// Shift events.
8mod shift;
9
10/// GLV events.
11mod glv;
12
13/// Swap events.
14mod swap;
15
16/// Order events.
17mod order;
18
19/// Trade events.
20mod trade;
21
22/// Market events.
23mod market;
24
25/// GT events.
26mod gt;
27
28pub use deposit::*;
29pub use glv::*;
30pub use gt::*;
31pub use market::*;
32pub use order::*;
33pub use shift::*;
34pub use swap::*;
35pub use trade::*;
36pub use withdrawal::*;
37
38use anchor_lang::prelude::*;
39
40/// Event Emitter.
41#[derive(Clone, Copy)]
42pub(crate) struct EventEmitter<'a, 'info> {
43    event_authority: &'a AccountInfo<'info>,
44    bump: u8,
45}
46
47impl<'a, 'info> EventEmitter<'a, 'info> {
48    /// Create an event emitter from event authority and bump.
49    pub fn new(event_authority: &'a AccountInfo<'info>, bump: u8) -> Self {
50        Self {
51            event_authority,
52            bump,
53        }
54    }
55}
56
57impl<'a, 'info> From<(&'a AccountInfo<'info>, u8)> for EventEmitter<'a, 'info> {
58    fn from((event_authority, bump): (&'a AccountInfo<'info>, u8)) -> Self {
59        Self::new(event_authority, bump)
60    }
61}
62
63impl EventEmitter<'_, '_> {
64    /// Emit event through CPI with the given space.
65    pub fn emit_cpi_with_space<E>(&self, event: &E, space: usize) -> Result<()>
66    where
67        E: Event,
68    {
69        event.emit_cpi_with_space(self.event_authority.clone(), self.bump, space)
70    }
71
72    /// Emit event through CPI.
73    pub fn emit_cpi<E>(&self, event: &E) -> Result<()>
74    where
75        E: gmsol_utils::InitSpace + Event,
76    {
77        self.emit_cpi_with_space(event, E::INIT_SPACE)
78    }
79}
80
81/// Event.
82pub trait Event: borsh::BorshSerialize + anchor_lang::Discriminator {
83    /// Emit this event through CPI. This is a manual implementation of `emit_cpi!`.
84    fn emit_cpi_with_space(
85        &self,
86        event_authority: AccountInfo,
87        event_authority_bump: u8,
88        space: usize,
89    ) -> Result<()> {
90        use anchor_lang::solana_program::instruction::Instruction;
91
92        let disc = anchor_lang::event::EVENT_IX_TAG_LE;
93        let mut ix_data = Vec::with_capacity(16 + space);
94        ix_data.extend_from_slice(&disc);
95        ix_data.extend_from_slice(&Self::DISCRIMINATOR);
96        self.serialize(&mut ix_data)?;
97        let ix = Instruction {
98            program_id: crate::ID,
99            accounts: vec![AccountMeta::new_readonly(*event_authority.key, true)],
100            data: ix_data,
101        };
102        anchor_lang::solana_program::program::invoke_signed(
103            &ix,
104            &[event_authority],
105            &[&[
106                crate::constants::EVENT_AUTHORITY_SEED,
107                &[event_authority_bump],
108            ]],
109        )?;
110        Ok(())
111    }
112}