error.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //! Error types
  2. use pinocchio::program_error::{ProgramError, ToStr};
  3. /// Errors that may be returned by the Token program.
  4. #[derive(Clone, Debug, Eq, PartialEq)]
  5. pub enum TokenError {
  6. // 0
  7. /// Lamport balance below rent-exempt threshold.
  8. NotRentExempt,
  9. /// Insufficient funds for the operation requested.
  10. InsufficientFunds,
  11. /// Invalid Mint.
  12. InvalidMint,
  13. /// Account not associated with this Mint.
  14. MintMismatch,
  15. /// Owner does not match.
  16. OwnerMismatch,
  17. // 5
  18. /// This token's supply is fixed and new tokens cannot be minted.
  19. FixedSupply,
  20. /// The account cannot be initialized because it is already being used.
  21. AlreadyInUse,
  22. /// Invalid number of provided signers.
  23. InvalidNumberOfProvidedSigners,
  24. /// Invalid number of required signers.
  25. InvalidNumberOfRequiredSigners,
  26. /// State is uninitialized.
  27. UninitializedState,
  28. // 10
  29. /// Instruction does not support native tokens
  30. NativeNotSupported,
  31. /// Non-native account can only be closed if its balance is zero
  32. NonNativeHasBalance,
  33. /// Invalid instruction
  34. InvalidInstruction,
  35. /// State is invalid for requested operation.
  36. InvalidState,
  37. /// Operation overflowed
  38. Overflow,
  39. // 15
  40. /// Account does not support specified authority type.
  41. AuthorityTypeNotSupported,
  42. /// This token mint cannot freeze accounts.
  43. MintCannotFreeze,
  44. /// Account is frozen; all account operations will fail
  45. AccountFrozen,
  46. /// Mint decimals mismatch between the client and mint
  47. MintDecimalsMismatch,
  48. /// Instruction does not support non-native tokens
  49. NonNativeNotSupported,
  50. }
  51. impl From<TokenError> for ProgramError {
  52. fn from(e: TokenError) -> Self {
  53. ProgramError::Custom(e as u32)
  54. }
  55. }
  56. impl ToStr for TokenError {
  57. fn to_str<E>(&self) -> &'static str
  58. where
  59. E: 'static + ToStr + TryFrom<u32>,
  60. {
  61. match self {
  62. TokenError::NotRentExempt => "Error: Lamport balance below rent-exempt threshold",
  63. TokenError::InsufficientFunds => "Error: insufficient funds",
  64. TokenError::InvalidMint => "Error: Invalid Mint",
  65. TokenError::MintMismatch => "Error: Account not associated with this Mint",
  66. TokenError::OwnerMismatch => "Error: owner does not match",
  67. TokenError::FixedSupply => "Error: the total supply of this token is fixed",
  68. TokenError::AlreadyInUse => "Error: account or token already in use",
  69. TokenError::InvalidNumberOfProvidedSigners => {
  70. "Error: Invalid number of provided signers"
  71. }
  72. TokenError::InvalidNumberOfRequiredSigners => {
  73. "Error: Invalid number of required signers"
  74. }
  75. TokenError::UninitializedState => "Error: State is uninitialized",
  76. TokenError::NativeNotSupported => "Error: Instruction does not support native tokens",
  77. TokenError::NonNativeHasBalance => {
  78. "Error: Non-native account can only be closed if its balance is zero"
  79. }
  80. TokenError::InvalidInstruction => "Error: Invalid instruction",
  81. TokenError::InvalidState => "Error: Invalid account state for operation",
  82. TokenError::Overflow => "Error: Operation overflowed",
  83. TokenError::AuthorityTypeNotSupported => {
  84. "Error: Account does not support specified authority type"
  85. }
  86. TokenError::MintCannotFreeze => "Error: This token mint cannot freeze accounts",
  87. TokenError::AccountFrozen => "Error: Account is frozen",
  88. TokenError::MintDecimalsMismatch => "Error: decimals different from the Mint decimals",
  89. TokenError::NonNativeNotSupported => {
  90. "Error: Instruction does not support non-native tokens"
  91. }
  92. }
  93. }
  94. }
  95. impl TryFrom<u32> for TokenError {
  96. type Error = ProgramError;
  97. fn try_from(value: u32) -> Result<Self, Self::Error> {
  98. match value {
  99. 0 => Ok(TokenError::NotRentExempt),
  100. 1 => Ok(TokenError::InsufficientFunds),
  101. 2 => Ok(TokenError::InvalidMint),
  102. 3 => Ok(TokenError::MintMismatch),
  103. 4 => Ok(TokenError::OwnerMismatch),
  104. 5 => Ok(TokenError::FixedSupply),
  105. 6 => Ok(TokenError::AlreadyInUse),
  106. 7 => Ok(TokenError::InvalidNumberOfProvidedSigners),
  107. 8 => Ok(TokenError::InvalidNumberOfRequiredSigners),
  108. 9 => Ok(TokenError::UninitializedState),
  109. 10 => Ok(TokenError::NativeNotSupported),
  110. 11 => Ok(TokenError::NonNativeHasBalance),
  111. 12 => Ok(TokenError::InvalidInstruction),
  112. 13 => Ok(TokenError::InvalidState),
  113. 14 => Ok(TokenError::Overflow),
  114. 15 => Ok(TokenError::AuthorityTypeNotSupported),
  115. 16 => Ok(TokenError::MintCannotFreeze),
  116. 17 => Ok(TokenError::AccountFrozen),
  117. 18 => Ok(TokenError::MintDecimalsMismatch),
  118. 19 => Ok(TokenError::NonNativeNotSupported),
  119. _ => Err(ProgramError::InvalidArgument),
  120. }
  121. }
  122. }