1use light_account_checks::error::AccountError;
2use light_compressed_account::CompressedAccountError;
3use light_hasher::HasherError;
4use light_sdk_types::error::LightSdkTypesError;
5use light_zero_copy::errors::ZeroCopyError;
6use thiserror::Error;
7
8use crate::ProgramError;
9
10pub type Result<T> = std::result::Result<T, LightSdkError>;
11
12#[derive(Debug, Error, PartialEq)]
13pub enum LightSdkError {
14 #[error("Constraint violation")]
15 ConstraintViolation,
16 #[error("Invalid light-system-program ID")]
17 InvalidLightSystemProgram,
18 #[error("Expected accounts in the instruction")]
19 ExpectedAccounts,
20 #[error("Expected address Merkle context to be provided")]
21 ExpectedAddressTreeInfo,
22 #[error("Expected address root index to be provided")]
23 ExpectedAddressRootIndex,
24 #[error("Accounts with a specified input are expected to have data")]
25 ExpectedData,
26 #[error("Accounts with specified data are expected to have a discriminator")]
27 ExpectedDiscriminator,
28 #[error("Accounts with specified data are expected to have a hash")]
29 ExpectedHash,
30 #[error("Expected the `{0}` light account to be provided")]
31 ExpectedLightSystemAccount(String),
32 #[error("`mut` and `close` accounts are expected to have a Merkle context")]
33 ExpectedMerkleContext,
34 #[error("Expected root index to be provided")]
35 ExpectedRootIndex,
36 #[error("Cannot transfer lamports from an account without input")]
37 TransferFromNoInput,
38 #[error("Cannot transfer from an account without lamports")]
39 TransferFromNoLamports,
40 #[error("Account, from which a transfer was attempted, has insufficient amount of lamports")]
41 TransferFromInsufficientLamports,
42 #[error("Integer overflow resulting from too large resulting amount")]
43 TransferIntegerOverflow,
44 #[error("Borsh error.")]
45 Borsh,
46 #[error("Fewer accounts than number of system accounts.")]
47 FewerAccountsThanSystemAccounts,
48 #[error("InvalidCpiSignerAccount")]
49 InvalidCpiSignerAccount,
50 #[error("Missing meta field: {0}")]
51 MissingField(String),
52 #[error("Output state tree index is none. Use an CompressedAccountMeta type with output tree index to initialize or update accounts.")]
53 OutputStateTreeIndexIsNone,
54 #[error("Address is none during initialization")]
55 InitAddressIsNone,
56 #[error("Address is none during initialization with address")]
57 InitWithAddressIsNone,
58 #[error("Output is none during initialization with address")]
59 InitWithAddressOutputIsNone,
60 #[error("Address is none during meta mutation")]
61 MetaMutAddressIsNone,
62 #[error("Input is none during meta mutation")]
63 MetaMutInputIsNone,
64 #[error("Output lamports is none during meta mutation")]
65 MetaMutOutputLamportsIsNone,
66 #[error("Output is none during meta mutation")]
67 MetaMutOutputIsNone,
68 #[error("Address is none during meta close")]
69 MetaCloseAddressIsNone,
70 #[error("Input is none during meta close")]
71 MetaCloseInputIsNone,
72 #[error("CPI accounts index out of bounds: {0}")]
73 CpiAccountsIndexOutOfBounds(usize),
74 #[error("Invalid CPI context account")]
75 InvalidCpiContextAccount,
76 #[error("Invalid SolPool PDA account")]
77 InvalidSolPoolPdaAccount,
78 #[error("CpigAccounts accounts slice starts with an invalid account. It should start with LightSystemProgram SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7.")]
79 InvalidCpiAccountsOffset,
80 #[error("Expected LightAccount to have no data for closure.")]
81 ExpectedNoData,
82 #[error("CPI context must be added before any other accounts (next_index must be 0)")]
83 CpiContextOrderingViolation,
84 #[error("Invalid merkle tree index in CPI accounts")]
85 InvalidMerkleTreeIndex,
86 #[error(
87 "Read-only account cannot use to_account_info(), use to_packed_read_only_account() instead"
88 )]
89 ReadOnlyAccountCannotUseToAccountInfo,
90 #[error("Account is not read-only, cannot use to_packed_read_only_account()")]
91 NotReadOnlyAccount,
92 #[error("Read-only accounts are not supported in write_to_cpi_context operations")]
93 ReadOnlyAccountsNotSupportedInCpiContext,
94 #[error(transparent)]
95 AccountError(#[from] AccountError),
96 #[error(transparent)]
97 Hasher(#[from] HasherError),
98 #[error(transparent)]
99 ZeroCopy(#[from] ZeroCopyError),
100 #[error("Program error: {0}")]
101 ProgramError(#[from] ProgramError),
102 #[error("Compressed account error: {0}")]
103 CompressedAccountError(#[from] CompressedAccountError),
104 #[error("Expected tree info to be provided for init_if_needed")]
105 ExpectedTreeInfo,
106 #[error("ExpectedSelfProgram")]
107 ExpectedSelfProgram,
108 #[error("Expected CPI context to be provided")]
109 ExpectedCpiContext,
110}
111
112impl From<LightSdkError> for ProgramError {
113 fn from(e: LightSdkError) -> Self {
114 ProgramError::Custom(e.into())
115 }
116}
117
118impl From<LightSdkTypesError> for LightSdkError {
119 fn from(e: LightSdkTypesError) -> Self {
120 match e {
121 LightSdkTypesError::InitAddressIsNone => LightSdkError::InitAddressIsNone,
122 LightSdkTypesError::InitWithAddressIsNone => LightSdkError::InitWithAddressIsNone,
123 LightSdkTypesError::InitWithAddressOutputIsNone => {
124 LightSdkError::InitWithAddressOutputIsNone
125 }
126 LightSdkTypesError::MetaMutAddressIsNone => LightSdkError::MetaMutAddressIsNone,
127 LightSdkTypesError::MetaMutInputIsNone => LightSdkError::MetaMutInputIsNone,
128 LightSdkTypesError::MetaMutOutputLamportsIsNone => {
129 LightSdkError::MetaMutOutputLamportsIsNone
130 }
131 LightSdkTypesError::MetaMutOutputIsNone => LightSdkError::MetaMutOutputIsNone,
132 LightSdkTypesError::MetaCloseAddressIsNone => LightSdkError::MetaCloseAddressIsNone,
133 LightSdkTypesError::MetaCloseInputIsNone => LightSdkError::MetaCloseInputIsNone,
134 LightSdkTypesError::FewerAccountsThanSystemAccounts => {
135 LightSdkError::FewerAccountsThanSystemAccounts
136 }
137 LightSdkTypesError::CpiAccountsIndexOutOfBounds(index) => {
138 LightSdkError::CpiAccountsIndexOutOfBounds(index)
139 }
140 LightSdkTypesError::InvalidSolPoolPdaAccount => LightSdkError::InvalidSolPoolPdaAccount,
141 LightSdkTypesError::InvalidCpiContextAccount => LightSdkError::InvalidCpiContextAccount,
142 LightSdkTypesError::InvalidCpiAccountsOffset => LightSdkError::InvalidCpiAccountsOffset,
143 LightSdkTypesError::AccountError(e) => LightSdkError::AccountError(e),
144 LightSdkTypesError::Hasher(e) => LightSdkError::Hasher(e),
145 }
146 }
147}
148
149impl From<LightSdkError> for u32 {
150 fn from(e: LightSdkError) -> Self {
151 match e {
152 LightSdkError::ConstraintViolation => 16001,
153 LightSdkError::InvalidLightSystemProgram => 16002,
154 LightSdkError::ExpectedAccounts => 16003,
155 LightSdkError::ExpectedAddressTreeInfo => 16004,
156 LightSdkError::ExpectedAddressRootIndex => 16005,
157 LightSdkError::ExpectedData => 16006,
158 LightSdkError::ExpectedDiscriminator => 16007,
159 LightSdkError::ExpectedHash => 16008,
160 LightSdkError::ExpectedLightSystemAccount(_) => 16009,
161 LightSdkError::ExpectedMerkleContext => 16010,
162 LightSdkError::ExpectedRootIndex => 16011,
163 LightSdkError::TransferFromNoInput => 16012,
164 LightSdkError::TransferFromNoLamports => 16013,
165 LightSdkError::TransferFromInsufficientLamports => 16014,
166 LightSdkError::TransferIntegerOverflow => 16015,
167 LightSdkError::Borsh => 16016,
168 LightSdkError::FewerAccountsThanSystemAccounts => 16017,
169 LightSdkError::InvalidCpiSignerAccount => 16018,
170 LightSdkError::MissingField(_) => 16019,
171 LightSdkError::OutputStateTreeIndexIsNone => 16020,
172 LightSdkError::InitAddressIsNone => 16021,
173 LightSdkError::InitWithAddressIsNone => 16022,
174 LightSdkError::InitWithAddressOutputIsNone => 16023,
175 LightSdkError::MetaMutAddressIsNone => 16024,
176 LightSdkError::MetaMutInputIsNone => 16025,
177 LightSdkError::MetaMutOutputLamportsIsNone => 16026,
178 LightSdkError::MetaMutOutputIsNone => 16027,
179 LightSdkError::MetaCloseAddressIsNone => 16028,
180 LightSdkError::MetaCloseInputIsNone => 16029,
181 LightSdkError::CpiAccountsIndexOutOfBounds(_) => 16031,
182 LightSdkError::InvalidCpiContextAccount => 16032,
183 LightSdkError::InvalidSolPoolPdaAccount => 16033,
184 LightSdkError::InvalidCpiAccountsOffset => 16034,
185 LightSdkError::ExpectedNoData => 16035,
186 LightSdkError::CpiContextOrderingViolation => 16036,
187 LightSdkError::InvalidMerkleTreeIndex => 16037,
188 LightSdkError::ReadOnlyAccountCannotUseToAccountInfo => 16038,
189 LightSdkError::NotReadOnlyAccount => 16039,
190 LightSdkError::ReadOnlyAccountsNotSupportedInCpiContext => 16040,
191 LightSdkError::AccountError(e) => e.into(),
192 LightSdkError::Hasher(e) => e.into(),
193 LightSdkError::ZeroCopy(e) => e.into(),
194 LightSdkError::ProgramError(e) => u64::from(e) as u32,
195 LightSdkError::CompressedAccountError(e) => e.into(),
196 LightSdkError::ExpectedTreeInfo => 16041,
197 LightSdkError::ExpectedSelfProgram => 16042,
198 LightSdkError::ExpectedCpiContext => 16043,
199 }
200 }
201}