use anchor_lang::prelude::*;
use borsh::BorshSerialize;
use gmsol_model::action::{
decrease_position::DecreasePositionReport, increase_position::IncreasePositionReport,
};
use gmsol_utils::InitSpace;
use crate::states::{common::action::ActionState, order::OrderKind};
use super::Event;
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct OrderCreated {
pub ts: i64,
pub store: Pubkey,
pub order: Pubkey,
pub position: Option<Pubkey>,
}
impl OrderCreated {
pub(crate) fn new(store: Pubkey, order: Pubkey, position: Option<Pubkey>) -> Result<Self> {
Ok(Self {
ts: Clock::get()?.unix_timestamp,
store,
order,
position,
})
}
}
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct PositionIncreased {
pub rev: u64,
pub market_token: Pubkey,
pub report: IncreasePositionReport<u128, i128>,
}
impl gmsol_utils::InitSpace for PositionIncreased {
const INIT_SPACE: usize = 8 + 32 + IncreasePositionReport::<u128, i128>::INIT_SPACE;
}
impl Event for PositionIncreased {}
impl PositionIncreased {
pub(crate) fn from_report(
rev: u64,
market_token: Pubkey,
report: IncreasePositionReport<u128, i128>,
) -> Self {
Self {
rev,
market_token,
report,
}
}
}
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct PositionDecreased {
pub rev: u64,
pub market_token: Pubkey,
pub report: Box<DecreasePositionReport<u128, i128>>,
}
impl gmsol_utils::InitSpace for PositionDecreased {
const INIT_SPACE: usize = 8 + 32 + DecreasePositionReport::<u128, i128>::INIT_SPACE;
}
impl Event for PositionDecreased {}
impl PositionDecreased {
pub(crate) fn from_report(
rev: u64,
market_token: Pubkey,
report: Box<DecreasePositionReport<u128, i128>>,
) -> Self {
Self {
rev,
market_token,
report,
}
}
}
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, InitSpace)]
pub struct OrderRemoved {
pub id: u64,
pub ts: i64,
pub slot: u64,
pub store: Pubkey,
pub order: Pubkey,
pub kind: OrderKind,
pub market_token: Pubkey,
pub owner: Pubkey,
pub state: ActionState,
#[max_len(32)]
pub reason: String,
}
impl OrderRemoved {
pub(crate) fn new(
id: u64,
store: Pubkey,
order: Pubkey,
kind: OrderKind,
market_token: Pubkey,
owner: Pubkey,
state: ActionState,
reason: impl ToString,
) -> Result<Self> {
let clock = Clock::get()?;
Ok(Self {
id,
ts: clock.unix_timestamp,
slot: clock.slot,
store,
kind,
order,
market_token,
owner,
state,
reason: reason.to_string(),
})
}
}
impl InitSpace for OrderRemoved {
const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
}
impl Event for OrderRemoved {}