bitcoind_request/command/
get_network_hash_ps.rs1use crate::{
20 client::Client,
21 command::{request::request, CallableCommand},
22};
23use serde::{Deserialize, Serialize};
24use serde_json::value::to_raw_value;
25
26const GET_NETWORK_HASH_PS_COMMAND: &str = "getnetworkhashps";
27const DEFAULT_N_BLOCKS: u64 = 120;
28const N_BLOCKS_ARGUMENT_FOR_BLOCKS_SINCE_LAST_DIFFICULT_CHANGE: i64 = -1;
29const HEIGHT_ARGUMENT_FOR_CALCULATING_BASED_ON_CURRENT_HEIGHT: i64 = -1;
30
31#[derive(Serialize, Deserialize, Debug)]
32pub struct GetNetworkHashPsCommandResponse(pub f64);
33
34pub enum BlocksToIncludeArg {
35 NBlocks(u64),
36 BlocksSinceLastDifficultyChange,
37}
38
39pub enum HeightArg {
40 Height(u64),
41 CurrentHeight,
42}
43pub struct GetNetworkHashPsCommand {
44 n_blocks: BlocksToIncludeArg, height: HeightArg, }
47impl GetNetworkHashPsCommand {
48 pub fn new() -> Self {
49 GetNetworkHashPsCommand {
50 n_blocks: BlocksToIncludeArg::NBlocks(DEFAULT_N_BLOCKS), height: HeightArg::CurrentHeight, }
54 }
55 pub fn set_n_blocks(mut self, n_blocks: BlocksToIncludeArg) -> Self {
56 self.n_blocks = n_blocks;
57 self
58 }
59 pub fn set_height(mut self, height: HeightArg) -> Self {
60 self.height = height;
61 self
62 }
63}
64impl CallableCommand for GetNetworkHashPsCommand {
65 type Response = GetNetworkHashPsCommandResponse;
66 fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
67 let mut params: Vec<i64> = vec![];
68 let n_blocks_arg: i64 = match self.n_blocks {
69 BlocksToIncludeArg::NBlocks(n_blocks) => n_blocks as i64,
70 BlocksToIncludeArg::BlocksSinceLastDifficultyChange => {
71 N_BLOCKS_ARGUMENT_FOR_BLOCKS_SINCE_LAST_DIFFICULT_CHANGE
72 }
73 };
74 let height_arg = match self.height {
75 HeightArg::Height(height) => height as i64,
76 HeightArg::CurrentHeight => HEIGHT_ARGUMENT_FOR_CALCULATING_BASED_ON_CURRENT_HEIGHT,
77 };
78
79 let n_blocks_arg_raw_value = to_raw_value(&n_blocks_arg).unwrap();
80 let height_arg_raw_value = to_raw_value(&height_arg).unwrap();
81 let mut params = vec![n_blocks_arg_raw_value, height_arg_raw_value];
82 let r = request(client, GET_NETWORK_HASH_PS_COMMAND, params);
83 let response: GetNetworkHashPsCommandResponse = r.result()?;
84 Ok(response)
85 }
86}