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
use crate::error::{Error, Result};
use crate::util::JsonDeserializer;
use serde_json::Value;
use std::borrow::Borrow;

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct BlockchainRewards {
    height: u32,
    current_reward: u64,
    total_waves_amount: u64,
    min_increment: u64,
    term: u32,
    next_check: u32,
    voting_interval_start: u32,
    voting_interval: u32,
    voting_threshold: u32,
    votes: Votes,
}

#[allow(clippy::too_many_arguments)]
impl BlockchainRewards {
    pub fn new(
        height: u32,
        current_reward: u64,
        total_waves_amount: u64,
        min_increment: u64,
        term: u32,
        next_check: u32,
        voting_interval_start: u32,
        voting_interval: u32,
        voting_threshold: u32,
        votes: Votes,
    ) -> Self {
        Self {
            height,
            current_reward,
            total_waves_amount,
            min_increment,
            term,
            next_check,
            voting_interval_start,
            voting_interval,
            voting_threshold,
            votes,
        }
    }

    pub fn height(&self) -> u32 {
        self.height
    }

    pub fn current_reward(&self) -> u64 {
        self.current_reward
    }

    pub fn total_waves_amount(&self) -> u64 {
        self.total_waves_amount
    }

    pub fn min_increment(&self) -> u64 {
        self.min_increment
    }

    pub fn term(&self) -> u32 {
        self.term
    }

    pub fn next_check(&self) -> u32 {
        self.next_check
    }

    pub fn voting_interval_start(&self) -> u32 {
        self.voting_interval_start
    }

    pub fn voting_interval(&self) -> u32 {
        self.voting_interval
    }

    pub fn voting_threshold(&self) -> u32 {
        self.voting_threshold
    }

    pub fn votes(&self) -> Votes {
        self.votes.clone()
    }
}

impl TryFrom<&Value> for BlockchainRewards {
    type Error = Error;

    fn try_from(value: &Value) -> Result<Self> {
        let height = JsonDeserializer::safe_to_int_from_field(value, "height")?;
        let current_reward = JsonDeserializer::safe_to_int_from_field(value, "currentReward")?;
        let total_waves_amount =
            JsonDeserializer::safe_to_int_from_field(value, "totalWavesAmount")?;
        let min_increment = JsonDeserializer::safe_to_int_from_field(value, "minIncrement")?;
        let term = JsonDeserializer::safe_to_int_from_field(value, "term")?;
        let next_check = JsonDeserializer::safe_to_int_from_field(value, "nextCheck")?;
        let voting_interval_start =
            JsonDeserializer::safe_to_int_from_field(value, "votingIntervalStart")?;
        let voting_interval = JsonDeserializer::safe_to_int_from_field(value, "votingInterval")?;
        let voting_threshold = JsonDeserializer::safe_to_int_from_field(value, "votingThreshold")?;
        let votes: Votes = value["votes"].borrow().try_into()?;

        Ok(BlockchainRewards {
            height: height as u32,
            current_reward: current_reward as u64,
            total_waves_amount: total_waves_amount as u64,
            min_increment: min_increment as u64,
            term: term as u32,
            next_check: next_check as u32,
            voting_interval_start: voting_interval_start as u32,
            voting_interval: voting_interval as u32,
            voting_threshold: voting_threshold as u32,
            votes,
        })
    }
}

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Votes {
    increase: u32,
    decrease: u32,
}

impl Votes {
    pub fn new(increase: u32, decrease: u32) -> Self {
        Self { increase, decrease }
    }

    pub fn increase(&self) -> u32 {
        self.increase
    }

    pub fn decrease(&self) -> u32 {
        self.decrease
    }
}

impl TryFrom<&Value> for Votes {
    type Error = Error;

    fn try_from(value: &Value) -> Result<Self> {
        let increase = JsonDeserializer::safe_to_int_from_field(value, "increase")?;
        let decrease = JsonDeserializer::safe_to_int_from_field(value, "decrease")?;
        Ok(Votes {
            increase: increase as u32,
            decrease: decrease as u32,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::model::BlockchainRewards;
    use serde_json::Value;
    use std::borrow::Borrow;
    use std::fs;

    #[test]
    fn test_json_to_blockchain_rewards() {
        let data = fs::read_to_string("./tests/resources/blockchain/blockchain_rewards.json")
            .expect("Unable to read file");
        let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");

        let rewards_from_json: BlockchainRewards = json.borrow().try_into().unwrap();

        assert_eq!(2224756, rewards_from_json.height());
        assert_eq!(10904654200000000, rewards_from_json.total_waves_amount());
        assert_eq!(600000000, rewards_from_json.current_reward());
        assert_eq!(50000000, rewards_from_json.min_increment());
        assert_eq!(100000, rewards_from_json.term());
        assert_eq!(2316999, rewards_from_json.next_check());
        assert_eq!(2307000, rewards_from_json.voting_interval_start());
        assert_eq!(10000, rewards_from_json.voting_interval());
        assert_eq!(5001, rewards_from_json.voting_threshold());
        assert_eq!(0, rewards_from_json.votes().increase());
        assert_eq!(1, rewards_from_json.votes().decrease());
    }
}