bitcoind_request/command/
get_chain_tx_stats.rs

1use crate::client::Client;
2/*
3getchaintxstats ( nblocks "blockhash" )
4
5Compute statistics about the total number and rate of transactions in the chain.
6
7Arguments:
81. nblocks      (numeric, optional, default=one month) Size of the window in number of blocks
92. blockhash    (string, optional, default=chain tip) The hash of the block that ends the window.
10
11Result:
12{                                       (json object)
13  "time" : xxx,                         (numeric) The timestamp for the final block in the window, expressed in UNIX epoch time
14  "txcount" : n,                        (numeric) The total number of transactions in the chain up to that point
15  "window_final_block_hash" : "hex",    (string) The hash of the final block in the window
16  "window_final_block_height" : n,      (numeric) The height of the final block in the window.
17  "window_block_count" : n,             (numeric) Size of the window in number of blocks
18  "window_tx_count" : n,                (numeric) The number of transactions in the window. Only returned if "window_block_count" is > 0
19  "window_interval" : n,                (numeric) The elapsed time in the window in seconds. Only returned if "window_block_count" is > 0
20  "txrate" : n                          (numeric) The average rate of transactions per second in the window. Only returned if "window_interval" is > 0
21}
22
23Examples:
24> bitcoin-cli getchaintxstats
25> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getchaintxstats", "params": [2016]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
26 */
27use crate::command::request::request;
28use crate::command::CallableCommand;
29use crate::Blockhash;
30use crate::BlockhashHexEncoded;
31use serde::Deserialize;
32use serde::Serialize;
33use serde_json::value::{to_raw_value, RawValue};
34
35pub struct GetChainTxStatsCommand {
36    n_blocks: Option<u64>, // (numeric, optional, default=one month) Size of the window in number of blocks
37    blockhash: Option<Blockhash>, //  (string, optional, default=chain tip) The hash of the block that ends the window.
38}
39impl GetChainTxStatsCommand {
40    pub fn new() -> Self {
41        GetChainTxStatsCommand {
42            n_blocks: None, // defaults to one month
43            blockhash: None,
44        }
45    }
46    pub fn set_n_blocks(mut self, n_blocks: u64) -> Self {
47        self.n_blocks = Some(n_blocks);
48        self
49    }
50    pub fn set_blockhash(mut self, blockhash: Blockhash) -> Self {
51        self.blockhash = Some(blockhash);
52        self
53    }
54}
55#[derive(Serialize, Deserialize, Debug)]
56pub struct GetChainTxStatsCommandResponse {
57    pub time: u64, // The timestamp for the final block in the window, expressed in UNIX epoch time
58    pub txcount: u64, // The total number of transactions in the chain up to that point
59    pub window_final_block_hash: String, // "hex" The hash of the final block in the window
60    pub window_final_block_height: u64, // The height of the final block in the window.
61    pub window_block_count: u64, // Size of the window in number of blocks
62    pub window_tx_count: u64, // The number of transactions in the window. Only returned if "window_block_count" is > 0
63    pub window_interval: u64, // The elapsed time in the window in seconds. Only returned if "window_block_count" is > 0
64    pub txrate: f64, // The average rate of transactions per second in the window. Only returned if "window_interval" is > 0
65}
66
67impl CallableCommand for GetChainTxStatsCommand {
68    type Response = GetChainTxStatsCommandResponse;
69    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
70        let command = "getchaintxstats";
71        let mut params: Vec<Box<RawValue>> = vec![];
72        if let Some(n_blocks) = &self.n_blocks {
73            let n_blocks_arg_raw_value = to_raw_value(&n_blocks).unwrap();
74            params.push(n_blocks_arg_raw_value)
75        }
76        if let Some(blockhash) = &self.blockhash {
77            let blockhash_str = &blockhash.0;
78            let blockhash_arg_raw_value = to_raw_value(&blockhash_str).unwrap();
79            params.push(blockhash_arg_raw_value)
80        }
81        let r = request(client, command, params);
82        let response: GetChainTxStatsCommandResponse = r.result()?;
83        Ok(response)
84    }
85}