0

I have a few dozen "position" accounts for a user's positions in my AMM program.

I previously had been getting these with a getProgramAccounts filter, which I understand is not very performant. So I then moved on to below:

When I try to grab these with ".fetchMultiple" it throws an error, despite the docs which indicate that missing or invalid accounts will return null among successful returns.

Secondly and perhaps more importantly, when I ".fetch" accounts individually, which I know to exist and are correct, it will take a few tries before returning the correct account data. Initially a large portion of these will return "Invalid Account Discriminator".

What could be causing these issues? Any insight would be much appreciated.

1 Answer 1

3

Have you changed the name of your account type?

Anchor adds a discriminant when an account is initialized. Suppose you have the following account type in your AMM program:

#[account]
pub struct Position {
   ...

Now, whenever you will initialize an account of type Position, Anchor will generate a SHA-256 hash of the input - "account:Position" and save the first 8 bytes of the hash in the account.

If you later on change the name of the account type to let say, AmmPosition, the accounts generated subsequent to the change will have a different discriminant than the previous ones.

After the above changes are reflected in the IDL, you will now use something like this to fetch the accounts:

const accounts = await program.account.ammPosition.fetchMultiple([add1, add2, ..])

But since the old accounts have a different discriminant, you are getting an error.


Using getProgramAccounts on the other hand won't have the above issue because the change won't affect the account size (the number of bytes remains the same) and all accounts will have the same fields, that's why it works.

1
  • Very helpful information, thanks, though I'm fairly sure I haven't changed the name of the account.
    – joebuild
    Commented Aug 23, 2023 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.