use serde::{Deserialize, Serialize};
use serde_json::value::to_raw_value;
use crate::client::Client;
use crate::{command::CallableCommand, Blockhash};
use crate::command::request::request;
type TxId = String;
pub struct GetRawTransactionCommand {
txid: TxId,
is_verbose: bool,
blockhash: Option<Blockhash>,
}
impl GetRawTransactionCommand {
pub fn new(txid: TxId) -> Self {
GetRawTransactionCommand {
txid,
is_verbose: false,
blockhash: None,
}
}
pub fn verbose(&mut self, verbose: bool) -> &Self {
self.is_verbose = verbose;
self
}
pub fn blockhash(&mut self, blockhash: Blockhash) -> &Self {
self.blockhash = Some(blockhash);
self
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct HexEncodedWitnessData(pub String);
#[derive(Serialize, Deserialize, Debug)]
pub struct BitcoinAddress(pub String);
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ScriptPubKey {
pub asm: String, pub hex: String, pub req_sigs: Option<u64>, #[serde(alias = "type")]
pub type_: String, }
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ScriptSig {
pub asm: String, pub hex: String, }
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum Vin {
Coinbase(CoinbaseVin),
NonCoinbase(NonCoinbaseVin),
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CoinbaseVin {
pub coinbase: String,
pub sequence: u64, pub txinwitness: Vec<HexEncodedWitnessData>, }
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct NonCoinbaseVin {
pub txid: String, pub vout: u64, pub script_sig: ScriptSig,
pub sequence: u64, pub txinwitness: Option<Vec<HexEncodedWitnessData>>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Vout {
pub value: f64, pub n: u64, pub script_pub_key: ScriptPubKey,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
pub hex: String, pub txid: String, pub hash: String, pub size: u64, pub vsize: u64, pub weight: u64, pub version: u64, pub locktime: u64, pub vin: Vec<Vin>,
pub vout: Vec<Vout>,
pub blockhash: String, pub confirmations: u64, pub blocktime: u64, pub time: u64, }
impl Transaction {
pub fn is_coinbase_transaction(&self) -> bool {
match self.vin.first().unwrap() {
Vin::Coinbase(_x) => true,
Vin::NonCoinbase(_x) => false,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum GetRawTransactionCommandResponse {
SerializedHexEncodedData(String),
Transaction(Transaction),
}
impl CallableCommand for GetRawTransactionCommand {
type Response = GetRawTransactionCommandResponse;
fn call(&self, client: &Client) -> Self::Response {
let txid_arg = &self.txid;
let verbose_arg = &self.is_verbose;
let txid_arg_raw_value = to_raw_value(&txid_arg).unwrap();
let verbose_arg_raw_value = to_raw_value(&verbose_arg).unwrap();
let command = "getrawtransaction";
let params = vec![txid_arg_raw_value, verbose_arg_raw_value];
let r = request(client, command, params);
let response: GetRawTransactionCommandResponse = r.result().unwrap();
response
}
}