use num_derive::FromPrimitive;
use gemachain_program::{decode_error::DecodeError, program_error::ProgramError};
use thiserror::Error;
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
pub enum SwapError {
#[error("Swap account already in use")]
AlreadyInUse,
#[error("Invalid program address generated from bump seed and key")]
InvalidProgramAddress,
#[error("Input account owner is not the program address")]
InvalidOwner,
#[error("Output pool account owner cannot be the program address")]
InvalidOutputOwner,
#[error("Deserialized account is not an GPL Token mint")]
ExpectedMint,
#[error("Deserialized account is not an GPL Token account")]
ExpectedAccount,
#[error("Input token account empty")]
EmptySupply,
#[error("Pool token mint has a non-zero supply")]
InvalidSupply,
#[error("Token account has a delegate")]
InvalidDelegate,
#[error("InvalidInput")]
InvalidInput,
#[error("Address of the provided swap token account is incorrect")]
IncorrectSwapAccount,
#[error("Address of the provided pool token mint is incorrect")]
IncorrectPoolMint,
#[error("InvalidOutput")]
InvalidOutput,
#[error("General calculation failure due to overflow or underflow")]
CalculationFailure,
#[error("Invalid instruction")]
InvalidInstruction,
#[error("Swap input token accounts have the same mint")]
RepeatedMint,
#[error("Swap instruction exceeds desired slippage limit")]
ExceededSlippage,
#[error("Token account has a close authority")]
InvalidCloseAuthority,
#[error("Pool token mint has a freeze authority")]
InvalidFreezeAuthority,
#[error("Pool fee token account incorrect")]
IncorrectFeeAccount,
#[error("Given pool token amount results in zero trading tokens")]
ZeroTradingTokens,
#[error("Fee calculation failed due to overflow, underflow, or unexpected 0")]
FeeCalculationFailure,
#[error("Conversion to u64 failed with an overflow or underflow")]
ConversionFailure,
#[error("The provided fee does not match the program owner's constraints")]
InvalidFee,
#[error("The provided token program does not match the token program expected by the swap")]
IncorrectTokenProgramId,
#[error("The provided curve type is not supported by the program owner")]
UnsupportedCurveType,
#[error("The provided curve parameters are invalid")]
InvalidCurve,
#[error("The operation cannot be performed on the given curve")]
UnsupportedCurveOperation,
}
impl From<SwapError> for ProgramError {
fn from(e: SwapError) -> Self {
ProgramError::Custom(e as u32)
}
}
impl<T> DecodeError<T> for SwapError {
fn type_of() -> &'static str {
"Swap Error"
}
}