1

I want to create an PDA with a seed that could change after i already found the program address on the client side.

pub struct CreatePost<'info> {

    #[account(mut)]
    pub creator: Signer<'info>,

    #[account(
        init,
        payer = creator,
        space = 8 + Post::MAX_SIZE, seeds = [b"post", category.name.as_bytes(), &category.post_count.to_be_bytes()], bump
    )]
    pub post: Account<'info, Post>,

    #[account(
        mut,
        seeds = [b"category", category.name.as_bytes()], bump = category.bump
    )]
    pub category: Account<'info, Category>,

    #[account(
        mut,
        seeds = [b"global"], bump = global.bump
    )]
    pub global: Account<'info, GlobalStats>,
    pub system_program: Program<'info, System>
}

The initialzation of the post Account has the dependency to category.post_count, that could change, after someone else posted.

I'm calculating the PDA on the client like so:

const [postPDA, pdaBump] = await PublicKey.findProgramAddress(
        [
          anchor.utils.bytes.utf8.encode("post"),
          anchor.utils.bytes.utf8.encode("global"),
          Uint8Array.of(...count.toArray('be', 8))
        ],
        programId)

The problem is, that the PDA could have already been created by someone else, after i try to initialize it, resulting in not creating a new PDA.

However i always want to create a new PDA when i call create_post, that will increment after someone else posted.

Do i need to create the PDA manually or is it possible to calculate the seed in Anchor?

1 Answer 1

1

You could make a post_count field for each owner (creator). This way, creator could be one of your seed sources, along with their post count, creating a unique address for each creator with every post.

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.