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
use crate::pb::eth::v2 as pb;
use substreams::scalar::{BigDecimal, BigInt};

impl Into<BigInt> for pb::BigInt {
    fn into(self) -> BigInt {
        BigInt::from_unsigned_bytes_be(self.bytes.as_ref())
    }
}

impl Into<BigDecimal> for pb::BigInt {
    fn into(self) -> BigDecimal {
        let v = BigInt::from_unsigned_bytes_be(self.bytes.as_ref());
        BigDecimal::new(v, 0)
    }
}

pub fn to_option_decimal(v: Option<pb::BigInt>) -> Option<BigDecimal> {
    match v {
        Some(v) => {
            let out: BigDecimal = v.into();
            Some(out)
        }
        None => None,
    }
}

pub fn to_option_bigint(v: Option<pb::BigInt>) -> Option<BigInt> {
    match v {
        Some(v) => Some(v.into()),
        None => None,
    }
}

pub fn to_option_decimal_with_decimal(v: Option<pb::BigInt>, decimal: u32) -> Option<BigDecimal> {
    match v {
        Some(v) => Some(v.with_decimal(decimal)),
        None => None,
    }
}

impl pb::BigInt {
    pub fn with_decimal(self, decimal: u32) -> BigDecimal {
        let num: BigDecimal = self.into();
        let dem = BigInt::from(10 as u32).pow(decimal);
        return num / BigDecimal::from(dem);
    }
}

#[cfg(test)]
mod tests {
    use crate::pb::eth::v2 as pb;
    use crate::scalar::{to_option_bigint, to_option_decimal, to_option_decimal_with_decimal};

    #[test]
    fn zero_into_bigint() {
        let v: substreams::scalar::BigInt = new_pb_bigint(0).into();
        assert_eq!(v.to_u64(), 0);
    }

    #[test]
    fn number_into_bigint() {
        let v: substreams::scalar::BigInt = new_pb_bigint(253).into();
        assert_eq!(v.to_u64(), 253);
    }

    #[test]
    fn zero_into_bigdecmal() {
        let v: substreams::scalar::BigDecimal = new_pb_bigint(0).into();
        assert_eq!(v.to_string(), "0");
    }

    #[test]
    fn number_into_bigdecmal() {
        let v: substreams::scalar::BigDecimal = new_pb_bigint(253).into();
        assert_eq!(v.to_string(), "253");
    }

    #[test]
    fn some_option_pb_to_decimal() {
        let v = Some(new_pb_bigint(253));
        assert_eq!(
            to_option_decimal(v),
            Some(substreams::scalar::BigDecimal::from(253 as u32))
        );
    }

    #[test]
    fn none_option_pb_to_decimal() {
        let v: Option<pb::BigInt> = None;
        assert_eq!(to_option_decimal(v), None);
    }

    #[test]
    fn some_option_pb_to_bigint() {
        let v = Some(new_pb_bigint(253));
        assert_eq!(
            to_option_bigint(v),
            Some(substreams::scalar::BigInt::from(253 as u32))
        );
    }

    #[test]
    fn none_option_pb_to_bigint() {
        let v: Option<pb::BigInt> = None;
        assert_eq!(to_option_bigint(v), None);
    }

    #[test]
    fn with_decimal() {
        let v = new_pb_bigint_bytes(vec![114, 10, 199, 169, 74, 64, 0].as_ref());
        assert_eq!(v.with_decimal(18).to_string(), "0.0321");
    }

    #[test]
    fn some_option_pb_to_bigdecimal_with_decimal() {
        let v = Some(new_pb_bigint_bytes(
            vec![114, 10, 199, 169, 74, 64, 0].as_ref(),
        ));
        let out = substreams::scalar::BigDecimal::try_from("0.0321".to_owned()).unwrap();
        assert_eq!(to_option_decimal_with_decimal(v, 18), Some(out));
    }

    #[test]
    fn none_option_pb_to_bigdecimal_with_decimal() {
        let v: Option<pb::BigInt> = None;
        assert_eq!(to_option_decimal_with_decimal(v, 18), None);
    }

    #[test]
    fn some_option_pb_to_bigdecimal() {
        let v = Some(new_pb_bigint(253));
        assert_eq!(
            to_option_decimal(v),
            Some(substreams::scalar::BigDecimal::from(253 as u32))
        );
    }

    #[test]
    fn none_option_pb_to_bigdecimal() {
        let v: Option<pb::BigInt> = None;
        assert_eq!(to_option_decimal(v), None);
    }

    pub fn new_pb_bigint(value: u32) -> pb::BigInt {
        let v = num_bigint::BigInt::new(num_bigint::Sign::Plus, vec![value]);
        let (_, bytes) = v.to_bytes_be();
        pb::BigInt { bytes }
    }

    pub fn new_pb_bigint_bytes(bytes: &[u8]) -> pb::BigInt {
        let v = num_bigint::BigInt::from_bytes_be(num_bigint::Sign::Plus, bytes);
        let (_, bytes) = v.to_bytes_be();
        pb::BigInt { bytes }
    }
}