Created
August 28, 2025 16:00
-
-
Save yuhcee/8dcdd6c821da5b69eb0e618abfe954a6 to your computer and use it in GitHub Desktop.
A basic Anchor calculator program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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