1

I have this code:

#[derive(Accounts)]
#[instruction(campaign_id: u32)]
pub struct CreateCompaign<'info> {
    #[account(
        mut,
        seeds = [SEED.as_bytes(), &compaign_id.to_le_bytes()],
        bump,
    )]
    pub campaign: Account<'info, Campaign>,
   // ....
}

what is this #[instruction(campaign_id: u32)] how does it work? I googled it, and searched it on this platform but could not see anything. (I called it as macro but I am not sure if it is a macro)

1 Answer 1

2

It is a macro, and it lets you access your instruction arguments in your validator(i.e Accounts struct). Here for instance, without the instruction macro, you wouldn't be able to write the check for the campaign pda's seeds.

So here the #[instruction(campaign_id: u32)] line says "Get the value from the instruction arguments for use in the validator". That way the check for the 2nd seed of campaign is bound to your instruction argument

4
  • the problem is there is no instruction on the front-end
    – Yilmaz
    Feb 25, 2023 at 6:01
  • what do you mean by that? There isn't any method with a ctx of Context<CreateCampaign> in your anchor program module?
    – Ademola
    Feb 25, 2023 at 10:30
  • if you have #[instruction(campaign_id: u32)] in your program, you are telling it that your instruction with Context<CreateCampaign> should also have a campaign_id passed. your Campaign PDA is seeded with this id, so it's necessary to pass from the front end.
    – AMilz
    Mar 1, 2023 at 15:17
  • 1
    In case you want to poke around the macro definition, you can find it at github.com/coral-xyz/anchor/blob/master/lang/syn/src/codegen/…
    – Jon C
    Mar 6, 2023 at 19:51

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.