1use num_derive::FromPrimitive;
4use solana_program::{
5 decode_error::DecodeError,
6 msg,
7 program_error::{PrintProgramError, ProgramError},
8};
9use thiserror::Error;
10
11#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
13pub enum SwapError {
14 #[error("Swap account already in use")]
17 AlreadyInUse,
18 #[error("Invalid program address generated from bump seed and key")]
20 InvalidProgramAddress,
21 #[error("Input account owner is not the program address")]
23 InvalidOwner,
24 #[error("Output pool account owner cannot be the program address")]
26 InvalidOutputOwner,
27 #[error("Deserialized account is not an SPL Token mint")]
29 ExpectedMint,
30
31 #[error("Deserialized account is not an SPL Token account")]
34 ExpectedAccount,
35 #[error("Input token account empty")]
37 EmptySupply,
38 #[error("Pool token mint has a non-zero supply")]
40 InvalidSupply,
41 #[error("Token account has a delegate")]
43 InvalidDelegate,
44 #[error("InvalidInput")]
46 InvalidInput,
47
48 #[error("Address of the provided swap token account is incorrect")]
51 IncorrectSwapAccount,
52 #[error("Address of the provided pool token mint is incorrect")]
54 IncorrectPoolMint,
55 #[error("InvalidOutput")]
57 InvalidOutput,
58 #[error("General calculation failure due to overflow or underflow")]
60 CalculationFailure,
61 #[error("Invalid instruction")]
63 InvalidInstruction,
64
65 #[error("Swap input token accounts have the same mint")]
68 RepeatedMint,
69 #[error("Swap instruction exceeds desired slippage limit")]
71 ExceededSlippage,
72 #[error("Token account has a close authority")]
74 InvalidCloseAuthority,
75 #[error("Pool token mint has a freeze authority")]
77 InvalidFreezeAuthority,
78 #[error("Pool fee token account incorrect")]
80 IncorrectFeeAccount,
81
82 #[error("Given pool token amount results in zero trading tokens")]
85 ZeroTradingTokens,
86 #[error("Fee calculation failed due to overflow, underflow, or unexpected 0")]
88 FeeCalculationFailure,
89 #[error("Conversion to u64 failed with an overflow or underflow")]
91 ConversionFailure,
92 #[error("The provided fee does not match the program owner's constraints")]
94 InvalidFee,
95 #[error("The provided token program does not match the token program expected by the swap")]
97 IncorrectTokenProgramId,
98
99 #[error("The provided curve type is not supported by the program owner")]
102 UnsupportedCurveType,
103 #[error("The provided curve parameters are invalid")]
105 InvalidCurve,
106 #[error("The operation cannot be performed on the given curve")]
108 UnsupportedCurveOperation,
109 #[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}