bitcoind_request/command/
get_best_block_hash.rs

1use crate::client::Client;
2/*
3getbestblockhash
4
5Returns the hash of the best (tip) block in the most-work fully-validated chain.
6
7Result:
8"hex"    (string) the block hash, hex-encoded
9
10Examples:
11> bitcoin-cli getbestblockhash
12> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getbestblockhash", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
13*/
14use crate::command::request::request;
15use crate::command::CallableCommand;
16use crate::Blockhash;
17use crate::BlockhashHexEncoded;
18use serde::Deserialize;
19use serde::Serialize;
20use serde_json::value::{to_raw_value, RawValue};
21
22pub struct GetBestBlockHashCommand {
23    blockhash_hex_encoded: BlockhashHexEncoded,
24}
25impl GetBestBlockHashCommand {
26    pub fn new(blockhash_hex_encoded: BlockhashHexEncoded) -> Self {
27        GetBestBlockHashCommand {
28            blockhash_hex_encoded,
29        }
30    }
31}
32#[derive(Serialize, Deserialize, Debug)]
33pub struct GetBestBlockHashCommandResponse(pub Blockhash);
34
35impl CallableCommand for GetBestBlockHashCommand {
36    type Response = GetBestBlockHashCommandResponse;
37    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
38        let blockhash_arg = &self.blockhash_hex_encoded.0;
39        let command = "getbestblockhash";
40        let params: Vec<Box<RawValue>> = vec![];
41        let r = request(client, command, params);
42        let response: GetBestBlockHashCommandResponse = r.result()?;
43        Ok(response)
44    }
45}