waves_rust/model/rs/
blockchain.rs1use crate::error::{Error, Result};
2use crate::util::JsonDeserializer;
3use serde_json::Value;
4use std::borrow::Borrow;
5
6#[derive(Eq, PartialEq, Clone, Debug)]
7pub struct BlockchainRewards {
8 height: u32,
9 current_reward: u64,
10 total_waves_amount: u64,
11 min_increment: u64,
12 term: u32,
13 next_check: u32,
14 voting_interval_start: u32,
15 voting_interval: u32,
16 voting_threshold: u32,
17 votes: Votes,
18}
19
20#[allow(clippy::too_many_arguments)]
21impl BlockchainRewards {
22 pub fn new(
23 height: u32,
24 current_reward: u64,
25 total_waves_amount: u64,
26 min_increment: u64,
27 term: u32,
28 next_check: u32,
29 voting_interval_start: u32,
30 voting_interval: u32,
31 voting_threshold: u32,
32 votes: Votes,
33 ) -> Self {
34 Self {
35 height,
36 current_reward,
37 total_waves_amount,
38 min_increment,
39 term,
40 next_check,
41 voting_interval_start,
42 voting_interval,
43 voting_threshold,
44 votes,
45 }
46 }
47
48 pub fn height(&self) -> u32 {
49 self.height
50 }
51
52 pub fn current_reward(&self) -> u64 {
53 self.current_reward
54 }
55
56 pub fn total_waves_amount(&self) -> u64 {
57 self.total_waves_amount
58 }
59
60 pub fn min_increment(&self) -> u64 {
61 self.min_increment
62 }
63
64 pub fn term(&self) -> u32 {
65 self.term
66 }
67
68 pub fn next_check(&self) -> u32 {
69 self.next_check
70 }
71
72 pub fn voting_interval_start(&self) -> u32 {
73 self.voting_interval_start
74 }
75
76 pub fn voting_interval(&self) -> u32 {
77 self.voting_interval
78 }
79
80 pub fn voting_threshold(&self) -> u32 {
81 self.voting_threshold
82 }
83
84 pub fn votes(&self) -> Votes {
85 self.votes.clone()
86 }
87}
88
89impl TryFrom<&Value> for BlockchainRewards {
90 type Error = Error;
91
92 fn try_from(value: &Value) -> Result<Self> {
93 let height = JsonDeserializer::safe_to_int_from_field(value, "height")?;
94 let current_reward = JsonDeserializer::safe_to_int_from_field(value, "currentReward")?;
95 let total_waves_amount =
96 JsonDeserializer::safe_to_int_from_field(value, "totalWavesAmount")?;
97 let min_increment = JsonDeserializer::safe_to_int_from_field(value, "minIncrement")?;
98 let term = JsonDeserializer::safe_to_int_from_field(value, "term")?;
99 let next_check = JsonDeserializer::safe_to_int_from_field(value, "nextCheck")?;
100 let voting_interval_start =
101 JsonDeserializer::safe_to_int_from_field(value, "votingIntervalStart")?;
102 let voting_interval = JsonDeserializer::safe_to_int_from_field(value, "votingInterval")?;
103 let voting_threshold = JsonDeserializer::safe_to_int_from_field(value, "votingThreshold")?;
104 let votes: Votes = value["votes"].borrow().try_into()?;
105
106 Ok(BlockchainRewards {
107 height: height as u32,
108 current_reward: current_reward as u64,
109 total_waves_amount: total_waves_amount as u64,
110 min_increment: min_increment as u64,
111 term: term as u32,
112 next_check: next_check as u32,
113 voting_interval_start: voting_interval_start as u32,
114 voting_interval: voting_interval as u32,
115 voting_threshold: voting_threshold as u32,
116 votes,
117 })
118 }
119}
120
121#[derive(Eq, PartialEq, Clone, Debug)]
122pub struct Votes {
123 increase: u32,
124 decrease: u32,
125}
126
127impl Votes {
128 pub fn new(increase: u32, decrease: u32) -> Self {
129 Self { increase, decrease }
130 }
131
132 pub fn increase(&self) -> u32 {
133 self.increase
134 }
135
136 pub fn decrease(&self) -> u32 {
137 self.decrease
138 }
139}
140
141impl TryFrom<&Value> for Votes {
142 type Error = Error;
143
144 fn try_from(value: &Value) -> Result<Self> {
145 let increase = JsonDeserializer::safe_to_int_from_field(value, "increase")?;
146 let decrease = JsonDeserializer::safe_to_int_from_field(value, "decrease")?;
147 Ok(Votes {
148 increase: increase as u32,
149 decrease: decrease as u32,
150 })
151 }
152}
153
154#[cfg(test)]
155mod tests {
156 use crate::model::BlockchainRewards;
157 use serde_json::Value;
158 use std::borrow::Borrow;
159 use std::fs;
160
161 #[test]
162 fn test_json_to_blockchain_rewards() {
163 let data = fs::read_to_string("./tests/resources/blockchain/blockchain_rewards.json")
164 .expect("Unable to read file");
165 let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");
166
167 let rewards_from_json: BlockchainRewards = json.borrow().try_into().unwrap();
168
169 assert_eq!(2224756, rewards_from_json.height());
170 assert_eq!(10904654200000000, rewards_from_json.total_waves_amount());
171 assert_eq!(600000000, rewards_from_json.current_reward());
172 assert_eq!(50000000, rewards_from_json.min_increment());
173 assert_eq!(100000, rewards_from_json.term());
174 assert_eq!(2316999, rewards_from_json.next_check());
175 assert_eq!(2307000, rewards_from_json.voting_interval_start());
176 assert_eq!(10000, rewards_from_json.voting_interval());
177 assert_eq!(5001, rewards_from_json.voting_threshold());
178 assert_eq!(0, rewards_from_json.votes().increase());
179 assert_eq!(1, rewards_from_json.votes().decrease());
180 }
181}