Soldev

Solana errors

Last updated:

Errors in Solana can be difficult to interpret. When you get a custom error, have to lookup the error code yourself.

https://github.com/coral-xyz/anchor/blob/master/lang/src/error.rs#L22

SolanaError: custom program error: #3002

Would be

/// 3002 - Account discriminator did not match what was expected
#[msg("Account discriminator did not match what was expected")]
AccountDiscriminatorMismatch,

Error codes that can be returned by internal framework code are identified by its number range:

User defined errors are anything greater than 6000.

Custom errors

You can define your own custom errors in a module. They increment from 6000 by their order of definition.

use anchor_lang::prelude::*;

#[error_code]
pub enum CustomError {
  // 6000
  #[msg("Maths not mathing")]
  MathOverflow,

  // 60001
  #[msg("Maths still not mathing")]
  MathUnderflow,
}

Token gotcha

I am already using InterfaceAccount, I added the constraint associated_token::mint = mint but it gives An associated constraint was violated when i use a Token2022 mint

use associated_token::token_program = token_program

Out of room on the stack

"Program D4JCMSe8bh1GcuPyGjicJ4JbdcmWmAPLvcuDqgpVSWFB failed: Access violation in unknown section at address 0x109 of size 8"

You ran out of room on the stack. Box your accounts to move them to the heap.

Too many instruction parameters

InstructionDidNotDeserialize

You can't have infinite parameters

Corrupt data on chain

pub struct InstrData {
    pub decimals: u8,
    pub weights: [u32; 10],
    pub inv_T: u128,
}

This struct is missing #[derive(AnchorSerialize, AnchorDeserialize)] so if you try and pass this to an instruction the data won't be what you expect.