bitcoind_json_rpc_client/client_sync/v17/
blockchain.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Macros for implementing JSON-RPC methods on a client.
4//!
5//! Specifically this is methods found under the `== Blockchain ==` section of the
6//! API docs of `bitcoind v0.17.1`.
7//!
8//! All macros require `Client` to be in scope.
9//!
10//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11
12/// Implements bitcoind JSON-RPC API method `getbestblockhash`
13#[macro_export]
14macro_rules! impl_client_v17__getbestblockhash {
15    () => {
16        impl Client {
17            /// Gets the blockhash of the current chain tip.
18            pub fn best_block_hash(&self) -> Result<bitcoin::BlockHash> {
19                let json = self.get_best_block_hash()?;
20                Ok(json.block_hash()?)
21            }
22
23            pub fn get_best_block_hash(&self) -> Result<GetBestBlockHash> {
24                self.call("getbestblockhash", &[])
25            }
26        }
27    };
28}
29
30/// Implements bitcoind JSON-RPC API method `getblock`
31#[macro_export]
32macro_rules! impl_client_v17__getblock {
33    () => {
34        impl Client {
35            /// Gets a block by blockhash.
36            pub fn get_block(&self, hash: BlockHash) -> Result<Block> {
37                let json = self.get_block_verbosity_zero(hash)?;
38                Ok(json.block()?)
39            }
40
41            pub fn get_block_verbosity_zero(
42                &self,
43                hash: BlockHash,
44            ) -> Result<GetBlockVerbosityZero> {
45                self.call("getblock", &[into_json(hash)?, 0.into()])
46            }
47
48            pub fn get_block_verbosity_one(&self, hash: BlockHash) -> Result<GetBlockVerbosityOne> {
49                self.call("getblock", &[into_json(hash)?, 1.into()])
50            }
51        }
52    };
53}
54
55/// Implements bitcoind JSON-RPC API method `getblockchaininfo`
56#[macro_export]
57macro_rules! impl_client_v17__getblockchaininfo {
58    () => {
59        impl Client {
60            pub fn get_blockchain_info(&self) -> Result<GetBlockchainInfo> {
61                self.call("getblockchaininfo", &[])
62            }
63        }
64    };
65}
66
67/// Implements bitcoind JSON-RPC API method `getblockcount`
68#[macro_export]
69macro_rules! impl_client_v17__getblockcount {
70    () => {
71        impl Client {
72            pub fn get_block_count(&self) -> Result<GetBlockCount> {
73                self.call("getblockcount", &[])
74            }
75        }
76    };
77}
78
79/// Implements bitcoind JSON-RPC API method `getblockhash`
80#[macro_export]
81macro_rules! impl_client_v17__getblockhash {
82    () => {
83        impl Client {
84            pub fn get_block_hash(&self, height: u64) -> Result<GetBlockHash> {
85                self.call("getblockhash", &[into_json(height)?])
86            }
87        }
88    };
89}
90
91/// Implements bitcoind JSON-RPC API method `getblockheader`
92#[macro_export]
93macro_rules! impl_client_v17__getblockheader {
94    () => {
95        impl Client {
96            pub fn get_block_header(&self, hash: &BlockHash) -> Result<GetBlockHeader> {
97                self.call("getblockheader", &[into_json(hash)?, into_json(false)?])
98            }
99
100            // This is the same as calling getblockheader with verbose==true.
101            pub fn get_block_header_verbose(
102                &self,
103                hash: &BlockHash,
104            ) -> Result<GetBlockHeaderVerbose> {
105                self.call("getblockheader", &[into_json(hash)?])
106            }
107        }
108    };
109}
110
111/// Implements bitcoind JSON-RPC API method `getblockstats`
112#[macro_export]
113macro_rules! impl_client_v17__getblockstats {
114    () => {
115        impl Client {
116            pub fn get_block_stats_by_height(&self, height: u32) -> Result<GetBlockStats> {
117                self.call("getblockstats", &[into_json(height)?])
118            }
119
120            pub fn get_block_stats_by_block_hash(&self, hash: &BlockHash) -> Result<GetBlockStats> {
121                self.call("getblockstats", &[into_json(hash)?])
122            }
123        }
124    };
125}
126
127/// Implements bitcoind JSON-RPC API method `getchaintips`
128#[macro_export]
129macro_rules! impl_client_v17__getchaintips {
130    () => {
131        impl Client {
132            pub fn get_chain_tips(&self) -> Result<GetChainTips> { self.call("getchaintips", &[]) }
133        }
134    };
135}
136
137/// Implements bitcoind JSON-RPC API method `getchaintxstats`
138#[macro_export]
139macro_rules! impl_client_v17__getchaintxstats {
140    () => {
141        impl Client {
142            pub fn get_chain_tx_stats(&self) -> Result<GetChainTxStats> {
143                self.call("getchaintxstats", &[])
144            }
145        }
146    };
147}
148
149/// Implements bitcoind JSON-RPC API method `getdifficulty`
150#[macro_export]
151macro_rules! impl_client_v17__getdifficulty {
152    () => {
153        impl Client {
154            pub fn get_difficulty(&self) -> Result<GetDifficulty> {
155                self.call("getdifficulty", &[])
156            }
157        }
158    };
159}
160
161/// Implements bitcoind JSON-RPC API method `getmempoolancestors`
162#[macro_export]
163macro_rules! impl_client_v17__getmempoolancestors {
164    () => {
165        impl Client {
166            pub fn get_mempool_ancestors(&self, txid: Txid) -> Result<GetMempoolAncestors> {
167                self.call("getmempoolancestors", &[into_json(txid)?])
168            }
169        }
170    };
171}
172
173/// Implements bitcoind JSON-RPC API method `gettxout`
174#[macro_export]
175macro_rules! impl_client_v17__gettxout {
176    () => {
177        impl Client {
178            pub fn get_tx_out(&self, txid: Txid, vout: u64) -> Result<GetTxOut> {
179                self.call("gettxout", &[into_json(txid)?, into_json(vout)?])
180            }
181        }
182    };
183}