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)]
pub struct Vin {
pub coinbase: Option<String>,
pub txinwitness: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ScriptPubKey {
pub asm: String,
pub hex: String,
pub address: Option<String>,
#[serde(rename = "type")]
pub type_: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Vout {
pub value: f64,
pub n: i64,
pub script_pub_key: ScriptPubKey,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DecodeRawTransactionResponse {
pub txid: String, pub hash: String, pub size: u64, pub vsize: u64, pub version: u64, pub weight: u64, pub locktime: u64, pub vin: Vec<Vin>,
pub vout: Vec<Vout>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum GetBlockCommandTransactionResponse {
Raw(DecodeRawTransactionResponse),
Id(String),
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum GetBlockCommandResponse {
BlockHash(String),
Block(Block),
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Block {
pub hash: String, pub confirmations: i64, pub size: u64, pub strippedsize: u64, pub weight: u64, pub height: u64, pub version: u64, pub version_hex: String, pub merkleroot: String, pub tx: Vec<GetBlockCommandTransactionResponse>, 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>, }
type GetBlockAsSerializedHextEncodedDataCommandResponse = String;
pub enum GetBlockCommandVerbosity {
SerializedHexEncodedData, BlockObjectWithoutTransactionInformation, BlockObjectWithTransactionInformation, }
pub struct GetBlockCommand {
blockhash: Blockhash,
verbosity: GetBlockCommandVerbosity,
}
impl GetBlockCommand {
pub fn new(blockhash: Blockhash) -> Self {
GetBlockCommand {
blockhash,
verbosity: GetBlockCommandVerbosity::BlockObjectWithoutTransactionInformation,
}
}
pub fn verbosity(&mut self, verbosity: GetBlockCommandVerbosity) -> &Self {
self.verbosity = verbosity;
self
}
}
impl CallableCommand for GetBlockCommand {
type Response = GetBlockCommandResponse;
fn call(&self, client: &Client) -> Self::Response {
let verbosity_arg = match self.verbosity {
GetBlockCommandVerbosity::SerializedHexEncodedData => 0,
GetBlockCommandVerbosity::BlockObjectWithoutTransactionInformation => 1,
GetBlockCommandVerbosity::BlockObjectWithTransactionInformation => 2,
};
let blockhash_arg = &self.blockhash.0;
let blockhash_arg_raw_value = to_raw_value(&blockhash_arg).unwrap();
let verbosity_arg_raw_value = to_raw_value(&verbosity_arg).unwrap();
let command = "getblock";
let params = vec![blockhash_arg_raw_value, verbosity_arg_raw_value];
println!("{:?}", params);
let r = request(client, command, params);
let response: GetBlockCommandResponse = r.result().unwrap();
response
}
}