bitcoincore_rpc_async/
queryable.rs1use super::bitcoin;
12use super::json;
13use serde_json;
14
15use crate::client::Result;
16use crate::client::RpcApi;
17use async_trait::async_trait;
18
19#[async_trait]
21pub trait Queryable<C: RpcApi>: Sized + Send + Sync {
22 type Id;
24 async fn query(rpc: &C, id: &Self::Id) -> Result<Self>;
26}
27
28#[async_trait]
29impl<C: RpcApi + std::marker::Sync> Queryable<C> for bitcoin::blockdata::block::Block {
30 type Id = bitcoin::BlockHash;
31
32 async fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
33 let rpc_name = "getblock";
34 let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?, 0.into()]).await?;
35 let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
36 Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
37 }
38}
39
40#[async_trait]
41impl<C: RpcApi + std::marker::Sync> Queryable<C> for bitcoin::blockdata::transaction::Transaction {
42 type Id = bitcoin::Txid;
43
44 async fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
45 let rpc_name = "getrawtransaction";
46 let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?]).await?;
47 let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
48 Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
49 }
50}
51
52#[async_trait]
53impl<C: RpcApi + std::marker::Sync> Queryable<C> for Option<json::GetTxOutResult> {
54 type Id = bitcoin::OutPoint;
55
56 async fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
57 rpc.get_tx_out(&id.txid, id.vout, Some(true)).await
58 }
59}