1

According to the Anchor Book bump constraint can be specified for a PDA, so Anchor doesn't need to search for it:

#[derive(Accounts)]
pub struct ChangeUserName<'info> {
    pub user: Signer<'info>,
    #[account(mut, seeds = [b"user-stats", user.key().as_ref()], bump = user_stats.bump)]
    pub user_stats: Account<'info, UserStats>,
}

But when I try to use the same approach with AccountLoader:

#[derive(Accounts)]
pub struct ChangeUserName<'info> {
    pub user: Signer<'info>,
    #[account(mut, seeds = [b"user-stats", user.key().as_ref()], bump = user_stats.bump)]
    pub user_stats: AccountLoader<'info, UserStats>,
}

I get an error:

error[E0609]: no field bump on type `anchor_lang::prelude::AccountLoader<'_, UserStats>

Is that a limitation of Anchor or there is a way to fix that code?

2 Answers 2

6

The error indicates that you're trying to access the variable bump on the user_stats account inside the account struct. However because the account type is AccountLoader anchor specifically will not deserialise and load the account data into memory for you to access in the same way as the normal Account<'info, > syntax. This has to do with the fact it's a zero copy account. To access the bump variable you instead need to use the load() method on the account first:

bump = user_stats.load()?.bump

As noted in the other answer, you can also avoid this issue by using the bump keyword without assigning any value to it.

1
  • awesome, I din't know you can call functions inside attributes, seems like a right solution, I'll give it a try
    – ASkibin
    Commented Jul 26, 2022 at 14:34
0

I don't think you need to specify anything beyond bump in the declaration. So there is no need for the =user_stats.bump as it uses the canonical bump by default.

Example:

#[derive(Accounts)]
pub struct ChangeUserName<'info> {
    pub user: Signer<'info>,
    #[account(mut, seeds = [b"user-stats", user.key().as_ref()], bump)]
    pub user_stats: AccountLoader<'info, UserStats>,
}
1
  • I know that it works with the canonical bump, but I don't want Anchor to search for the PDA address wasting compute units
    – ASkibin
    Commented Jul 26, 2022 at 14:30

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.