Superteam BrasilSuperteam BrasilSuperteam Academy
CoursesDashboardLeaderboard
HomeCoursesDashboardLeaderboardProfileSettings
Superteam BrasilSuperteam Brasil

Superteam Brasil Academy — learn to build production dApps on Solana.

Built on Solana

Platform

  • Courses
  • Dashboard
  • Leaderboard

Resources

  • Solana Fundamentals
  • Anchor 101
  • Security Auditing

Community

  • Builder Profile
  • Top Learners
  • Preferences

Legal

  • Terms
  • Privacy
  • Code of Conduct
Built for the Superteam Brazil ecosystem© 2026 Superteam Academy

Anchor Framework 101

Errors and Events

Define custom errors and emit events for off-chain indexing.

Content
Lesson 3 of 3100%

Lesson content

Custom Errors

#[error_code]
pub enum ErrorCode {
    #[msg("Course is not active")]
    CourseNotActive,
    #[msg("Lesson already completed")]
    LessonAlreadyCompleted,
    #[msg("Arithmetic overflow")]
    Overflow,
}

Use errors in constraints: @ ErrorCode::CourseNotActive

Or in handlers: require!(condition, ErrorCode::Overflow)

Events

Events are emitted via transaction logs and indexed off-chain:

#[event]
pub struct LessonCompleted {
    pub learner: Pubkey,
    pub course: Pubkey,
    pub lesson_index: u8,
    pub xp_earned: u64,
    pub timestamp: i64,
}

// In handler:
emit!(LessonCompleted {
    learner: ctx.accounts.learner.key(),
    course: ctx.accounts.course.key(),
    lesson_index,
    xp_earned: course.xp_per_lesson as u64,
    timestamp: Clock::get()?.unix_timestamp,
});

Client-Side Listening

program.addEventListener("LessonCompleted", (event) => {
  console.log("Lesson completed:", event.lessonIndex);
});
Previous