I'm trying to write a simple Solana Program using Rust/Anchor which uses a PDA.
Here is the Program code:
use anchor_lang::prelude::*;
declare_id!("51v31qHaEQniLoYuvvtXByZcfiyvog3R2EKC39EPD52p");
#[program]
pub mod solana_sandbox {
use super::*;
pub fn initialize(ctx: Context<Initialize>, bump: u8) -> ProgramResult {
ctx.accounts.sandbox_account.bump = bump;
Ok(())
}
}
#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct Initialize<'info> {
#[account(mut)]
pub signer: Signer<'info>,
#[account(
init,
seeds = [b"seed".as_ref()],
bump,
payer = signer,
)]
pub sandbox_account: Account<'info, SandboxAccount>,
pub system_program: Program<'info, System>,
}
#[account]
#[derive(Default)]
pub struct SandboxAccount {
pub bump: u8,
}
Here is the client code:
const [sandboxPda, sandboxBump] = await PublicKey.findProgramAddress([Buffer.from('seed')], this.program.programId);
await program.rpc.initialize(
sandboxBump,
{
accounts: {
signer: keypair.publicKey,
sandboxAccount: sandboxPda,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: [keypair],
instructions: []
});
It works correctly, but I have a doubt. I get sandboxBump from findProgramAddress and input this but it isn't used.
If I set bump when init like this :
#[account(
init,
seeds = [b"seed".as_ref()],
bump = bump,
payer = signer,
)]
Error occurred. So I delete the instruction macro from the program code but it still works correctly. So is bump value no need when PDA initialization or does anchor system use it automatically?
Appreciate any help!
