Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit d3cbe36

Browse files
authored
token-2022: Refactor PrintProgramError impl to error file (#3366)
1 parent 725430d commit d3cbe36

File tree

2 files changed

+111
-109
lines changed

2 files changed

+111
-109
lines changed

token/program-2022/src/error.rs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
33
use {
44
num_derive::FromPrimitive,
5-
solana_program::{decode_error::DecodeError, program_error::ProgramError},
5+
solana_program::{
6+
decode_error::DecodeError,
7+
msg,
8+
program_error::{PrintProgramError, ProgramError},
9+
},
610
thiserror::Error,
711
};
812

@@ -163,3 +167,107 @@ impl<T> DecodeError<T> for TokenError {
163167
"TokenError"
164168
}
165169
}
170+
171+
impl PrintProgramError for TokenError {
172+
fn print<E>(&self)
173+
where
174+
E: 'static + std::error::Error + DecodeError<E> + num_traits::FromPrimitive,
175+
{
176+
match self {
177+
TokenError::NotRentExempt => msg!("Error: Lamport balance below rent-exempt threshold"),
178+
TokenError::InsufficientFunds => msg!("Error: insufficient funds"),
179+
TokenError::InvalidMint => msg!("Error: Invalid Mint"),
180+
TokenError::MintMismatch => msg!("Error: Account not associated with this Mint"),
181+
TokenError::OwnerMismatch => msg!("Error: owner does not match"),
182+
TokenError::FixedSupply => msg!("Error: the total supply of this token is fixed"),
183+
TokenError::AlreadyInUse => msg!("Error: account or token already in use"),
184+
TokenError::InvalidNumberOfProvidedSigners => {
185+
msg!("Error: Invalid number of provided signers")
186+
}
187+
TokenError::InvalidNumberOfRequiredSigners => {
188+
msg!("Error: Invalid number of required signers")
189+
}
190+
TokenError::UninitializedState => msg!("Error: State is uninitialized"),
191+
TokenError::NativeNotSupported => {
192+
msg!("Error: Instruction does not support native tokens")
193+
}
194+
TokenError::NonNativeHasBalance => {
195+
msg!("Error: Non-native account can only be closed if its balance is zero")
196+
}
197+
TokenError::InvalidInstruction => msg!("Error: Invalid instruction"),
198+
TokenError::InvalidState => msg!("Error: Invalid account state for operation"),
199+
TokenError::Overflow => msg!("Error: Operation overflowed"),
200+
TokenError::AuthorityTypeNotSupported => {
201+
msg!("Error: Account does not support specified authority type")
202+
}
203+
TokenError::MintCannotFreeze => msg!("Error: This token mint cannot freeze accounts"),
204+
TokenError::AccountFrozen => msg!("Error: Account is frozen"),
205+
TokenError::MintDecimalsMismatch => {
206+
msg!("Error: decimals different from the Mint decimals")
207+
}
208+
TokenError::NonNativeNotSupported => {
209+
msg!("Error: Instruction does not support non-native tokens")
210+
}
211+
TokenError::ExtensionTypeMismatch => {
212+
msg!("Error: New extension type does not match already existing extensions")
213+
}
214+
TokenError::ExtensionBaseMismatch => {
215+
msg!("Error: Extension does not match the base type provided")
216+
}
217+
TokenError::ExtensionAlreadyInitialized => {
218+
msg!("Error: Extension already initialized on this account")
219+
}
220+
TokenError::ConfidentialTransferAccountHasBalance => {
221+
msg!("Error: An account can only be closed if its confidential balance is zero")
222+
}
223+
TokenError::ConfidentialTransferAccountNotApproved => {
224+
msg!("Error: Account not approved for confidential transfers")
225+
}
226+
TokenError::ConfidentialTransferDepositsAndTransfersDisabled => {
227+
msg!("Error: Account not accepting deposits or transfers")
228+
}
229+
TokenError::ConfidentialTransferElGamalPubkeyMismatch => {
230+
msg!("Error: ElGamal public key mismatch")
231+
}
232+
TokenError::ConfidentialTransferBalanceMismatch => {
233+
msg!("Error: Balance mismatch")
234+
}
235+
TokenError::MintHasSupply => {
236+
msg!("Error: Mint has non-zero supply. Burn all tokens before closing the mint")
237+
}
238+
TokenError::NoAuthorityExists => {
239+
msg!("Error: No authority exists to perform the desired operation");
240+
}
241+
TokenError::TransferFeeExceedsMaximum => {
242+
msg!("Error: Transfer fee exceeds maximum of 10,000 basis points");
243+
}
244+
TokenError::MintRequiredForTransfer => {
245+
msg!("Mint required for this account to transfer tokens, use `transfer_checked` or `transfer_checked_with_fee`");
246+
}
247+
TokenError::FeeMismatch => {
248+
msg!("Calculated fee does not match expected fee");
249+
}
250+
TokenError::FeeParametersMismatch => {
251+
msg!("Fee parameters associated with zero-knowledge proofs do not match fee parameters in mint")
252+
}
253+
TokenError::ImmutableOwner => {
254+
msg!("The owner authority cannot be changed");
255+
}
256+
TokenError::AccountHasWithheldTransferFees => {
257+
msg!("Error: An account can only be closed if its withheld fee balance is zero, harvest fees to the mint and try again");
258+
}
259+
TokenError::NoMemo => {
260+
msg!("Error: No memo in previous instruction; required for recipient to receive a transfer");
261+
}
262+
TokenError::NonTransferable => {
263+
msg!("Transfer is disabled for this mint");
264+
}
265+
TokenError::NonTransferableNeedsImmutableOwnership => {
266+
msg!("Non-transferable tokens can't be minted to an account without immutable ownership");
267+
}
268+
TokenError::MaximumPendingBalanceCreditCounterExceeded => {
269+
msg!("The total number of `Deposit` and `Transfer` instructions to an account cannot exceed the associated `maximum_pending_balance_credit_counter`");
270+
}
271+
}
272+
}
273+
}

