port_finance_staking/
error.rs

1use num_derive::FromPrimitive;
2use num_traits::FromPrimitive;
3use thiserror::Error;
4
5use crate::solana_program::decode_error::DecodeError;
6use crate::solana_program::msg;
7use crate::solana_program::program_error::{PrintProgramError, ProgramError};
8
9#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
10pub enum StakingError {
11    //0
12    #[error("Failed to unpack instruction data")]
13    InstructionUnpackError,
14    #[error("Account is already initialized")]
15    AlreadyInitialized,
16    /// Lamport balance below rent-exempt threshold.
17    #[error("Lamport balance below rent-exempt threshold")]
18    NotRentExempt,
19    /// Math operation overflow
20    #[error("Math operation overflow")]
21    MathOverflow,
22    #[error("Stake Account deposits have zero value")]
23    StakeDepositsZero,
24
25    //5
26    #[error("Stake Account withdraws have zero value")]
27    StakeWithdrawsZero,
28    #[error("Invalid Argument")]
29    InvalidArgumentError,
30    #[error("Supply to the staking pool must be non zero")]
31    InvalidSupplyError,
32    #[error("Duration to the staking pool must be non zero")]
33    InvalidDurationError,
34    #[error("Current Time must be greater than start time")]
35    InvalidCurrentTimeError,
36
37    //10
38    #[error("Withdraw amount must be smaller than balance")]
39    InvalidWithdrawAmountError,
40    #[error("Reward rate must be monotonic increasing")]
41    InvalidCurrentRateError,
42    #[error("Invalid account owner")]
43    InvalidAccountOwner,
44    #[error("Insufficient token supply for rewarding")]
45    InSufficientSupplyError,
46    #[error("Not correct signer")]
47    InvalidSigner,
48
49    //15
50    #[error("Reward supply token account is illegal")]
51    InvalidRewardSupplyAccountError,
52    #[error("Input token mint account is not valid")]
53    InvalidTokenMint,
54    #[error("Input token account is not owned by the correct token program id")]
55    InvalidTokenOwner,
56    #[error("Reward token pool must be owned by the staking program")]
57    InvalidRewardTokenPoolOwner,
58    #[error("Invalid reward token supply account")]
59    InvalidRewardTokenSupplyAccount,
60
61    //20
62    #[error("Invalid staking pool")]
63    InvalidStakingPool,
64    #[error("Invalid stake account")]
65    InvalidStakeAccount,
66    #[error("Invalid reward token pool")]
67    InvalidRewardTokenPool,
68    #[error("Transfer token failed")]
69    TokenTransferFailed,
70    #[error("token account init failed")]
71    TokenInitializeAccountFailed,
72
73    //25
74    #[error("Cannot reduce reward smaller than zero")]
75    ReduceRewardTooMuch,
76}
77
78impl From<StakingError> for ProgramError {
79    fn from(e: StakingError) -> Self {
80        ProgramError::Custom(e as u32)
81    }
82}
83
84impl<T> DecodeError<T> for StakingError {
85    fn type_of() -> &'static str {
86        "Staking Error"
87    }
88}
89
90impl PrintProgramError for StakingError {
91    fn print<E>(&self)
92    where
93        E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
94    {
95        msg!(&self.to_string());
96    }
97}