gpl_token_swap/
error.rs

1//! Error types
2
3use num_derive::FromPrimitive;
4use gemachain_program::{decode_error::DecodeError, program_error::ProgramError};
5use thiserror::Error;
6
7/// Errors that may be returned by the TokenSwap program.
8#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
9pub enum SwapError {
10    // 0.
11    /// The account cannot be initialized because it is already being used.
12    #[error("Swap account already in use")]
13    AlreadyInUse,
14    /// The program address provided doesn't match the value generated by the program.
15    #[error("Invalid program address generated from bump seed and key")]
16    InvalidProgramAddress,
17    /// The owner of the input isn't set to the program address generated by the program.
18    #[error("Input account owner is not the program address")]
19    InvalidOwner,
20    /// The owner of the pool token output is set to the program address generated by the program.
21    #[error("Output pool account owner cannot be the program address")]
22    InvalidOutputOwner,
23    /// The deserialization of the account returned something besides State::Mint.
24    #[error("Deserialized account is not an GPL Token mint")]
25    ExpectedMint,
26
27    // 5.
28    /// The deserialization of the account returned something besides State::Account.
29    #[error("Deserialized account is not an GPL Token account")]
30    ExpectedAccount,
31    /// The input token account is empty.
32    #[error("Input token account empty")]
33    EmptySupply,
34    /// The pool token mint has a non-zero supply.
35    #[error("Pool token mint has a non-zero supply")]
36    InvalidSupply,
37    /// The provided token account has a delegate.
38    #[error("Token account has a delegate")]
39    InvalidDelegate,
40    /// The input token is invalid for swap.
41    #[error("InvalidInput")]
42    InvalidInput,
43
44    // 10.
45    /// Address of the provided swap token account is incorrect.
46    #[error("Address of the provided swap token account is incorrect")]
47    IncorrectSwapAccount,
48    /// Address of the provided pool token mint is incorrect
49    #[error("Address of the provided pool token mint is incorrect")]
50    IncorrectPoolMint,
51    /// The output token is invalid for swap.
52    #[error("InvalidOutput")]
53    InvalidOutput,
54    /// General calculation failure due to overflow or underflow
55    #[error("General calculation failure due to overflow or underflow")]
56    CalculationFailure,
57    /// Invalid instruction number passed in.
58    #[error("Invalid instruction")]
59    InvalidInstruction,
60
61    // 15.
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    /// The pool fee token account is incorrect
75    #[error("Pool fee token account incorrect")]
76    IncorrectFeeAccount,
77
78    // 20.
79    /// Given pool token amount results in zero trading tokens
80    #[error("Given pool token amount results in zero trading tokens")]
81    ZeroTradingTokens,
82    /// The fee calculation failed due to overflow, underflow, or unexpected 0
83    #[error("Fee calculation failed due to overflow, underflow, or unexpected 0")]
84    FeeCalculationFailure,
85    /// ConversionFailure
86    #[error("Conversion to u64 failed with an overflow or underflow")]
87    ConversionFailure,
88    /// The provided fee does not match the program owner's constraints
89    #[error("The provided fee does not match the program owner's constraints")]
90    InvalidFee,
91    /// The provided token program does not match the token program expected by the swap
92    #[error("The provided token program does not match the token program expected by the swap")]
93    IncorrectTokenProgramId,
94
95    // 25.
96    /// The provided curve type is not supported by the program owner
97    #[error("The provided curve type is not supported by the program owner")]
98    UnsupportedCurveType,
99    /// The provided curve parameters are invalid
100    #[error("The provided curve parameters are invalid")]
101    InvalidCurve,
102    /// The operation cannot be performed on the given curve
103    #[error("The operation cannot be performed on the given curve")]
104    UnsupportedCurveOperation,
105}
106impl From<SwapError> for ProgramError {
107    fn from(e: SwapError) -> Self {
108        ProgramError::Custom(e as u32)
109    }
110}
111impl<T> DecodeError<T> for SwapError {
112    fn type_of() -> &'static str {
113        "Swap Error"
114    }
115}