1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::str::FromStr;

use bigint::U256;
use cosmwasm_std::{Uint128, Uint256};
use schemars::JsonSchema;
// pub struct FPDecimal(#[schemars(with = "String")] pub i128);

#[allow(clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Default, Debug, Eq, JsonSchema)]
pub struct FPDecimal {
    #[schemars(with = "String")]
    pub num: U256,
    pub sign: i8,
}

impl From<u128> for FPDecimal {
    fn from(x: u128) -> FPDecimal {
        FPDecimal {
            num: U256::from_little_endian(&x.to_le_bytes()) * FPDecimal::ONE.num,
            sign: 1,
        }
    }
}

impl From<i128> for FPDecimal {
    fn from(x: i128) -> FPDecimal {
        let mut sign = 1;
        if x < 0 {
            sign = 0;
        }

        let abs_x: u128 = x.unsigned_abs();
        FPDecimal {
            num: U256::from_little_endian(&abs_x.to_le_bytes()) * FPDecimal::ONE.num,
            sign,
        }
    }
}

impl From<FPDecimal> for u128 {
    fn from(x: FPDecimal) -> u128 {
        let num: U256 = x.int().num / FPDecimal::ONE.num;
        if num.bits() > 128 {
            panic!("overflow");
        }

        let mut array: [u8; 32] = [0; 32];
        num.to_little_endian(&mut array);

        let mut arr2: [u8; 16] = Default::default();
        arr2.copy_from_slice(&array[0..16]);
        u128::from_le_bytes(arr2)
    }
}

impl From<U256> for FPDecimal {
    fn from(x: U256) -> FPDecimal {
        FPDecimal { num: x, sign: 1 }
    }
}

impl From<FPDecimal> for Uint128 {
    fn from(x: FPDecimal) -> Uint128 {
        let number: u128 = x.into();
        number.into()
    }
}

impl From<Uint128> for FPDecimal {
    fn from(x: Uint128) -> FPDecimal {
        FPDecimal::from_str(&x.to_string()).unwrap()
    }
}

impl From<Uint256> for FPDecimal {
    fn from(x: Uint256) -> FPDecimal {
        FPDecimal::from_str(&x.to_string()).unwrap()
    }
}

// #[cfg(not(target_arch = "wasm32"))]
// impl convert::From<FPDecimal> for f32 {
//     fn from(x: FPDecimal) -> f32 {
//         f32::from_str(&x.to_string()).unwrap()
//     }
// }

impl FPDecimal {
    pub const MAX: FPDecimal = FPDecimal { num: U256::MAX, sign: 1 };
    pub const MIN: FPDecimal = FPDecimal { num: U256::MAX, sign: 0 };
    pub const DIGITS: usize = 18;
    pub const ZERO: FPDecimal = FPDecimal {
        num: U256([0, 0, 0, 0]),
        sign: 1,
    };
    pub const ONE: FPDecimal = FPDecimal {
        num: U256([1_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };

    pub const TWO: FPDecimal = FPDecimal {
        num: U256([2_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };

    pub const THREE: FPDecimal = FPDecimal {
        num: U256([3_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };

    pub const FOUR: FPDecimal = FPDecimal {
        num: U256([4_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };

    pub const FIVE: FPDecimal = FPDecimal {
        num: U256([5_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };
    pub const SIX: FPDecimal = FPDecimal {
        num: U256([6_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };
    pub const SEVEN: FPDecimal = FPDecimal {
        num: U256([7_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };
    pub const EIGHT: FPDecimal = FPDecimal {
        num: U256([8_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };
    pub const NINE: FPDecimal = FPDecimal {
        num: U256([9_000_000_000_000_000_000, 0, 0, 0]),
        sign: 1,
    };

    pub const MUL_PRECISION: FPDecimal = FPDecimal {
        num: U256([1_000_000_000, 0, 0, 0]),
        sign: 1,
    };
    pub const E_10: FPDecimal = FPDecimal {
        num: U256([1053370797511887454u64, 1194u64, 0, 0]),
        sign: 1,
    }; // e^10
    pub const E: FPDecimal = FPDecimal {
        num: U256([2718281828459045235, 0, 0, 0]),
        sign: 1,
    };
    pub const LN_10: FPDecimal = FPDecimal {
        num: U256([2302585092994045684, 0, 0, 0]),
        sign: 1,
    }; // ln(10)
    pub const LN_1_5: FPDecimal = FPDecimal {
        num: U256([405465108108164382, 0, 0, 0]),
        sign: 1,
    }; // ln(1.5)
    pub const PI: FPDecimal = FPDecimal {
        num: U256([3_141_592_653_589_793_238, 0, 0, 0]),
        sign: 1,
    };

    pub const fn one() -> FPDecimal {
        FPDecimal::ONE
    }

    pub const fn zero() -> FPDecimal {
        FPDecimal::ZERO
    }

    pub fn is_zero(&self) -> bool {
        self.num.is_zero()
    }

    pub const fn max() -> FPDecimal {
        FPDecimal::MAX
    }

    pub const fn min() -> FPDecimal {
        FPDecimal::MIN
    }

    pub const fn e() -> FPDecimal {
        FPDecimal::E
    }

    pub fn _int(x: FPDecimal) -> FPDecimal {
        let x1 = x.num;
        let x1_1 = x1 / FPDecimal::ONE.num;
        let x_final = x1_1 * FPDecimal::ONE.num;
        FPDecimal { num: x_final, sign: x.sign }
    }

    pub fn int(&self) -> FPDecimal {
        FPDecimal::_int(*self)
    }
    pub fn is_int(&self) -> bool {
        *self == self.int()
    }

    pub fn _sign(x: FPDecimal) -> i8 {
        x.sign
    }

    pub fn _fraction(x: FPDecimal) -> FPDecimal {
        let x1 = x.num;
        FPDecimal {
            num: x1 - FPDecimal::_int(x).num,
            sign: x.sign,
        }
    }

    pub fn fraction(&self) -> FPDecimal {
        FPDecimal::_fraction(*self)
    }
}

mod arithmetic;
mod comparison;
mod display;
mod exp;
mod factorial;
mod from_str;
mod hyper;
mod log;
mod serde;
mod trigonometry; // cosmwasm serialization