solana_maths/
common.rs

1//! Common module for Decimal and Rate
2
3use solana_program::program_error::ProgramError;
4
5/// Scale of precision
6pub const SCALE: usize = 18;
7/// Identity
8pub const WAD: u64 = 1_000_000_000_000_000_000;
9/// Half of identity
10pub const HALF_WAD: u64 = 500_000_000_000_000_000;
11/// Scale for percentages
12pub const PERCENT_SCALER: u64 = 10_000_000_000_000_000;
13
14/// Scale for bips
15pub const BIPS_SCALER: u64 = PERCENT_SCALER / 100;
16
17/// Try to subtract, return an error on underflow
18pub trait TrySub: Sized {
19    /// Subtract
20    fn try_sub(self, rhs: Self) -> Result<Self, ProgramError>;
21}
22
23/// Try to subtract, return an error on overflow
24pub trait TryAdd: Sized {
25    /// Add
26    fn try_add(self, rhs: Self) -> Result<Self, ProgramError>;
27}
28
29/// Try to divide, return an error on overflow or divide by zero
30pub trait TryDiv<RHS>: Sized {
31    /// Divide
32    fn try_div(self, rhs: RHS) -> Result<Self, ProgramError>;
33}
34
35/// Try to multiply, return an error on overflow
36pub trait TryMul<RHS>: Sized {
37    /// Multiply
38    fn try_mul(self, rhs: RHS) -> Result<Self, ProgramError>;
39}