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
use crate::{
client::Client,
command::{request::request, CallableCommand},
};
use serde::{Deserialize, Serialize};
use serde_json::value::to_raw_value;
const GET_NETWORK_HASH_PS_COMMAND: &str = "getnetworkhashps";
const DEFAULT_N_BLOCKS: u64 = 120;
const N_BLOCKS_ARGUMENT_FOR_BLOCKS_SINCE_LAST_DIFFICULT_CHANGE: i64 = -1;
const HEIGHT_ARGUMENT_FOR_CALCULATING_BASED_ON_CURRENT_HEIGHT: i64 = -1;
#[derive(Serialize, Deserialize, Debug)]
pub struct GetNetworkHashPsCommandResponse(pub f64);
pub enum BlocksToIncludeArg {
NBlocks(u64),
BlocksSinceLastDifficultyChange,
}
pub enum HeightArg {
Height(u64),
CurrentHeight,
}
pub struct GetNetworkHashPsCommand {
n_blocks: BlocksToIncludeArg, height: HeightArg, }
impl GetNetworkHashPsCommand {
pub fn new() -> Self {
GetNetworkHashPsCommand {
n_blocks: BlocksToIncludeArg::NBlocks(DEFAULT_N_BLOCKS), height: HeightArg::CurrentHeight, }
}
pub fn set_n_blocks(mut self, n_blocks: BlocksToIncludeArg) -> Self {
self.n_blocks = n_blocks;
self
}
pub fn set_height(mut self, height: HeightArg) -> Self {
self.height = height;
self
}
}
impl CallableCommand for GetNetworkHashPsCommand {
type Response = GetNetworkHashPsCommandResponse;
fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
let mut params: Vec<i64> = vec![];
let n_blocks_arg: i64 = match self.n_blocks {
BlocksToIncludeArg::NBlocks(n_blocks) => n_blocks as i64,
BlocksToIncludeArg::BlocksSinceLastDifficultyChange => {
N_BLOCKS_ARGUMENT_FOR_BLOCKS_SINCE_LAST_DIFFICULT_CHANGE
}
};
let height_arg = match self.height {
HeightArg::Height(height) => height as i64,
HeightArg::CurrentHeight => HEIGHT_ARGUMENT_FOR_CALCULATING_BASED_ON_CURRENT_HEIGHT,
};
let n_blocks_arg_raw_value = to_raw_value(&n_blocks_arg).unwrap();
let height_arg_raw_value = to_raw_value(&height_arg).unwrap();
let mut params = vec![n_blocks_arg_raw_value, height_arg_raw_value];
let r = request(client, GET_NETWORK_HASH_PS_COMMAND, params);
let response: GetNetworkHashPsCommandResponse = r.result()?;
Ok(response)
}
}