stable_swap_client/
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 StableSwap program.
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Error, FromPrimitive)]
13pub enum SwapError {
14    /// The account cannot be initialized because it is already being used.
15    #[error("Swap account already in use")]
16    AlreadyInUse,
17    /// The address of the admin fee account is incorrect.
18    #[error("Address of the admin fee account is incorrect")]
19    InvalidAdmin,
20    /// The owner of the input isn't set to the program address generated by the program.
21    #[error("Input account owner is not the program address")]
22    InvalidOwner,
23    /// The owner of the pool token output is set to the program address generated by the program.
24    #[error("Output pool account owner cannot be the program address")]
25    InvalidOutputOwner,
26    /// The program address provided doesn't match the value generated by the program.
27    #[error("Invalid program address generated from nonce and key")]
28    InvalidProgramAddress,
29    /// The deserialization of the account returned something besides State::Mint.
30    #[error("Deserialized account is not an SPL Token mint")]
31    ExpectedMint,
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 pool supply is empty.
36    #[error("Pool token supply is 0")]
37    EmptyPool,
38    /// The input token account is empty.
39    #[error("Input token account empty")]
40    EmptySupply,
41    /// The pool token mint has a non-zero supply.
42    #[error("Pool token mint has a non-zero supply")]
43    InvalidSupply,
44    /// The provided token account has a delegate.
45    #[error("Token account has a delegate")]
46    InvalidDelegate,
47    /// The input token is invalid for swap.
48    #[error("InvalidInput")]
49    InvalidInput,
50    /// Address of the provided swap token account is incorrect.
51    #[error("Address of the provided swap token account is incorrect")]
52    IncorrectSwapAccount,
53    /// Address of the provided token mint is incorrect
54    #[error("Address of the provided token mint is incorrect")]
55    IncorrectMint,
56    /// The calculation failed.
57    #[error("CalculationFailure")]
58    CalculationFailure,
59    /// Invalid instruction number passed in.
60    #[error("Invalid instruction")]
61    InvalidInstruction,
62    /// Swap input token accounts have the same mint
63    #[error("Swap input token accounts have the same mint")]
64    RepeatedMint,
65    /// Swap instruction exceeds desired slippage limit
66    #[error("Swap instruction exceeds desired slippage limit")]
67    ExceededSlippage,
68    /// The provided token account has a close authority.
69    #[error("Token account has a close authority")]
70    InvalidCloseAuthority,
71    /// The pool token mint has a freeze authority.
72    #[error("Pool token mint has a freeze authority")]
73    InvalidFreezeAuthority,
74    /// ConversionFailure
75    #[error("Conversion to u64 failed with an overflow or underflow")]
76    ConversionFailure,
77    /// Unauthorized
78    #[error("Account is not authorized to execute this instruction")]
79    Unauthorized,
80    /// Swap pool is paused
81    #[error("Swap pool is paused")]
82    IsPaused,
83    /// Amp. coefficient change is within min ramp duration
84    #[error("Ramp is locked in this time period")]
85    RampLocked,
86    /// Insufficient ramp time for the ramp operation
87    #[error("Insufficient ramp time")]
88    InsufficientRampTime,
89    /// Active admin transfer in progress
90    #[error("Active admin transfer in progress")]
91    ActiveTransfer,
92    /// No active admin transfer in progress
93    #[error("No active admin transfer in progress")]
94    NoActiveTransfer,
95    /// Admin transfer deadline exceeded
96    #[error("Admin transfer deadline exceeded")]
97    AdminDeadlineExceeded,
98    /// Token mint decimals must be the same.
99    #[error("Token mints must have same decimals")]
100    MismatchedDecimals,
101}
102
103impl From<SwapError> for ProgramError {
104    fn from(e: SwapError) -> Self {
105        ProgramError::Custom(e as u32)
106    }
107}
108
109impl<T> DecodeError<T> for SwapError {
110    fn type_of() -> &'static str {
111        "Swap Error"
112    }
113}
114
115impl PrintProgramError for SwapError {
116    fn print<E>(&self)
117    where
118        E: 'static
119            + std::error::Error
120            + DecodeError<E>
121            + PrintProgramError
122            + num_traits::FromPrimitive,
123    {
124        match self {
125            SwapError::AlreadyInUse => msg!("Error: Swap account already in use"),
126            SwapError::InvalidAdmin => {
127                msg!("Error: Address of the admin fee account is incorrect")
128            }
129            SwapError::InvalidOwner => {
130                msg!("Error: The input account owner is not the program address")
131            }
132            SwapError::InvalidOutputOwner => {
133                msg!("Error: Output pool account owner cannot be the program address")
134            }
135            SwapError::InvalidProgramAddress => {
136                msg!("Error: Invalid program address generated from nonce and key")
137            }
138            SwapError::ExpectedMint => {
139                msg!("Error: Deserialized account is not an SPL Token mint")
140            }
141            SwapError::ExpectedAccount => {
142                msg!("Error: Deserialized account is not an SPL Token account")
143            }
144            SwapError::EmptySupply => msg!("Error: Input token account empty"),
145            SwapError::EmptyPool => msg!("Error: Pool token supply is 0"),
146            SwapError::InvalidSupply => msg!("Error: Pool token mint has a non-zero supply"),
147            SwapError::RepeatedMint => msg!("Error: Swap input token accounts have the same mint"),
148            SwapError::InvalidDelegate => msg!("Error: Token account has a delegate"),
149            SwapError::InvalidInput => msg!("Error: InvalidInput"),
150            SwapError::IncorrectSwapAccount => {
151                msg!("Error: Address of the provided swap token account is incorrect")
152            }
153            SwapError::IncorrectMint => {
154                msg!("Error: Address of the provided token mint is incorrect")
155            }
156            SwapError::CalculationFailure => msg!("Error: CalculationFailure"),
157            SwapError::InvalidInstruction => msg!("Error: InvalidInstruction"),
158            SwapError::ExceededSlippage => {
159                msg!("Error: Swap instruction exceeds desired slippage limit")
160            }
161            SwapError::InvalidCloseAuthority => msg!("Error: Token account has a close authority"),
162            SwapError::InvalidFreezeAuthority => {
163                msg!("Error: Pool token mint has a freeze authority")
164            }
165            SwapError::ConversionFailure => msg!("Error: Conversion to or from u64 failed"),
166            SwapError::Unauthorized => {
167                msg!("Error: Account is not authorized to execute this instruction")
168            }
169            SwapError::IsPaused => msg!("Error: Swap pool is paused"),
170            SwapError::RampLocked => msg!("Error: Ramp is locked in this time period"),
171            SwapError::InsufficientRampTime => msg!("Error: Insufficient ramp time"),
172            SwapError::ActiveTransfer => msg!("Error: Active admin transfer in progress"),
173            SwapError::NoActiveTransfer => msg!("Error: No active admin transfer in progress"),
174            SwapError::AdminDeadlineExceeded => msg!("Error: Admin transfer deadline exceeded"),
175            SwapError::MismatchedDecimals => msg!("Error: Token mints must have same decimals"),
176        }
177    }
178}