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(transparent)]
85 AccountError(#[from] AccountError),
86 #[error(transparent)]
87 Hasher(#[from] HasherError),
88 #[error(transparent)]
89 ZeroCopy(#[from] ZeroCopyError),
90 #[error("Program error: {0}")]
91 ProgramError(#[from] ProgramError),
92 #[error("Compressed account error: {0}")]
93 CompressedAccountError(#[from] CompressedAccountError),
94 #[error("Expected tree info to be provided for init_if_needed")]
95 ExpectedTreeInfo,
96}
97
98impl From<LightSdkError> for ProgramError {
99 fn from(e: LightSdkError) -> Self {
100 ProgramError::Custom(e.into())
101 }
102}
103
104impl From<LightSdkTypesError> for LightSdkError {
105 fn from(e: LightSdkTypesError) -> Self {
106 match e {
107 LightSdkTypesError::InitAddressIsNone => LightSdkError::InitAddressIsNone,
108 LightSdkTypesError::InitWithAddressIsNone => LightSdkError::InitWithAddressIsNone,
109 LightSdkTypesError::InitWithAddressOutputIsNone => {
110 LightSdkError::InitWithAddressOutputIsNone
111 }
112 LightSdkTypesError::MetaMutAddressIsNone => LightSdkError::MetaMutAddressIsNone,
113 LightSdkTypesError::MetaMutInputIsNone => LightSdkError::MetaMutInputIsNone,
114 LightSdkTypesError::MetaMutOutputLamportsIsNone => {
115 LightSdkError::MetaMutOutputLamportsIsNone
116 }
117 LightSdkTypesError::MetaMutOutputIsNone => LightSdkError::MetaMutOutputIsNone,
118 LightSdkTypesError::MetaCloseAddressIsNone => LightSdkError::MetaCloseAddressIsNone,
119 LightSdkTypesError::MetaCloseInputIsNone => LightSdkError::MetaCloseInputIsNone,
120 LightSdkTypesError::FewerAccountsThanSystemAccounts => {
121 LightSdkError::FewerAccountsThanSystemAccounts
122 }
123 LightSdkTypesError::CpiAccountsIndexOutOfBounds(index) => {
124 LightSdkError::CpiAccountsIndexOutOfBounds(index)
125 }
126 LightSdkTypesError::InvalidSolPoolPdaAccount => LightSdkError::InvalidSolPoolPdaAccount,
127 LightSdkTypesError::InvalidCpiContextAccount => LightSdkError::InvalidCpiContextAccount,
128 LightSdkTypesError::InvalidCpiAccountsOffset => LightSdkError::InvalidCpiAccountsOffset,
129 LightSdkTypesError::AccountError(e) => LightSdkError::AccountError(e),
130 LightSdkTypesError::Hasher(e) => LightSdkError::Hasher(e),
131 }
132 }
133}
134
135impl From<LightSdkError> for u32 {
136 fn from(e: LightSdkError) -> Self {
137 match e {
138 LightSdkError::ConstraintViolation => 16001,
139 LightSdkError::InvalidLightSystemProgram => 16002,
140 LightSdkError::ExpectedAccounts => 16003,
141 LightSdkError::ExpectedAddressTreeInfo => 16004,
142 LightSdkError::ExpectedAddressRootIndex => 16005,
143 LightSdkError::ExpectedData => 16006,
144 LightSdkError::ExpectedDiscriminator => 16007,
145 LightSdkError::ExpectedHash => 16008,
146 LightSdkError::ExpectedLightSystemAccount(_) => 16009,
147 LightSdkError::ExpectedMerkleContext => 16010,
148 LightSdkError::ExpectedRootIndex => 16011,
149 LightSdkError::TransferFromNoInput => 16012,
150 LightSdkError::TransferFromNoLamports => 16013,
151 LightSdkError::TransferFromInsufficientLamports => 16014,
152 LightSdkError::TransferIntegerOverflow => 16015,
153 LightSdkError::Borsh => 16016,
154 LightSdkError::FewerAccountsThanSystemAccounts => 16017,
155 LightSdkError::InvalidCpiSignerAccount => 16018,
156 LightSdkError::MissingField(_) => 16019,
157 LightSdkError::OutputStateTreeIndexIsNone => 16020,
158 LightSdkError::InitAddressIsNone => 16021,
159 LightSdkError::InitWithAddressIsNone => 16022,
160 LightSdkError::InitWithAddressOutputIsNone => 16023,
161 LightSdkError::MetaMutAddressIsNone => 16024,
162 LightSdkError::MetaMutInputIsNone => 16025,
163 LightSdkError::MetaMutOutputLamportsIsNone => 16026,
164 LightSdkError::MetaMutOutputIsNone => 16027,
165 LightSdkError::MetaCloseAddressIsNone => 16028,
166 LightSdkError::MetaCloseInputIsNone => 16029,
167 LightSdkError::CpiAccountsIndexOutOfBounds(_) => 16031,
168 LightSdkError::InvalidCpiContextAccount => 16032,
169 LightSdkError::InvalidSolPoolPdaAccount => 16033,
170 LightSdkError::InvalidCpiAccountsOffset => 16034,
171 LightSdkError::ExpectedNoData => 16035,
172 LightSdkError::CpiContextOrderingViolation => 16036,
173 LightSdkError::AccountError(e) => e.into(),
174 LightSdkError::Hasher(e) => e.into(),
175 LightSdkError::ZeroCopy(e) => e.into(),
176 LightSdkError::ProgramError(e) => u64::from(e) as u32,
177 LightSdkError::CompressedAccountError(e) => e.into(),
178 LightSdkError::ExpectedTreeInfo => 16021,
179 }
180 }
181}