use anchor_lang::prelude::*;
use borsh::BorshSerialize;
use gmsol_model::{
action::{
distribute_position_impact::DistributePositionImpactReport,
update_borrowing_state::UpdateBorrowingReport, update_funding_state::UpdateFundingReport,
},
PoolKind,
};
use crate::states::{
market::{pool::Pool, Clocks},
OtherState,
};
use super::Event;
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct MarketFeesUpdated {
pub rev: u64,
pub market_token: Pubkey,
pub position_impact_distribution: DistributePositionImpactReport<u128>,
pub update_borrowing_state: UpdateBorrowingReport<u128>,
pub update_funding_state: UpdateFundingReport<u128, i128>,
}
impl gmsol_utils::InitSpace for MarketFeesUpdated {
const INIT_SPACE: usize = 8
+ 32
+ DistributePositionImpactReport::<u128>::INIT_SPACE
+ UpdateBorrowingReport::<u128>::INIT_SPACE
+ UpdateFundingReport::<u128, i128>::INIT_SPACE;
}
impl Event for MarketFeesUpdated {}
impl MarketFeesUpdated {
pub fn from_reports(
rev: u64,
market_token: Pubkey,
position_impact_distribution: DistributePositionImpactReport<u128>,
update_borrowing_state: UpdateBorrowingReport<u128>,
update_funding_state: UpdateFundingReport<u128, i128>,
) -> Self {
Self {
rev,
market_token,
position_impact_distribution,
update_borrowing_state,
update_funding_state,
}
}
}
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct BorrowingFeesUpdated {
pub rev: u64,
pub market_token: Pubkey,
pub update_borrowing_state: UpdateBorrowingReport<u128>,
}
impl gmsol_utils::InitSpace for BorrowingFeesUpdated {
const INIT_SPACE: usize = 8 + 32 + UpdateBorrowingReport::<u128>::INIT_SPACE;
}
impl Event for BorrowingFeesUpdated {}
impl BorrowingFeesUpdated {
pub fn from_report(
rev: u64,
market_token: Pubkey,
update_borrowing_state: UpdateBorrowingReport<u128>,
) -> Self {
Self {
rev,
market_token,
update_borrowing_state,
}
}
}
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(AnchorSerialize, AnchorDeserialize, InitSpace)]
pub struct EventPool {
pub is_pure: u8,
#[cfg_attr(feature = "serde", serde(skip))]
#[cfg_attr(feature = "debug", debug(skip))]
pub(crate) padding: [u8; 15],
pub long_token_amount: u128,
pub short_token_amount: u128,
}
static_assertions::const_assert_eq!(EventPool::INIT_SPACE, Pool::INIT_SPACE);
#[derive(AnchorSerialize, AnchorDeserialize, InitSpace)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EventClocks {
#[cfg_attr(feature = "debug", debug(skip))]
pub(crate) padding: [u8; 8],
pub rev: u64,
pub price_impact_distribution: i64,
pub borrowing: i64,
pub funding: i64,
pub adl_for_long: i64,
pub adl_for_short: i64,
#[cfg_attr(feature = "debug", debug(skip))]
pub(crate) reserved: [i64; 3],
}
static_assertions::const_assert_eq!(EventClocks::INIT_SPACE, Clocks::INIT_SPACE);
#[derive(AnchorSerialize, AnchorDeserialize, InitSpace)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EventOtherState {
#[cfg_attr(feature = "debug", debug(skip))]
pub(crate) padding: [u8; 16],
pub rev: u64,
pub trade_count: u64,
pub long_token_balance: u64,
pub short_token_balance: u64,
pub funding_factor_per_second: i128,
#[cfg_attr(feature = "debug", debug(skip))]
#[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
pub(crate) reserved: [u8; 256],
}
static_assertions::const_assert_eq!(EventOtherState::INIT_SPACE, OtherState::INIT_SPACE);
#[event]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct MarketStateUpdated {
rev: u64,
market_token: Pubkey,
pool_kinds: Vec<PoolKind>,
pools: Vec<EventPool>,
clocks: Vec<EventClocks>,
other: Vec<EventOtherState>,
}
impl MarketStateUpdated {
const fn space(num_pools: usize, num_clocks: usize, num_other: usize) -> usize {
8 + 32
+ (4 + PoolKind::INIT_SPACE * num_pools)
+ (4 + Pool::INIT_SPACE * num_pools)
+ (4 + Clocks::INIT_SPACE * num_clocks)
+ (4 + OtherState::INIT_SPACE * num_other)
}
}
#[cfg(feature = "utils")]
impl MarketStateUpdated {
pub fn market_token(&self) -> Pubkey {
self.market_token
}
pub fn pools(&self) -> impl Iterator<Item = (PoolKind, &EventPool)> {
self.pool_kinds.iter().copied().zip(self.pools.iter())
}
pub fn clocks(&self) -> Option<&EventClocks> {
self.clocks.first()
}
pub fn other(&self) -> Option<&EventOtherState> {
self.other.first()
}
}
#[derive(BorshSerialize)]
pub(crate) struct MarketStateUpdatedRef<'a> {
rev: u64,
market_token: Pubkey,
pool_kinds: Vec<PoolKind>,
pools: Vec<&'a Pool>,
clocks: Vec<&'a Clocks>,
other: Vec<&'a OtherState>,
}
impl<'a> MarketStateUpdatedRef<'a> {
pub(crate) fn new(
rev: u64,
market_token: Pubkey,
pool_kinds: Vec<PoolKind>,
pools: Vec<&'a Pool>,
clocks: Option<&'a Clocks>,
other: Option<&'a OtherState>,
) -> Self {
assert_eq!(pool_kinds.len(), pools.len());
Self {
rev,
market_token,
pool_kinds,
pools,
clocks: clocks.into_iter().collect(),
other: other.into_iter().collect(),
}
}
pub(crate) fn space(&self) -> usize {
MarketStateUpdated::space(self.pools.len(), self.clocks.len(), self.other.len())
}
}
impl anchor_lang::Discriminator for MarketStateUpdatedRef<'_> {
const DISCRIMINATOR: [u8; 8] = MarketStateUpdated::DISCRIMINATOR;
}
impl Event for MarketStateUpdatedRef<'_> {}