use crate::{
client::Client,
command::{request::request, CallableCommand},
};
use serde::{Deserialize, Serialize};
use serde_json::value::to_raw_value;
const GET_TX_OUT_COMMAND: &str = "gettxout";
#[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, pub addresses: Option<Vec<String>>, pub address: Option<String>, }
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetTxOutCommandResponse {
bestblock: String, confirmations: u64, value: f64, script_pub_key: ScriptPubKey,
coinbase: bool, }
pub struct GetTxOutCommand {
tx_id: String, n: u64, include_mempool: Option<bool>, }
impl GetTxOutCommand {
pub fn new(tx_id: String, n: u64) -> Self {
GetTxOutCommand {
tx_id,
n,
include_mempool: None,
}
}
pub fn include_mempool(&mut self, include_mempool: bool) -> &Self {
self.include_mempool = Some(include_mempool);
self
}
}
impl CallableCommand for GetTxOutCommand {
type Response = GetTxOutCommandResponse;
fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
let tx_id_arg = &self.tx_id;
let n_arg = &self.n;
let include_mempool = &self.include_mempool;
let tx_id_arg_raw_value = to_raw_value(&tx_id_arg).unwrap();
let n_arg_raw_value = to_raw_value(&n_arg).unwrap();
let mut params = vec![tx_id_arg_raw_value, n_arg_raw_value];
if let Some(include_mempool_arg) = include_mempool {
let include_mempool_arg_raw_value = to_raw_value(&include_mempool_arg).unwrap();
params.push(include_mempool_arg_raw_value)
}
let r = request(client, GET_TX_OUT_COMMAND, params);
let response: GetTxOutCommandResponse = r.result()?;
Ok(response)
}
}