Skip to content

Safe Math wrapper for the native primitive numeric types used in an Anchor project

License

Notifications You must be signed in to change notification settings

Apocentre/anchor-safe-math

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Anchor Safe Math

use anchor_lang::prelude::*;
use safe_math::{SafeMath};

#[program]
pub mod example {
  use super::*;

  pub fn instruction(ctx: Context<Instruction>, amount: u64) -> ProgramResult {
    let state = &mut ctx.accounts.state;

    // You can apply any of the following operations
    state.total_amount = state.total_amount.safe_add(amount)?;
    state.total_amount = state.total_amount.safe_sub(amount)?;
    state.total_amount = state.total_amount.safe_mul(amount)?;
    state.total_amount = state.total_amount.safe_div(amount)?;
    state.total_amount = state.total_amount.safe_pow(8_u32)?;
  }
}

#[derive(Accounts)]
pub struct Instruction<'info> {
  ...
}

Works with u128, u64, u32, u16 and u8

About

Safe Math wrapper for the native primitive numeric types used in an Anchor project

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages