1pub mod hub;
2pub mod hub_tf;
3
4mod 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 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;