phoenix/program/
error.rs

1use num_enum::IntoPrimitive;
2use solana_program::{entrypoint::ProgramResult, msg, program_error::ProgramError};
3use thiserror::Error;
4
5#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
6#[repr(u32)]
7pub enum PhoenixError {
8    #[error("Invalid market parameters error")]
9    InvalidMarketParameters = 0,
10    #[error("Invalid market authority error")]
11    InvalidMarketAuthority = 1,
12    #[error("Market deserialization error")]
13    FailedToLoadMarketFromAccount = 2,
14    #[error("Market already initialized error")]
15    MarketAlreadyInitialized = 3,
16    #[error("Market is not initialized error")]
17    MarketUninitialized = 4,
18    #[error("Invalid state transition error")]
19    InvalidStateTransition = 5,
20    #[error("Invalid market signer error")]
21    InvalidMarketSigner = 6,
22    #[error("Invalid lot size error")]
23    InvalidLotSize = 7,
24    #[error("Invalid tick size error")]
25    InvalidTickSize = 8,
26    #[error("Invalid mint error")]
27    InvalidMint = 9,
28    #[error("Invalid base vault error")]
29    InvalidBaseVault = 10,
30    #[error("Invalid quote vault error")]
31    InvalidQuoteVault = 11,
32    #[error("Invalid base account error")]
33    InvalidBaseAccount = 12,
34    #[error("Invalid quote account error")]
35    InvalidQuoteAccount = 13,
36    #[error("Too many events error")]
37    TooManyEvents = 14,
38    #[error("New order error")]
39    NewOrderError = 15,
40    #[error("Reduce order error")]
41    ReduceOrderError = 16,
42    #[error("Cancel multiple orders error")]
43    CancelMultipleOrdersError = 17,
44    #[error("Withdraw funds error")]
45    WithdrawFundsError = 18,
46    #[error("Remove empty orders error")]
47    RemoveEmptyOrdersError = 19,
48    #[error("Trader not found error")]
49    TraderNotFound = 20,
50    #[error("Invalid seat status")]
51    InvalidSeatStatus = 21,
52    #[error("Failed to evict trader")]
53    EvictionError = 22,
54    #[error("Non empty scratch buffer")]
55    NonEmptyScratchBuffer = 23,
56    #[error("Failed to serialize event")]
57    FailedToSerializeEvent = 24,
58    #[error("Failed to flush buffer")]
59    FailedToFlushBuffer = 25,
60}
61
62impl From<PhoenixError> for ProgramError {
63    fn from(e: PhoenixError) -> Self {
64        ProgramError::Custom(e as u32)
65    }
66}
67
68#[track_caller]
69#[inline(always)]
70pub fn assert_with_msg(v: bool, err: impl Into<ProgramError>, msg: &str) -> ProgramResult {
71    if v {
72        Ok(())
73    } else {
74        let caller = std::panic::Location::caller();
75        msg!("{}. \n{}", msg, caller);
76        Err(err.into())
77    }
78}