Skip to content

Instantly share code, notes, and snippets.

@yuhcee
Created August 28, 2025 16:00
Show Gist options
  • Save yuhcee/8dcdd6c821da5b69eb0e618abfe954a6 to your computer and use it in GitHub Desktop.
Save yuhcee/8dcdd6c821da5b69eb0e618abfe954a6 to your computer and use it in GitHub Desktop.
A basic Anchor calculator program
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod anchor_calculator {
use super::*;
pub fn add(ctx: Context<Calculator>, a: i64, b: i64) -> Result<()> {
let result = a + b;
msg!("Result: {}", result);
Ok(())
}
pub fn subtract(ctx: Context<Calculator>, a: i64, b: i64) -> Result<()> {
let result = a - b;
msg!("Result: {}", result);
Ok(())
}
pub fn multiply(ctx: Context<Calculator>, a: i64, b: i64) -> Result<()> {
let result = a * b;
msg!("Result: {}", result);
Ok(())
}
pub fn divide(ctx: Context<Calculator>, a: i64, b: i64) -> Result<()> {
if b == 0 {
return err!(CalculatorError::DivisionByZero);
}
let result = a / b;
msg!("Result: {}", result);
Ok(())
}
}
#[derive(Accounts)]
pub struct Calculator {}
#[error_code]
pub enum CalculatorError {
#[msg("Division by zero is not allowed.")]
DivisionByZero,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment