Search code examples
solanaanchor-solana

What does "invalid account discriminator" mean in Anchor?


I'm trying to fetch an account from a public key:

await program.account.myAccountType.fetch(somePubkey);

But then Anchor throws an error saying:

Invalid account discriminator

What is an account discriminator?


Solution

  • An account discriminator is few bytes that Anchor puts at the front of an account, like a header. It lets anchor know what type of account it should deserialize the data as.

    This error happens if you try to to fetch something as myAccountType, but it's actually a pubkey for some other account, like a Token Account, or another account within your program.

    Here's some things you could try:

    • console logging somePubkey and putting it into the explorer
    • making extra double sure that you meant program.account.myAccountType and not program.account.someOtherAccountType.