token/program-2022/src/processor.rs

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ use {
2020
native_mint,
2121
state::{Account, AccountState, Mint, Multisig},
2222
},
23-
num_traits::FromPrimitive,
2423
solana_program::{
2524
account_info::{next_account_info, AccountInfo},
2625
clock::Clock,
27-
decode_error::DecodeError,
2826
entrypoint::ProgramResult,
2927
msg,
3028
program::{invoke, invoke_signed, set_return_data},
31-
program_error::{PrintProgramError, ProgramError},
29+
program_error::ProgramError,
3230
program_memory::sol_memset,
3331
program_option::COption,
3432
program_pack::Pack,
@@ -1390,110 +1388,6 @@ impl Processor {
13901388
}
13911389
}
13921390

1393-
impl PrintProgramError for TokenError {
1394-
fn print<E>(&self)
1395-
where
1396-
E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
1397-
{
1398-
match self {
1399-
TokenError::NotRentExempt => msg!("Error: Lamport balance below rent-exempt threshold"),
1400-
TokenError::InsufficientFunds => msg!("Error: insufficient funds"),
1401-
TokenError::InvalidMint => msg!("Error: Invalid Mint"),
1402-
TokenError::MintMismatch => msg!("Error: Account not associated with this Mint"),
1403-
TokenError::OwnerMismatch => msg!("Error: owner does not match"),
1404-
TokenError::FixedSupply => msg!("Error: the total supply of this token is fixed"),
1405-
TokenError::AlreadyInUse => msg!("Error: account or token already in use"),
1406-
TokenError::InvalidNumberOfProvidedSigners => {
1407-
msg!("Error: Invalid number of provided signers")
1408-
}
1409-
TokenError::InvalidNumberOfRequiredSigners => {
1410-
msg!("Error: Invalid number of required signers")
1411-
}
1412-
TokenError::UninitializedState => msg!("Error: State is uninitialized"),
1413-
TokenError::NativeNotSupported => {
1414-
msg!("Error: Instruction does not support native tokens")
1415-
}
1416-
TokenError::NonNativeHasBalance => {
1417-
msg!("Error: Non-native account can only be closed if its balance is zero")
1418-
}
1419-
TokenError::InvalidInstruction => msg!("Error: Invalid instruction"),
1420-
TokenError::InvalidState => msg!("Error: Invalid account state for operation"),
1421-
TokenError::Overflow => msg!("Error: Operation overflowed"),
1422-
TokenError::AuthorityTypeNotSupported => {
1423-
msg!("Error: Account does not support specified authority type")
1424-
}
1425-
TokenError::MintCannotFreeze => msg!("Error: This token mint cannot freeze accounts"),
1426-
TokenError::AccountFrozen => msg!("Error: Account is frozen"),
1427-
TokenError::MintDecimalsMismatch => {
1428-
msg!("Error: decimals different from the Mint decimals")
1429-
}
1430-
TokenError::NonNativeNotSupported => {
1431-
msg!("Error: Instruction does not support non-native tokens")
1432-
}
1433-
TokenError::ExtensionTypeMismatch => {
1434-
msg!("Error: New extension type does not match already existing extensions")
1435-
}
1436-
TokenError::ExtensionBaseMismatch => {
1437-
msg!("Error: Extension does not match the base type provided")
1438-
}
1439-
TokenError::ExtensionAlreadyInitialized => {
1440-
msg!("Error: Extension already initialized on this account")
1441-
}
1442-
TokenError::ConfidentialTransferAccountHasBalance => {
1443-
msg!("Error: An account can only be closed if its confidential balance is zero")
1444-
}
1445-
TokenError::ConfidentialTransferAccountNotApproved => {
1446-
msg!("Error: Account not approved for confidential transfers")
1447-
}
1448-
TokenError::ConfidentialTransferDepositsAndTransfersDisabled => {
1449-
msg!("Error: Account not accepting deposits or transfers")
1450-
}
1451-
TokenError::ConfidentialTransferElGamalPubkeyMismatch => {
1452-
msg!("Error: ElGamal public key mismatch")
1453-
}
1454-
TokenError::ConfidentialTransferBalanceMismatch => {
1455-
msg!("Error: Balance mismatch")
1456-
}
1457-
TokenError::MintHasSupply => {
1458-
msg!("Error: Mint has non-zero supply. Burn all tokens before closing the mint")
1459-
}
1460-
TokenError::NoAuthorityExists => {
1461-
msg!("Error: No authority exists to perform the desired operation");
1462-
}
1463-
TokenError::TransferFeeExceedsMaximum => {
1464-
msg!("Error: Transfer fee exceeds maximum of 10,000 basis points");
1465-
}
1466-
TokenError::MintRequiredForTransfer => {
1467-
msg!("Mint required for this account to transfer tokens, use `transfer_checked` or `transfer_checked_with_fee`");
1468-
}
1469-
TokenError::FeeMismatch => {
1470-
msg!("Calculated fee does not match expected fee");
1471-
}
1472-
TokenError::FeeParametersMismatch => {
1473-
msg!("Fee parameters associated with zero-knowledge proofs do not match fee parameters in mint")
1474-
}
1475-
TokenError::ImmutableOwner => {
1476-
msg!("The owner authority cannot be changed");
1477-
}
1478-
TokenError::AccountHasWithheldTransferFees => {
1479-
msg!("Error: An account can only be closed if its withheld fee balance is zero, harvest fees to the mint and try again");
1480-
}
1481-
TokenError::NoMemo => {
1482-
msg!("Error: No memo in previous instruction; required for recipient to receive a transfer");
1483-
}
1484-
TokenError::NonTransferable => {
1485-
msg!("Transfer is disabled for this mint");
1486-
}
1487-
TokenError::NonTransferableNeedsImmutableOwnership => {
1488-
msg!("Non-transferable tokens can't be minted to an account without immutable ownership");
1489-
}
1490-
TokenError::MaximumPendingBalanceCreditCounterExceeded => {
1491-
msg!("The total number of `Deposit` and `Transfer` instructions to an account cannot exceed the associated `maximum_pending_balance_credit_counter`");
1492-
}
1493-
}
1494-
}
1495-
}
1496-
14971391
#[cfg(test)]
14981392
mod tests {
14991393
use {
@@ -1506,7 +1400,7 @@ mod tests {
15061400
account_info::IntoAccountInfo,
15071401
clock::Epoch,
15081402
instruction::Instruction,
1509-
program_error,
1403+
program_error::{self, PrintProgramError},
15101404
sysvar::{clock::Clock, rent},
15111405
},
15121406
solana_sdk::account::{

0 commit comments

Comments
 (0)