use anchor_lang::prelude::*;
use borsh::BorshSerialize;
use gmsol_model::action::{
decrease_position::{DecreasePositionReport, DecreasePositionSwapType},
increase_position::IncreasePositionReport,
};
use gmsol_utils::InitSpace;
use crate::states::{
common::action::{Action, ActionState},
order::OrderKind,
Order,
};
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 {}
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, InitSpace)]
pub struct InsufficientFundingFeePayment {
pub ts: i64,
pub slot: u64,
pub store: Pubkey,
pub market_token: Pubkey,
pub cost_amount: u128,
pub paid_in_collateral_amount: u128,
pub paid_in_secondary_output_amount: u128,
pub is_collateral_token_long: bool,
}
impl InsufficientFundingFeePayment {
pub(crate) fn new(
store: &Pubkey,
market_token: &Pubkey,
cost_amount: u128,
paid_in_collateral_amount: u128,
paid_in_secondary_output_amount: u128,
is_collateral_token_long: bool,
) -> Result<Self> {
let clock = Clock::get()?;
Ok(Self {
ts: clock.unix_timestamp,
slot: clock.slot,
store: *store,
market_token: *market_token,
cost_amount,
paid_in_collateral_amount,
paid_in_secondary_output_amount,
is_collateral_token_long,
})
}
}
impl InitSpace for InsufficientFundingFeePayment {
const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
}
impl Event for InsufficientFundingFeePayment {}
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(AnchorSerialize, AnchorDeserialize, Clone, InitSpace)]
pub struct OrderParamsForEvent {
pub kind: OrderKind,
pub is_long: bool,
pub decrease_position_swap_type: DecreasePositionSwapType,
pub position: Option<Pubkey>,
pub collateral_token: Pubkey,
pub initial_collateral_token: Option<Pubkey>,
pub initial_collateral_delta_amount: u64,
pub size_delta_value: u128,
pub min_output: u128,
pub trigger_price: u128,
pub acceptable_price: u128,
pub valid_from_ts: i64,
}
impl TryFrom<&Order> for OrderParamsForEvent {
type Error = Error;
fn try_from(order: &Order) -> std::result::Result<Self, Self::Error> {
let params = order.params();
Ok(Self {
kind: params.kind()?,
is_long: params.side()?.is_long(),
decrease_position_swap_type: params.decrease_position_swap_type()?,
position: params.position().copied(),
collateral_token: params.collateral_token,
initial_collateral_token: order.tokens.initial_collateral.token(),
initial_collateral_delta_amount: params.initial_collateral_delta_amount,
size_delta_value: params.size_delta_value,
min_output: params.min_output(),
trigger_price: params.trigger_price,
acceptable_price: params.acceptable_price,
valid_from_ts: params.valid_from_ts,
})
}
}
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone, InitSpace)]
pub struct OrderUpdated {
pub is_create: bool,
pub id: u64,
pub ts: i64,
pub slot: u64,
pub store: Pubkey,
pub order: Pubkey,
pub market_token: Pubkey,
pub owner: Pubkey,
pub params: OrderParamsForEvent,
}
impl OrderUpdated {
pub(crate) fn new(is_create: bool, address: &Pubkey, order: &Order) -> Result<Self> {
let clock = Clock::get()?;
Ok(Self {
is_create,
id: order.header().id(),
ts: clock.unix_timestamp,
slot: clock.slot,
store: *order.header().store(),
order: *address,
market_token: *order.market_token(),
owner: *order.header().owner(),
params: order.try_into()?,
})
}
}
impl InitSpace for OrderUpdated {
const INIT_SPACE: usize = <Self as Space>::INIT_SPACE;
}
impl Event for OrderUpdated {}