Daily Challenges
Code Challenges
> Sharpen your Solana skills with daily coding challenges. Race against the clock, earn XP, and climb the speed leaderboard.
Challenge #42
Intermediate
Implement a PDA-based Counter Program
Create a Solana program using Anchor that initializes a counter PDA and provides increment/decrement instructions. The counter should track the authority and enforce access control.
AnchorPDAAccess Control
use anchor_lang::prelude::*;
declare_id!("YourProgramId");
#[program]
pub mod counter {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
// TODO: Initialize the counter account
// Set count to 0 and authority to the signer
Ok(())
}
pub fn increment(ctx: Context<Update>) -> Result<()> {
// TODO: Increment the counter
// Only the authority should be able to increment
Ok(())
}
pub fn decrement(ctx: Context<Update>) -> Result<()> {
// TODO: Decrement the counter
// Prevent underflow (count should not go below 0)
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
// TODO: Define accounts for initialization
}
#[derive(Accounts)]
pub struct Update<'info> {
// TODO: Define accounts for update
}
#[account]
pub struct Counter {
// TODO: Define counter state
}Test Cases
✓ Counter initializes with count = 0
✓ Authority can increment the counter
✓ Authority can decrement the counter
✗ Non-authority cannot modify the counter
✗ Counter cannot go below 0
Progressive Hints
Time Remaining
30:00
200 XP
Reward
127 attempts 43 solved