astroport_types/
lib.rs

1pub mod asset;
2pub mod common;
3pub mod factory;
4pub mod generator;
5pub mod generator_proxy;
6pub mod maker;
7pub mod oracle;
8pub mod pair;
9pub mod querier;
10pub mod router;
11pub mod staking;
12pub mod token;
13pub mod vesting;
14pub mod xastro_token;
15
16#[cfg(test)]
17mod mock_querier;
18
19#[cfg(test)]
20mod testing;
21
22#[allow(clippy::all)]
23mod uints {
24    use uint::construct_uint;
25    construct_uint! {
26        pub struct U256(4);
27    }
28}
29
30mod decimal_checked_ops {
31    use cosmwasm_std::{Decimal, Fraction, OverflowError, Uint128, Uint256};
32    use std::convert::TryInto;
33    pub trait DecimalCheckedOps {
34        fn checked_add(self, other: Decimal) -> Result<Decimal, OverflowError>;
35        fn checked_mul(self, other: Uint128) -> Result<Uint128, OverflowError>;
36    }
37
38    impl DecimalCheckedOps for Decimal {
39        fn checked_add(self, other: Decimal) -> Result<Decimal, OverflowError> {
40            self.numerator()
41                .checked_add(other.numerator())
42                .map(|_| self + other)
43        }
44        fn checked_mul(self, other: Uint128) -> Result<Uint128, OverflowError> {
45            if self.is_zero() || other.is_zero() {
46                return Ok(Uint128::zero());
47            }
48            let multiply_ratio =
49                other.full_mul(self.numerator()) / Uint256::from(self.denominator());
50            if multiply_ratio > Uint256::from(Uint128::MAX) {
51                Err(OverflowError::new(
52                    cosmwasm_std::OverflowOperation::Mul,
53                    self,
54                    other,
55                ))
56            } else {
57                Ok(multiply_ratio.try_into().unwrap())
58            }
59        }
60    }
61}
62
63pub use decimal_checked_ops::DecimalCheckedOps;
64pub use uints::U256;