spl_token_swap/
error.rs

1//! Error types
2
3use num_derive::FromPrimitive;
4use solana_program::{
5    decode_error::DecodeError,
6    msg,
7    program_error::{PrintProgramError, ProgramError},
8};
9use thiserror::Error;
10
11/// Errors that may be returned by the TokenSwap program.
12#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
13pub enum SwapError {
14    // 0.
15    /// The account cannot be initialized because it is already being used.
16    #[error("Swap account already in use")]
17    AlreadyInUse,
18    /// The program address provided doesn't match the value generated by the program.
19    #[error("Invalid program address generated from bump seed and key")]
20    InvalidProgramAddress,
21    /// The owner of the input isn't set to the program address generated by the program.
22    #[error("Input account owner is not the program address")]
23    InvalidOwner,
24    /// The owner of the pool token output is set to the program address generated by the program.
25    #[error("Output pool account owner cannot be the program address")]
26    InvalidOutputOwner,
27    /// The deserialization of the account returned something besides State::Mint.
28    #[error("Deserialized account is not an SPL Token mint")]
29    ExpectedMint,
30
31    // 5.
32    /// The deserialization of the account returned something besides State::Account.
33    #[error("Deserialized account is not an SPL Token account")]
34    ExpectedAccount,
35    /// The input token account is empty.
36    #[error("Input token account empty")]
37    EmptySupply,
38    /// The pool token mint has a non-zero supply.
39    #[error("Pool token mint has a non-zero supply")]
40    InvalidSupply,
41    /// The provided token account has a delegate.
42    #[error("Token account has a delegate")]
43    InvalidDelegate,
44    /// The input token is invalid for swap.
45    #[error("InvalidInput")]
46    InvalidInput,
47
48    // 10.
49    /// Address of the provided swap token account is incorrect.
50    #[error("Address of the provided swap token account is incorrect")]
51    IncorrectSwapAccount,
52    /// Address of the provided pool token mint is incorrect
53    #[error("Address of the provided pool token mint is incorrect")]
54    IncorrectPoolMint,
55    /// The output token is invalid for swap.
56    #[error("InvalidOutput")]
57    InvalidOutput,
58    /// General calculation failure due to overflow or underflow
59    #[error("General calculation failure due to overflow or underflow")]
60    CalculationFailure,
61    /// Invalid instruction number passed in.
62    #[error("Invalid instruction")]
63    InvalidInstruction,
64
65    // 15.
66    /// Swap input token accounts have the same mint
67    #[error("Swap input token accounts have the same mint")]
68    RepeatedMint,
69    /// Swap instruction exceeds desired slippage limit
70    #[error("Swap instruction exceeds desired slippage limit")]
71    ExceededSlippage,
72    /// The provided token account has a close authority.
73    #[error("Token account has a close authority")]
74    InvalidCloseAuthority,
75    /// The pool token mint has a freeze authority.
76    #[error("Pool token mint has a freeze authority")]
77    InvalidFreezeAuthority,
78    /// The pool fee token account is incorrect
79    #[error("Pool fee token account incorrect")]
80    IncorrectFeeAccount,
81
82    // 20.
83    /// Given pool token amount results in zero trading tokens
84    #[error("Given pool token amount results in zero trading tokens")]
85    ZeroTradingTokens,
86    /// The fee calculation failed due to overflow, underflow, or unexpected 0
87    #[error("Fee calculation failed due to overflow, underflow, or unexpected 0")]
88    FeeCalculationFailure,
89    /// ConversionFailure
90    #[error("Conversion to u64 failed with an overflow or underflow")]
91    ConversionFailure,
92    /// The provided fee does not match the program owner's constraints
93    #[error("The provided fee does not match the program owner's constraints")]
94    InvalidFee,
95    /// The provided token program does not match the token program expected by the swap
96    #[error("The provided token program does not match the token program expected by the swap")]
97    IncorrectTokenProgramId,
98
99    // 25.
100    /// The provided curve type is not supported by the program owner
101    #[error("The provided curve type is not supported by the program owner")]
102    UnsupportedCurveType,
103    /// The provided curve parameters are invalid
104    #[error("The provided curve parameters are invalid")]
105    InvalidCurve,
106    /// The operation cannot be performed on the given curve
107    #[error("The operation cannot be performed on the given curve")]
108    UnsupportedCurveOperation,
109    /// The pool fee account is invalid.
110    #[error("The pool fee account is invalid")]
111    InvalidFeeAccount,
112}
113impl From<SwapError> for ProgramError {
114    fn from(e: SwapError) -> Self {
115        ProgramError::Custom(e as u32)
116    }
117}
118impl<T> DecodeError<T> for SwapError {
119    fn type_of() -> &'static str {
120        "Swap Error"
121    }
122}
123
124impl PrintProgramError for SwapError {
125    fn print<E>(&self)
126    where
127        E: 'static
128            + std::error::Error
129            + DecodeError<E>
130            + PrintProgramError
131            + num_traits::FromPrimitive,
132    {
133        match self {
134            SwapError::AlreadyInUse => msg!("Error: Swap account already in use"),
135            SwapError::InvalidProgramAddress => {
136                msg!("Error: Invalid program address generated from bump seed and key")
137            }
138            SwapError::InvalidOwner => {
139                msg!("Error: The input account owner is not the program address")
140            }
141            SwapError::InvalidOutputOwner => {
142                msg!("Error: Output pool account owner cannot be the program address")
143            }
144            SwapError::ExpectedMint => msg!("Error: Deserialized account is not an SPL Token mint"),
145            SwapError::ExpectedAccount => {
146                msg!("Error: Deserialized account is not an SPL Token account")
147            }
148            SwapError::EmptySupply => msg!("Error: Input token account empty"),
149            SwapError::InvalidSupply => msg!("Error: Pool token mint has a non-zero supply"),
150            SwapError::RepeatedMint => msg!("Error: Swap input token accounts have the same mint"),
151            SwapError::InvalidDelegate => msg!("Error: Token account has a delegate"),
152            SwapError::InvalidInput => msg!("Error: InvalidInput"),
153            SwapError::IncorrectSwapAccount => {
154                msg!("Error: Address of the provided swap token account is incorrect")
155            }
156            SwapError::IncorrectPoolMint => {
157                msg!("Error: Address of the provided pool token mint is incorrect")
158            }
159            SwapError::InvalidOutput => msg!("Error: InvalidOutput"),
160            SwapError::CalculationFailure => msg!("Error: CalculationFailure"),
161            SwapError::InvalidInstruction => msg!("Error: InvalidInstruction"),
162            SwapError::ExceededSlippage => {
163                msg!("Error: Swap instruction exceeds desired slippage limit")
164            }
165            SwapError::InvalidCloseAuthority => msg!("Error: Token account has a close authority"),
166            SwapError::InvalidFreezeAuthority => {
167                msg!("Error: Pool token mint has a freeze authority")
168            }
169            SwapError::IncorrectFeeAccount => msg!("Error: Pool fee token account incorrect"),
170            SwapError::ZeroTradingTokens => {
171                msg!("Error: Given pool token amount results in zero trading tokens")
172            }
173            SwapError::FeeCalculationFailure => msg!(
174                "Error: The fee calculation failed due to overflow, underflow, or unexpected 0"
175            ),
176            SwapError::ConversionFailure => msg!("Error: Conversion to or from u64 failed."),
177            SwapError::InvalidFee => {
178                msg!("Error: The provided fee does not match the program owner's constraints")
179            }
180            SwapError::IncorrectTokenProgramId => {
181                msg!("Error: The provided token program does not match the token program expected by the swap")
182            }
183            SwapError::UnsupportedCurveType => {
184                msg!("Error: The provided curve type is not supported by the program owner")
185            }
186            SwapError::InvalidCurve => {
187                msg!("Error: The provided curve parameters are invalid")
188            }
189            SwapError::UnsupportedCurveOperation => {
190                msg!("Error: The operation cannot be performed on the given curve")
191            }
192            SwapError::InvalidFeeAccount => {
193                msg!("Error: The pool fee account is invalid")
194            }
195        }
196    }
197}