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
use crate::Error;
use eyre::{Result, WrapErr};
use std::{
convert::{TryFrom, TryInto},
fmt::{self, Debug, Display},
str::FromStr,
};
pub const PRECISION: u32 = 18;
pub const FRACTIONAL_DIGITS_MAX: u64 = 9_999_999_999_999_999_999;
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct Decimal(rust_decimal::Decimal);
impl Decimal {
pub fn new(integral_digits: i64, fractional_digits: u64) -> Result<Self> {
if fractional_digits > FRACTIONAL_DIGITS_MAX {
return Err(Error::Decimal).wrap_err_with(|| {
format!(
"fractional digits exceed available precision: {}",
fractional_digits
)
});
}
let integral_digits: rust_decimal::Decimal = integral_digits.into();
let fractional_digits: rust_decimal::Decimal = fractional_digits.into();
let precision_exp: rust_decimal::Decimal = 10u64.pow(PRECISION).into();
let mut combined_decimal = (integral_digits * precision_exp) + fractional_digits;
combined_decimal.set_scale(PRECISION)?;
Ok(Decimal(combined_decimal))
}
pub fn to_amino_bytes(mut self) -> Vec<u8> {
self.0
.set_scale(0)
.expect("can't rescale decimal for Amino serialization");
self.to_string().into_bytes()
}
}
impl Debug for Decimal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl Display for Decimal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for Decimal {
type Err = eyre::Report;
fn from_str(s: &str) -> Result<Self> {
s.parse::<rust_decimal::Decimal>()?.try_into()
}
}
impl TryFrom<rust_decimal::Decimal> for Decimal {
type Error = eyre::Report;
fn try_from(mut decimal_value: rust_decimal::Decimal) -> Result<Self> {
match decimal_value.scale() {
0 => {
let exp: rust_decimal::Decimal = 10u64.pow(PRECISION).into();
decimal_value *= exp;
decimal_value.set_scale(PRECISION)?;
}
PRECISION => (),
other => {
return Err(Error::Decimal).wrap_err_with(|| {
format!("invalid decimal precision: {} (must be 0 or 18)", other)
})
}
}
Ok(Decimal(decimal_value))
}
}
macro_rules! impl_from_primitive_int_for_decimal {
($($int:ty),+) => {
$(impl From<$int> for Decimal {
fn from(num: $int) -> Decimal {
Decimal::new(num as i64, 0).unwrap()
}
})+
};
}
impl_from_primitive_int_for_decimal!(i8, i16, i32, i64, isize);
impl_from_primitive_int_for_decimal!(u8, u16, u32, u64, usize);
#[cfg(test)]
mod tests {
use super::Decimal;
#[test]
fn string_serialization_test() {
let num = Decimal::from(-1i8);
assert_eq!(num.to_string(), "-1.000000000000000000")
}
#[test]
fn amino_serialization_test() {
let num = Decimal::from(-1i8);
assert_eq!(b"-1000000000000000000", num.to_amino_bytes().as_slice());
}
}