use crate::client::Client;
use crate::command::{request::request, CallableCommand};
use crate::Blockhash;
use serde::{Deserialize, Serialize};
use serde_json::value::to_raw_value;
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum GetBlockHeaderCommandResponse {
BlockHash(String),
BlockHeader(BlockHeader),
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct BlockHeader {
pub hash: String, pub confirmations: i64, pub height: u64, pub version: u64, pub version_hex: String, pub merkleroot: String, pub time: u64, pub mediantime: u64, pub nonce: u64, pub bits: String, pub difficulty: f64, pub chainwork: String, pub n_tx: u64, pub previousblockhash: Option<String>, pub nextblockhash: Option<String>, }
pub struct GetBlockHeaderCommand {
blockhash: Blockhash,
verbose: bool,
}
impl GetBlockHeaderCommand {
pub fn new(blockhash: Blockhash) -> Self {
GetBlockHeaderCommand {
blockhash,
verbose: true,
}
}
pub fn verbose(&mut self, verbose: bool) -> &Self {
self.verbose = verbose;
self
}
}
impl CallableCommand for GetBlockHeaderCommand {
type Response = GetBlockHeaderCommandResponse;
fn call(&self, client: &Client) -> Self::Response {
let verbose_arg = self.verbose;
let blockhash_arg = &self.blockhash.0;
let blockhash_arg_raw_value = to_raw_value(&blockhash_arg).unwrap();
let verbose_arg_raw_value = to_raw_value(&verbose_arg).unwrap();
let command = "getblockheader";
let params = vec![blockhash_arg_raw_value, verbose_arg_raw_value];
println!("{:?}", params);
let r = request(client, command, params);
let response: GetBlockHeaderCommandResponse = r.result().unwrap();
response
}
}