electrs_request/
lib.rs

1use electrum_client::{
2    raw_client::RawClient, Client as ElectrumClient, Config, ElectrumApi, Param,
3};
4use serde::{Deserialize, Serialize};
5
6pub struct Client {
7    electrum_client: ElectrumClient,
8}
9impl Client {
10    pub fn new(electrum_server_address: &str) -> Self {
11        let address = format!("tcp://{}", electrum_server_address);
12        let config = Config::default();
13        let client = ElectrumClient::from_config(&address, config).unwrap();
14        Client {
15            electrum_client: client,
16        }
17    }
18}
19
20#[derive(Debug, Serialize, Deserialize)]
21pub struct BlockchainRelayFeeCommandResponse(pub f64);
22
23pub struct BlockchainRelayFeeCommand {}
24impl BlockchainRelayFeeCommand {
25    pub fn new() -> Self {
26        BlockchainRelayFeeCommand {}
27    }
28    pub fn call(
29        &self,
30        client: &Client,
31    ) -> Result<BlockchainRelayFeeCommandResponse, electrum_client::Error> {
32        let relay_fee_method = "blockchain.relayfee";
33        let relay_fee_response_result =
34            client.electrum_client.raw_call(relay_fee_method, vec![])?;
35        let blockchain_relay_fee_command_response: BlockchainRelayFeeCommandResponse =
36            serde_json::from_value(relay_fee_response_result).unwrap();
37        Ok(blockchain_relay_fee_command_response)
38    }
39}
40#[derive(Debug, Serialize, Deserialize)]
41pub struct BlockchainScriptHashGetBalanceCommand {
42    pub script_hash: String,
43}
44#[derive(Debug, Serialize, Deserialize)]
45pub struct BlockchainScriptHashGetBalanceCommandResponse {
46    pub confirmed: u64,
47    pub unconfirmed: u64,
48}
49impl BlockchainScriptHashGetBalanceCommand {
50    pub fn new(script_hash: &str) -> Self {
51        BlockchainScriptHashGetBalanceCommand {
52            script_hash: script_hash.to_string(),
53        }
54    }
55    pub fn call(
56        &self,
57        client: &Client,
58    ) -> Result<BlockchainScriptHashGetBalanceCommandResponse, electrum_client::Error> {
59        let get_balance_method = "blockchain.scripthash.get_balance";
60        let params = vec![Param::String(self.script_hash.to_owned())];
61        let get_balance_response_result = client
62            .electrum_client
63            .raw_call(get_balance_method, params)?;
64        let get_balance_command_response: BlockchainScriptHashGetBalanceCommandResponse =
65            serde_json::from_value(get_balance_response_result).unwrap();
66        Ok(get_balance_command_response)
67    }
68}
69#[derive(Debug, Serialize, Deserialize)]
70pub struct BlockchainScriptHashListUnspentCommand {
71    pub script_hash: String,
72}
73#[derive(Debug, Serialize, Deserialize)]
74pub struct UnspentResponse {
75    pub height: u64,
76    pub tx_hash: String,
77    pub tx_pos: u64,
78    pub value: u64,
79}
80#[derive(Debug, Serialize, Deserialize)]
81pub struct BlockchainScriptHashListUnspentCommandResponse(pub Vec<UnspentResponse>);
82impl BlockchainScriptHashListUnspentCommand {
83    pub fn new(script_hash: &str) -> Self {
84        BlockchainScriptHashListUnspentCommand {
85            script_hash: script_hash.to_string(),
86        }
87    }
88    pub fn call(
89        &self,
90        client: &Client,
91    ) -> Result<BlockchainScriptHashListUnspentCommandResponse, electrum_client::Error> {
92        let list_unspent_method = "blockchain.scripthash.listunspent";
93        let params = vec![Param::String(self.script_hash.to_owned())];
94        let list_unspent_response_result = client
95            .electrum_client
96            .raw_call(list_unspent_method, params)?;
97        let list_unspent_command_response: BlockchainScriptHashListUnspentCommandResponse =
98            serde_json::from_value(list_unspent_response_result).unwrap();
99        Ok(list_unspent_command_response)
100    }
101}
102#[derive(Debug, Serialize, Deserialize)]
103pub struct BlockchainScriptHashGetHistoryCommand {
104    pub script_hash: String,
105}
106#[derive(Debug, Serialize, Deserialize)]
107pub struct HistoricalTransaction {
108    // can be negative when block hasn't been confirmed
109    pub height: i64,
110    pub tx_hash: String,
111}
112#[derive(Debug, Serialize, Deserialize)]
113pub struct BlockchainScriptHashGetHistoryCommandResponse(pub Vec<HistoricalTransaction>);
114impl BlockchainScriptHashGetHistoryCommand {
115    pub fn new(script_hash: &str) -> Self {
116        BlockchainScriptHashGetHistoryCommand {
117            script_hash: script_hash.to_string(),
118        }
119    }
120    pub fn call(
121        &self,
122        client: &Client,
123    ) -> Result<BlockchainScriptHashGetHistoryCommandResponse, electrum_client::Error> {
124        let get_history_method = "blockchain.scripthash.get_history";
125        let params = vec![Param::String(self.script_hash.to_owned())];
126        let get_history_response_result = client
127            .electrum_client
128            .raw_call(get_history_method, params)?;
129        let get_history_command_response: BlockchainScriptHashGetHistoryCommandResponse =
130            serde_json::from_value(get_history_response_result).unwrap();
131        Ok(get_history_command_response)
132    }
133}