use {
num_derive::FromPrimitive,
solana_program::{
decode_error::DecodeError,
msg,
program_error::{PrintProgramError, ProgramError},
},
thiserror::Error,
};
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
pub enum TokenError {
#[error("Lamport balance below rent-exempt threshold")]
NotRentExempt,
#[error("Insufficient funds")]
InsufficientFunds,
#[error("Invalid Mint")]
InvalidMint,
#[error("Account not associated with this Mint")]
MintMismatch,
#[error("Owner does not match")]
OwnerMismatch,
#[error("Fixed supply")]
FixedSupply,
#[error("Already in use")]
AlreadyInUse,
#[error("Invalid number of provided signers")]
InvalidNumberOfProvidedSigners,
#[error("Invalid number of required signers")]
InvalidNumberOfRequiredSigners,
#[error("State is uninitialized")]
UninitializedState,
#[error("Instruction does not support native tokens")]
NativeNotSupported,
#[error("Non-native account can only be closed if its balance is zero")]
NonNativeHasBalance,
#[error("Invalid instruction")]
InvalidInstruction,
#[error("State is invalid for requested operation")]
InvalidState,
#[error("Operation overflowed")]
Overflow,
#[error("Account does not support specified authority type")]
AuthorityTypeNotSupported,
#[error("This token mint cannot freeze accounts")]
MintCannotFreeze,
#[error("Account is frozen")]
AccountFrozen,
#[error("The provided decimals value different from the Mint decimals")]
MintDecimalsMismatch,
#[error("Instruction does not support non-native tokens")]
NonNativeNotSupported,
}
impl From<TokenError> for ProgramError {
fn from(e: TokenError) -> Self {
ProgramError::Custom(e as u32)
}
}
impl<T> DecodeError<T> for TokenError {
fn type_of() -> &'static str {
"TokenError"
}
}
impl PrintProgramError for TokenError {
fn print<E>(&self)
where
E: 'static
+ std::error::Error
+ DecodeError<E>
+ PrintProgramError
+ num_traits::FromPrimitive,
{
match self {
TokenError::NotRentExempt => msg!("Error: Lamport balance below rent-exempt threshold"),
TokenError::InsufficientFunds => msg!("Error: insufficient funds"),
TokenError::InvalidMint => msg!("Error: Invalid Mint"),
TokenError::MintMismatch => msg!("Error: Account not associated with this Mint"),
TokenError::OwnerMismatch => msg!("Error: owner does not match"),
TokenError::FixedSupply => msg!("Error: the total supply of this token is fixed"),
TokenError::AlreadyInUse => msg!("Error: account or token already in use"),
TokenError::InvalidNumberOfProvidedSigners => {
msg!("Error: Invalid number of provided signers")
}
TokenError::InvalidNumberOfRequiredSigners => {
msg!("Error: Invalid number of required signers")
}
TokenError::UninitializedState => msg!("Error: State is uninitialized"),
TokenError::NativeNotSupported => {
msg!("Error: Instruction does not support native tokens")
}
TokenError::NonNativeHasBalance => {
msg!("Error: Non-native account can only be closed if its balance is zero")
}
TokenError::InvalidInstruction => msg!("Error: Invalid instruction"),
TokenError::InvalidState => msg!("Error: Invalid account state for operation"),
TokenError::Overflow => msg!("Error: Operation overflowed"),
TokenError::AuthorityTypeNotSupported => {
msg!("Error: Account does not support specified authority type")
}
TokenError::MintCannotFreeze => msg!("Error: This token mint cannot freeze accounts"),
TokenError::AccountFrozen => msg!("Error: Account is frozen"),
TokenError::MintDecimalsMismatch => {
msg!("Error: decimals different from the Mint decimals")
}
TokenError::NonNativeNotSupported => {
msg!("Error: Instruction does not support non-native tokens")
}
}
}
}