I am trying to initialize a PDA using the u64 value stored in another account. The idea is that the stored counter increments and the value determines the seed.
My code:
#[derive(Accounts)]
pub struct PdaInit<'info> {
/// CHECK: Just testing
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
#[account(init, seeds = [b"seeds".as_ref(), stored_counter_pda.counter.to_be_bytes().as_ref()], bump, payer=payer, space = 100)]
init_pda: Account<'info, SomeStruct>,
#[account(mut, seeds = [b"seeds".as_ref()], bump)]
stored_counter_pda: Account<'info, StoredCounter>,
}
#[account]
#[derive(Default)]
pub struct StoredCounter{
pub counter: u64,
}
The problem is that I also have to pass the init_pda from the Client, so I need the seed, and I don't know if fetching the stored value of the other PDA is the correct way to deal with this. Supposing that several users on the client side send the instruction to initialize the pda at the same time, if I fetch the counter, I will get the same value for every user and only one user will get a succesfull result on the transaction.
Any help will be appreciated. Thanks.