#solana #blockchain #math #safe #primitive #wrapper #type

solana-safe-math

Safe Math wrapper for the primitive numberic types used in a Solana program

1 unstable release

0.1.0 Jan 31, 2022

#346 in #safe

41 downloads per month

Custom license

6KB
69 lines

Solana Safe Math

 use solana_safe_math::{SafeMath};
 
 fn process_init_escrow(
   accounts: &[AccountInfo],
   amount: u64,
   program_id: &Pubkey
 ) -> ProgramResult {
   let val = 10_u64;
  
   val.safe_add(amount)?;
   val.safe_sub(amount)?;
   val.safe_mul(amount)?;
   val.safe_div(amount)?;
   val.safe_pow(8_u32)?;
 }

Works with u128, u64, u32, u16 and u8


lib.rs:

Anchor Safe Math

anchor_safe_math is a collection of helper numeric operation functions that removes the verbosity of checking for overflow, underflow and division by zero errors.

Examples

use solana_safe_math::{SafeMath};
use solana_program::{entrypoint::ProgramResult};

fn process_init_escrow(
  accounts: &[AccountInfo],
  amount: u64,
  program_id: &Pubkey
) -> ProgramResult {
  let val = 10_u64;
 
  val.safe_add(amount)?;
  val.safe_sub(amount)?;
  val.safe_mul(amount)?;
  val.safe_div(amount)?;
  val.safe_pow(8_u32)?;
}

Dependencies

~16–25MB
~416K SLoC