pfc_steak/
lib.rs

1pub mod hub;
2pub mod hub_tf;
3
4// this was copied from eris-staking's branch of STEAK.
5//
6mod decimal_checked_ops {
7    use std::{convert::TryInto, str::FromStr};
8
9    use cosmwasm_std::{Decimal, Decimal256, Fraction, OverflowError, StdError, Uint128, Uint256};
10
11    // pub trait Decimal256CheckedOps {
12    //     fn to_decimal(self) -> Result<Decimal, StdError>;
13    // }
14
15    // impl Decimal256CheckedOps for Decimal256 {
16    //     fn to_decimal(self) -> Result<Decimal, StdError> {
17    //         let U256(ref arr) = self.0;
18    //         if arr[2] == 0u64 || arr[3] == 0u64 {
19    //             return Err(StdError::generic_err(
20    //                 "overflow error by casting decimal256 to decimal",
21    //             ));
22    //         }
23    //         Decimal::from_str(&self.to_string())
24    //     }
25    // }
26
27    pub trait DecimalCheckedOps {
28        fn checked_add(self, other: Decimal) -> Result<Decimal, StdError>;
29        fn checked_mul_uint(self, other: Uint128) -> Result<Uint128, StdError>;
30        fn to_decimal256(self) -> Decimal256;
31    }
32
33    impl DecimalCheckedOps for Decimal {
34        fn checked_add(self, other: Decimal) -> Result<Decimal, StdError> {
35            self.numerator()
36                .checked_add(other.numerator())
37                .map(|_| self + other)
38                .map_err(StdError::overflow)
39        }
40
41        fn checked_mul_uint(self, other: Uint128) -> Result<Uint128, StdError> {
42            if self.is_zero() || other.is_zero() {
43                return Ok(Uint128::zero());
44            }
45            let multiply_ratio =
46                other.full_mul(self.numerator()) / Uint256::from(self.denominator());
47            if multiply_ratio > Uint256::from(Uint128::MAX) {
48                Err(StdError::overflow(OverflowError::new(
49                    cosmwasm_std::OverflowOperation::Mul,
50                    self,
51                    other,
52                )))
53            } else {
54                Ok(multiply_ratio.try_into().unwrap())
55            }
56        }
57
58        fn to_decimal256(self) -> Decimal256 {
59            Decimal256::from_str(&self.to_string()).unwrap()
60        }
61    }
62}
63
64pub use decimal_checked_ops::DecimalCheckedOps;