token.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * This code was AUTOGENERATED using the codama library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun codama to update it.
  5. *
  6. * @see https://github.com/codama-idl/codama
  7. */
  8. import {
  9. isProgramError,
  10. type Address,
  11. type SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM,
  12. type SolanaError,
  13. } from '@solana/kit';
  14. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  15. /** NotRentExempt: Lamport balance below rent-exempt threshold */
  16. export const TOKEN_ERROR__NOT_RENT_EXEMPT = 0x0; // 0
  17. /** InsufficientFunds: Insufficient funds */
  18. export const TOKEN_ERROR__INSUFFICIENT_FUNDS = 0x1; // 1
  19. /** InvalidMint: Invalid Mint */
  20. export const TOKEN_ERROR__INVALID_MINT = 0x2; // 2
  21. /** MintMismatch: Account not associated with this Mint */
  22. export const TOKEN_ERROR__MINT_MISMATCH = 0x3; // 3
  23. /** OwnerMismatch: Owner does not match */
  24. export const TOKEN_ERROR__OWNER_MISMATCH = 0x4; // 4
  25. /** FixedSupply: Fixed supply */
  26. export const TOKEN_ERROR__FIXED_SUPPLY = 0x5; // 5
  27. /** AlreadyInUse: Already in use */
  28. export const TOKEN_ERROR__ALREADY_IN_USE = 0x6; // 6
  29. /** InvalidNumberOfProvidedSigners: Invalid number of provided signers */
  30. export const TOKEN_ERROR__INVALID_NUMBER_OF_PROVIDED_SIGNERS = 0x7; // 7
  31. /** InvalidNumberOfRequiredSigners: Invalid number of required signers */
  32. export const TOKEN_ERROR__INVALID_NUMBER_OF_REQUIRED_SIGNERS = 0x8; // 8
  33. /** UninitializedState: State is unititialized */
  34. export const TOKEN_ERROR__UNINITIALIZED_STATE = 0x9; // 9
  35. /** NativeNotSupported: Instruction does not support native tokens */
  36. export const TOKEN_ERROR__NATIVE_NOT_SUPPORTED = 0xa; // 10
  37. /** NonNativeHasBalance: Non-native account can only be closed if its balance is zero */
  38. export const TOKEN_ERROR__NON_NATIVE_HAS_BALANCE = 0xb; // 11
  39. /** InvalidInstruction: Invalid instruction */
  40. export const TOKEN_ERROR__INVALID_INSTRUCTION = 0xc; // 12
  41. /** InvalidState: State is invalid for requested operation */
  42. export const TOKEN_ERROR__INVALID_STATE = 0xd; // 13
  43. /** Overflow: Operation overflowed */
  44. export const TOKEN_ERROR__OVERFLOW = 0xe; // 14
  45. /** AuthorityTypeNotSupported: Account does not support specified authority type */
  46. export const TOKEN_ERROR__AUTHORITY_TYPE_NOT_SUPPORTED = 0xf; // 15
  47. /** MintCannotFreeze: This token mint cannot freeze accounts */
  48. export const TOKEN_ERROR__MINT_CANNOT_FREEZE = 0x10; // 16
  49. /** AccountFrozen: Account is frozen */
  50. export const TOKEN_ERROR__ACCOUNT_FROZEN = 0x11; // 17
  51. /** MintDecimalsMismatch: The provided decimals value different from the Mint decimals */
  52. export const TOKEN_ERROR__MINT_DECIMALS_MISMATCH = 0x12; // 18
  53. /** NonNativeNotSupported: Instruction does not support non-native tokens */
  54. export const TOKEN_ERROR__NON_NATIVE_NOT_SUPPORTED = 0x13; // 19
  55. export type TokenError =
  56. | typeof TOKEN_ERROR__ACCOUNT_FROZEN
  57. | typeof TOKEN_ERROR__ALREADY_IN_USE
  58. | typeof TOKEN_ERROR__AUTHORITY_TYPE_NOT_SUPPORTED
  59. | typeof TOKEN_ERROR__FIXED_SUPPLY
  60. | typeof TOKEN_ERROR__INSUFFICIENT_FUNDS
  61. | typeof TOKEN_ERROR__INVALID_INSTRUCTION
  62. | typeof TOKEN_ERROR__INVALID_MINT
  63. | typeof TOKEN_ERROR__INVALID_NUMBER_OF_PROVIDED_SIGNERS
  64. | typeof TOKEN_ERROR__INVALID_NUMBER_OF_REQUIRED_SIGNERS
  65. | typeof TOKEN_ERROR__INVALID_STATE
  66. | typeof TOKEN_ERROR__MINT_CANNOT_FREEZE
  67. | typeof TOKEN_ERROR__MINT_DECIMALS_MISMATCH
  68. | typeof TOKEN_ERROR__MINT_MISMATCH
  69. | typeof TOKEN_ERROR__NATIVE_NOT_SUPPORTED
  70. | typeof TOKEN_ERROR__NON_NATIVE_HAS_BALANCE
  71. | typeof TOKEN_ERROR__NON_NATIVE_NOT_SUPPORTED
  72. | typeof TOKEN_ERROR__NOT_RENT_EXEMPT
  73. | typeof TOKEN_ERROR__OVERFLOW
  74. | typeof TOKEN_ERROR__OWNER_MISMATCH
  75. | typeof TOKEN_ERROR__UNINITIALIZED_STATE;
  76. let tokenErrorMessages: Record<TokenError, string> | undefined;
  77. if (process.env.NODE_ENV !== 'production') {
  78. tokenErrorMessages = {
  79. [TOKEN_ERROR__ACCOUNT_FROZEN]: `Account is frozen`,
  80. [TOKEN_ERROR__ALREADY_IN_USE]: `Already in use`,
  81. [TOKEN_ERROR__AUTHORITY_TYPE_NOT_SUPPORTED]: `Account does not support specified authority type`,
  82. [TOKEN_ERROR__FIXED_SUPPLY]: `Fixed supply`,
  83. [TOKEN_ERROR__INSUFFICIENT_FUNDS]: `Insufficient funds`,
  84. [TOKEN_ERROR__INVALID_INSTRUCTION]: `Invalid instruction`,
  85. [TOKEN_ERROR__INVALID_MINT]: `Invalid Mint`,
  86. [TOKEN_ERROR__INVALID_NUMBER_OF_PROVIDED_SIGNERS]: `Invalid number of provided signers`,
  87. [TOKEN_ERROR__INVALID_NUMBER_OF_REQUIRED_SIGNERS]: `Invalid number of required signers`,
  88. [TOKEN_ERROR__INVALID_STATE]: `State is invalid for requested operation`,
  89. [TOKEN_ERROR__MINT_CANNOT_FREEZE]: `This token mint cannot freeze accounts`,
  90. [TOKEN_ERROR__MINT_DECIMALS_MISMATCH]: `The provided decimals value different from the Mint decimals`,
  91. [TOKEN_ERROR__MINT_MISMATCH]: `Account not associated with this Mint`,
  92. [TOKEN_ERROR__NATIVE_NOT_SUPPORTED]: `Instruction does not support native tokens`,
  93. [TOKEN_ERROR__NON_NATIVE_HAS_BALANCE]: `Non-native account can only be closed if its balance is zero`,
  94. [TOKEN_ERROR__NON_NATIVE_NOT_SUPPORTED]: `Instruction does not support non-native tokens`,
  95. [TOKEN_ERROR__NOT_RENT_EXEMPT]: `Lamport balance below rent-exempt threshold`,
  96. [TOKEN_ERROR__OVERFLOW]: `Operation overflowed`,
  97. [TOKEN_ERROR__OWNER_MISMATCH]: `Owner does not match`,
  98. [TOKEN_ERROR__UNINITIALIZED_STATE]: `State is unititialized`,
  99. };
  100. }
  101. export function getTokenErrorMessage(code: TokenError): string {
  102. if (process.env.NODE_ENV !== 'production') {
  103. return (tokenErrorMessages as Record<TokenError, string>)[code];
  104. }
  105. return 'Error message not available in production bundles.';
  106. }
  107. export function isTokenError<TProgramErrorCode extends TokenError>(
  108. error: unknown,
  109. transactionMessage: {
  110. instructions: Record<number, { programAddress: Address }>;
  111. },
  112. code?: TProgramErrorCode
  113. ): error is SolanaError<typeof SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM> &
  114. Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> {
  115. return isProgramError<TProgramErrorCode>(
  116. error,
  117. transactionMessage,
  118. TOKEN_PROGRAM_ADDRESS,
  119. code
  120. );
  121. }