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}
114impl From<SinglePoolError> for ProgramError {
115    fn from(e: SinglePoolError) -> Self {
116        ProgramError::Custom(e as u32)
117    }
118}
119impl ToStr for SinglePoolError {
120    fn to_str(&self) -> &'static str {
121        match self {
122            SinglePoolError::InvalidPoolAccount =>
123                "Error: Provided pool account has the wrong address for its vote account, is uninitialized, \
124                     or is otherwise invalid.",
125            SinglePoolError::InvalidPoolStakeAccount =>
126                "Error: Provided pool stake account does not match address derived from the pool account.",
127            SinglePoolError::InvalidPoolMint =>
128                "Error: Provided pool mint does not match address derived from the pool account.",
129            SinglePoolError::InvalidPoolStakeAuthority =>
130                "Error: Provided pool stake authority does not match address derived from the pool account.",
131            SinglePoolError::InvalidPoolMintAuthority =>
132                "Error: Provided pool mint authority does not match address derived from the pool account.",
133            SinglePoolError::InvalidPoolMplAuthority =>
134                "Error: Provided pool MPL authority does not match address derived from the pool account.",
135            SinglePoolError::InvalidMetadataAccount =>
136                "Error: Provided metadata account does not match metadata account derived for pool mint.",
137            SinglePoolError::InvalidMetadataSigner =>
138                "Error: Authorized withdrawer provided for metadata update does not match the vote account.",
139            SinglePoolError::DepositTooSmall =>
140                "Error: Not enough lamports provided for deposit to result in one pool token.",
141            SinglePoolError::WithdrawalTooSmall =>
142                "Error: Not enough pool tokens provided to withdraw stake worth one lamport.",
143            SinglePoolError::WithdrawalTooLarge =>
144                "Error: Not enough stake to cover the provided quantity of pool tokens. \
145                    This typically means the value exists in the pool as activating stake, \
146                    and an epoch is required for it to become available. Otherwise, it means \
147                    active stake in the onramp must be moved via `ReplenishPool`.",
148            SinglePoolError::SignatureMissing => "Error: Required signature is missing.",
149            SinglePoolError::WrongStakeState => "Error: Stake account is not in the state expected by the program.",
150            SinglePoolError::ArithmeticOverflow => "Error: Unsigned subtraction crossed the zero.",
151            SinglePoolError::UnexpectedMathError =>
152                "Error: A calculation failed unexpectedly. \
153                     (This error should never be surfaced; it stands in for failure conditions that should never be reached.)",
154            SinglePoolError::UnparseableVoteAccount => "Error: Failed to parse vote account.",
155            SinglePoolError::LegacyVoteAccount =>
156                "Error: The V0_23_5 vote account type is unsupported and should be upgraded via `convert_to_current()`.",
157            SinglePoolError::WrongRentAmount =>
158                "Error: Incorrect number of lamports provided for rent-exemption when initializing.",
159            SinglePoolError::InvalidPoolStakeAccountUsage =>
160                "Error: Attempted to deposit from or withdraw to pool stake account.",
161            SinglePoolError::PoolAlreadyInitialized =>
162                "Error: Attempted to initialize a pool that is already initialized.",
163            SinglePoolError::InvalidPoolOnRampAccount =>
164                "Error: Provided pool onramp account does not match address derived from the pool account.",
165            SinglePoolError::OnRampDoesntExist =>
166                "Error: The onramp account for this pool does not exist; you must call `InitializePoolOnRamp` \
167                     before you can perform this operation.",
168            SinglePoolError::ReplenishRequired =>
169                "Error: The present operation requires a `ReplenishPool` call, either because the pool stake account \
170                    is in an exceptional state, or because the on-ramp account should be refreshed.",
171            SinglePoolError::WithdrawalViolatesPoolRequirements =>
172                "Error: Withdrawal would render the pool stake account impossible to redelegate. \
173                    This can only occur if the Stake Program minimum delegation increases above 1 sol.",
174        }
175    }
176}