solend_sdk/math/
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/// Try to subtract, return an error on underflow
15pub trait TrySub: Sized {
16    /// Subtract
17    fn try_sub(self, rhs: Self) -> Result<Self, ProgramError>;
18}
19
20/// Try to subtract, return an error on overflow
21pub trait TryAdd: Sized {
22    /// Add
23    fn try_add(self, rhs: Self) -> Result<Self, ProgramError>;
24}
25
26/// Try to divide, return an error on overflow or divide by zero
27pub trait TryDiv<RHS>: Sized {
28    /// Divide
29    fn try_div(self, rhs: RHS) -> Result<Self, ProgramError>;
30}
31
32/// Try to multiply, return an error on overflow
33pub trait TryMul<RHS>: Sized {
34    /// Multiply
35    fn try_mul(self, rhs: RHS) -> Result<Self, ProgramError>;
36}