1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use num_enum::IntoPrimitive;
use solana_program::{entrypoint::ProgramResult, msg, program_error::ProgramError};
use thiserror::Error;

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum PhoenixError {
    #[error("Invalid market parameters error")]
    InvalidMarketParameters = 0,
    #[error("Invalid market authority error")]
    InvalidMarketAuthority = 1,
    #[error("Market deserialization error")]
    FailedToLoadMarketFromAccount = 2,
    #[error("Market already initialized error")]
    MarketAlreadyInitialized = 3,
    #[error("Market is not initialized error")]
    MarketUninitialized = 4,
    #[error("Invalid state transition error")]
    InvalidStateTransition = 5,
    #[error("Invalid market signer error")]
    InvalidMarketSigner = 6,
    #[error("Invalid lot size error")]
    InvalidLotSize = 7,
    #[error("Invalid tick size error")]
    InvalidTickSize = 8,
    #[error("Invalid mint error")]
    InvalidMint = 9,
    #[error("Invalid base vault error")]
    InvalidBaseVault = 10,
    #[error("Invalid quote vault error")]
    InvalidQuoteVault = 11,
    #[error("Invalid base account error")]
    InvalidBaseAccount = 12,
    #[error("Invalid quote account error")]
    InvalidQuoteAccount = 13,
    #[error("Too many events error")]
    TooManyEvents = 14,
    #[error("New order error")]
    NewOrderError = 15,
    #[error("Reduce order error")]
    ReduceOrderError = 16,
    #[error("Cancel multiple orders error")]
    CancelMultipleOrdersError = 17,
    #[error("Withdraw funds error")]
    WithdrawFundsError = 18,
    #[error("Remove empty orders error")]
    RemoveEmptyOrdersError = 19,
    #[error("Trader not found error")]
    TraderNotFound = 20,
    #[error("Invalid seat status")]
    InvalidSeatStatus = 21,
    #[error("Failed to evict trader")]
    EvictionError = 22,
    #[error("Non empty scratch buffer")]
    NonEmptyScratchBuffer = 23,
    #[error("Failed to serialize event")]
    FailedToSerializeEvent = 24,
    #[error("Failed to flush buffer")]
    FailedToFlushBuffer = 25,
}

impl From<PhoenixError> for ProgramError {
    fn from(e: PhoenixError) -> Self {
        ProgramError::Custom(e as u32)
    }
}

#[track_caller]
#[inline(always)]
pub fn assert_with_msg(v: bool, err: impl Into<ProgramError>, msg: &str) -> ProgramResult {
    if v {
        Ok(())
    } else {
        let caller = std::panic::Location::caller();
        msg!("{}. \n{}", msg, caller);
        Err(err.into())
    }
}