0

I want to create an instruction where it is only possible to initialize a new PDA account if some parameter on another PDA account (same type) is fulfilled. So I was thinking that init_if_needed would be the best option for such an use-case.

In the case that the old account does exist, would I still be able to match on the correct bump when using init_if_needed - referring to old_acc:

    #[account(
        init_if_needed
        seeds = [seeds],
        bump = acc.bump,
        payer = payer
        space = SPACE
    )]
    pub old_acc
    
    #[account(
        init
        seeds = [seeds],
        bump,
        payer = payer
        space = SPACE
    )]
    pub new_acc

In the normal init i would just pass in "bump" without any assignment. Although not sure if I could still check for the correct bump if the account already exists.

So the question is how to account for the bumb in the case of account existence and non-existence

1 Answer 1

2

You should just omit the bump assertion (bump, instead of bump = ..., and let the Anchor generated code manage the calculation of it. There's no way to determine whether the bump assertion should exist based on account existence.

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.