Skip to main content

spl_single_pool/
error.rs

1//! Error types
2
3use {
4    solana_program_error::{ProgramError, ToStr},
5    thiserror::Error,
6};
7
8/// Errors that may be returned by the `SinglePool` program.
9#[derive(
10    Clone, Debug, Eq, Error, num_enum::TryFromPrimitive, num_derive::FromPrimitive, PartialEq,
11)]
12#[repr(u32)]
13pub enum SinglePoolError {
14    // 0.
15    /// Provided pool account has the wrong address for its vote account, is
16    /// uninitialized, or otherwise invalid.
17    #[error("InvalidPoolAccount")]
18    InvalidPoolAccount,
19    /// Provided pool stake account does not match address derived from the pool
20    /// account.
21    #[error("InvalidPoolStakeAccount")]
22    InvalidPoolStakeAccount,
23    /// Provided pool mint does not match address derived from the pool account.
24    #[error("InvalidPoolMint")]
25    InvalidPoolMint,
26    /// Provided pool stake authority does not match address derived from the
27    /// pool account.
28    #[error("InvalidPoolStakeAuthority")]
29    InvalidPoolStakeAuthority,
30    /// Provided pool mint authority does not match address derived from the
31    /// pool account.
32    #[error("InvalidPoolMintAuthority")]
33    InvalidPoolMintAuthority,
34
35    // 5.
36    /// Provided pool MPL authority does not match address derived from the pool
37    /// account.
38    #[error("InvalidPoolMplAuthority")]
39    InvalidPoolMplAuthority,
40    /// Provided metadata account does not match metadata account derived for
41    /// pool mint.
42    #[error("InvalidMetadataAccount")]
43    InvalidMetadataAccount,
44    /// Authorized withdrawer provided for metadata update does not match the
45    /// vote account.
46    #[error("InvalidMetadataSigner")]
47    InvalidMetadataSigner,
48    /// Not enough lamports provided for deposit to result in one pool token.
49    #[error("DepositTooSmall")]
50    DepositTooSmall,
51    /// Not enough pool tokens provided to withdraw stake worth one lamport.
52    #[error("WithdrawalTooSmall")]
53    WithdrawalTooSmall,
54
55    // 10
56    /// Not enough stake to cover the provided quantity of pool tokens.
57    /// This typically means the value exists in the pool as activating stake,
58    /// and an epoch is required for it to become available. Otherwise, it means
59    /// active stake in the on-ramp must be moved via `ReplenishPool`.
60    #[error("WithdrawalTooLarge")]
61    WithdrawalTooLarge,
62    /// Required signature is missing.
63    #[error("SignatureMissing")]
64    SignatureMissing,
65    /// Stake account is not in the state expected by the program.
66    #[error("WrongStakeState")]
67    WrongStakeState,
68    /// Unsigned subtraction crossed the zero.
69    #[error("ArithmeticOverflow")]
70    ArithmeticOverflow,
71    /// A calculation failed unexpectedly.
72    /// (This error should never be surfaced; it stands in for failure
73    /// conditions that should never be reached.)
74    #[error("UnexpectedMathError")]
75    UnexpectedMathError,
76
77    // 15
78    /// The `V0_23_5` vote account type is unsupported and should be upgraded via
79    /// `convert_to_current()`.
80    #[error("LegacyVoteAccount")]
81    LegacyVoteAccount,
82    /// Failed to parse vote account.
83    #[error("UnparseableVoteAccount")]
84    UnparseableVoteAccount,
85    /// Incorrect number of lamports provided for rent-exemption when
86    /// initializing.
87    #[error("WrongRentAmount")]
88    WrongRentAmount,
89    /// Attempted to deposit from or withdraw to pool stake account.
90    #[error("InvalidPoolStakeAccountUsage")]
91    InvalidPoolStakeAccountUsage,
92    /// Attempted to initialize a pool that is already initialized.
93    #[error("PoolAlreadyInitialized")]
94    PoolAlreadyInitialized,
95
96    // 20
97    /// Provided pool on-ramp account does not match address derived from the pool
98    /// account.
99    #[error("InvalidPoolOnRampAccount")]
100    InvalidPoolOnRampAccount,
101    /// The on-ramp account for this pool does not exist; you must call `InitializePoolOnRamp`
102    /// before you can perform this operation.
103    #[error("OnRampDoesntExist")]
104    OnRampDoesntExist,
105    /// The present operation requires a `ReplenishPool` call, either because the pool stake account
106    /// is in an exceptional state, or because the on-ramp account should be refreshed.
107    #[error("ReplenishRequired")]
108    ReplenishRequired,
109    /// Withdrawal would render the pool stake account impossible to redelegate.
110    /// This can only occur if the Stake Program minimum delegation increases above 1 sol.
111    #[error("WithdrawalViolatesPoolRequirements")]
112    WithdrawalViolatesPoolRequirements,
113    /// The user-owned lamport source cannot be validated for `DepositSol`.
114    #[error("InvalidDepositSolSource")]
115    InvalidDepositSolSource,
116}
117impl From<SinglePoolError> for ProgramError {
118    fn from(e: SinglePoolError) -> Self {
119        ProgramError::Custom(e as u32)
120    }
121}
122impl ToStr for SinglePoolError {
123    fn to_str(&self) -> &'static str {
124        match self {
125            SinglePoolError::InvalidPoolAccount =>
126                "Error: Provided pool account has the wrong address for its vote account, is uninitialized, \
127                     or is otherwise invalid.",
128            SinglePoolError::InvalidPoolStakeAccount =>
129                "Error: Provided pool stake account does not match address derived from the pool account.",
130            SinglePoolError::InvalidPoolMint =>
131                "Error: Provided pool mint does not match address derived from the pool account.",
132            SinglePoolError::InvalidPoolStakeAuthority =>
133                "Error: Provided pool stake authority does not match address derived from the pool account.",
134            SinglePoolError::InvalidPoolMintAuthority =>
135                "Error: Provided pool mint authority does not match address derived from the pool account.",
136            SinglePoolError::InvalidPoolMplAuthority =>
137                "Error: Provided pool MPL authority does not match address derived from the pool account.",
138            SinglePoolError::InvalidMetadataAccount =>
139                "Error: Provided metadata account does not match metadata account derived for pool mint.",
140            SinglePoolError::InvalidMetadataSigner =>
141                "Error: Authorized withdrawer provided for metadata update does not match the vote account.",
142            SinglePoolError::DepositTooSmall =>
143                "Error: Not enough lamports provided for deposit to result in one pool token.",
144            SinglePoolError::WithdrawalTooSmall =>
145                "Error: Not enough pool tokens provided to withdraw stake worth one lamport.",
146            SinglePoolError::WithdrawalTooLarge =>
147                "Error: Not enough stake to cover the provided quantity of pool tokens. \
148                    This typically means the value exists in the pool as activating stake, \
149                    and an epoch is required for it to become available. Otherwise, it means \
150                    active stake in the onramp must be moved via `ReplenishPool`.",
151            SinglePoolError::SignatureMissing => "Error: Required signature is missing.",
152            SinglePoolError::WrongStakeState => "Error: Stake account is not in the state expected by the program.",
153            SinglePoolError::ArithmeticOverflow => "Error: Unsigned subtraction crossed the zero.",
154            SinglePoolError::UnexpectedMathError =>
155                "Error: A calculation failed unexpectedly. \
156                     (This error should never be surfaced; it stands in for failure conditions that should never be reached.)",
157            SinglePoolError::UnparseableVoteAccount => "Error: Failed to parse vote account.",
158            SinglePoolError::LegacyVoteAccount =>
159                "Error: The V0_23_5 vote account type is unsupported and should be upgraded via `convert_to_current()`.",
160            SinglePoolError::WrongRentAmount =>
161                "Error: Incorrect number of lamports provided for rent-exemption when initializing.",
162            SinglePoolError::InvalidPoolStakeAccountUsage =>
163                "Error: Attempted to deposit from or withdraw to pool stake account.",
164            SinglePoolError::PoolAlreadyInitialized =>
165                "Error: Attempted to initialize a pool that is already initialized.",
166            SinglePoolError::InvalidPoolOnRampAccount =>
167                "Error: Provided pool onramp account does not match address derived from the pool account.",
168            SinglePoolError::OnRampDoesntExist =>
169                "Error: The onramp account for this pool does not exist; you must call `InitializePoolOnRamp` \
170                     before you can perform this operation.",
171            SinglePoolError::ReplenishRequired =>
172                "Error: The present operation requires a `ReplenishPool` call, either because the pool stake account \
173                    is in an exceptional state, or because the on-ramp account should be refreshed.",
174            SinglePoolError::WithdrawalViolatesPoolRequirements =>
175                "Error: Withdrawal would render the pool stake account impossible to redelegate. \
176                    This can only occur if the Stake Program minimum delegation increases above 1 sol.",
177            SinglePoolError::InvalidDepositSolSource =>
178                "Error: The user-owned lamport source cannot be validated for `DepositSol`.",
179        }
180    }
181}