Atrás
+20 XP
10/16
Challenge: Build the Increment
Add an increment instruction to the counter program.
Requirements
- Add a
pub fn incrementinside the#[program]module - It should take
ctx: Context<Increment>and returnResult<()> - Get a mutable reference to
ctx.accounts.counterand add 1 tocount - Add an
Incrementaccounts struct withcounter(PDA,mut, same seeds as Initialize) anduser(Signer)
Why user is needed in Increment
The counter is a PDA derived from [b"counter", user.key()]. To verify the PDA matches, Anchor needs the user account to re-derive the seeds. This also acts as authorization — only the wallet that matches the seeds can modify this counter.
Tips
- The
Incrementstruct needs the<'info>lifetime, just likeInitialize - The counter field needs
seedsandbump(same seeds as Initialize, but noinit) - The
userfield isSigner<'info>(not mut — they don't pay anything here) - Don't change the existing
initializeorgreetinstructions
Casos de Prueba
Program compiles successfully
Entrada:
Esperado: trueCode contains an increment function
Entrada:
Esperado: true