gmsol_store/events/
mod.rs1mod deposit;
3
4mod withdrawal;
6
7mod shift;
9
10mod glv;
12
13mod swap;
15
16mod order;
18
19mod trade;
21
22mod market;
24
25mod 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#[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 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 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 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
81pub trait Event: borsh::BorshSerialize + anchor_lang::Discriminator {
83 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}