bitcoind_request/command/
get_block_hash.rs

1/*
2getblockhash height
3
4Returns hash of block in best-block-chain at height provided.
5
6Arguments:
71. height    (numeric, required) The height index
8
9Result:
10"hex"    (string) The block hash
11
12Examples:
13> bitcoin-cli getblockhash 1000
14> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockhash", "params": [1000]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
15 */
16use crate::command::CallableCommand;
17use crate::Blockhash;
18use crate::{client::Client, command::request::request};
19use serde::{Deserialize, Serialize};
20use serde_json::value::{to_raw_value, RawValue};
21
22type BlockHeight = u64;
23pub struct GetBlockHashCommand {
24    height: BlockHeight,
25}
26impl GetBlockHashCommand {
27    pub fn new(height: BlockHeight) -> Self {
28        GetBlockHashCommand { height }
29    }
30}
31// TODO: struct GetBlockHashCommandResponse(String);
32#[derive(Serialize, Deserialize, Debug)]
33pub struct GetBlockHashCommandResponse(pub Blockhash);
34
35impl CallableCommand for GetBlockHashCommand {
36    type Response = GetBlockHashCommandResponse;
37    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
38        let height_arg = &self.height;
39        let height_arg_raw_value = to_raw_value(height_arg).unwrap();
40        let command = "getblockhash";
41        let params = vec![height_arg_raw_value];
42        let r = request(client, command, params);
43        let response: GetBlockHashCommandResponse = r.result()?;
44        Ok(response)
45    }
46}