1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use electrum_client::{
    raw_client::RawClient, Client as ElectrumClient, Config, ElectrumApi, Param,
};
use serde::{Deserialize, Serialize};

pub struct Client {
    electrum_client: ElectrumClient,
}
impl Client {
    pub fn new(electrum_server_address: &str) -> Self {
        let address = format!("tcp://{}", electrum_server_address);
        let config = Config::default();
        let client = ElectrumClient::from_config(&address, config).unwrap();
        Client {
            electrum_client: client,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BlockchainRelayFeeCommandResponse(f64);

pub struct BlockchainRelayFeeCommand {}
impl BlockchainRelayFeeCommand {
    pub fn new() -> Self {
        BlockchainRelayFeeCommand {}
    }
    pub fn call(
        &self,
        client: &Client,
    ) -> Result<BlockchainRelayFeeCommandResponse, electrum_client::Error> {
        let relay_fee_method = "blockchain.relayfee";
        let relay_fee_response_result =
            client.electrum_client.raw_call(relay_fee_method, vec![])?;
        let blockchain_relay_fee_command_response: BlockchainRelayFeeCommandResponse =
            serde_json::from_value(relay_fee_response_result).unwrap();
        Ok(blockchain_relay_fee_command_response)
    }
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockchainScriptHashGetBalanceCommand {
    pub script_hash: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockchainScriptHashGetBalanceCommandResponse {
    pub confirmed: u64,
    pub unconfirmed: u64,
}
impl BlockchainScriptHashGetBalanceCommand {
    pub fn new(script_hash: &str) -> Self {
        BlockchainScriptHashGetBalanceCommand {
            script_hash: script_hash.to_string(),
        }
    }
    pub fn call(
        &self,
        client: &Client,
    ) -> Result<BlockchainScriptHashGetBalanceCommandResponse, electrum_client::Error> {
        let get_balance_method = "blockchain.scripthash.get_balance";
        let params = vec![Param::String(self.script_hash.to_owned())];
        let get_balance_response_result = client
            .electrum_client
            .raw_call(get_balance_method, params)?;
        let get_balance_command_response: BlockchainScriptHashGetBalanceCommandResponse =
            serde_json::from_value(get_balance_response_result).unwrap();
        Ok(get_balance_command_response)
    }
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockchainScriptHashListUnspentCommand {
    pub script_hash: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UnspentResponse {
    pub height: u64,
    pub tx_hash: String,
    pub tx_pos: u64,
    pub value: u64,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockchainScriptHashListUnspentCommandResponse(Vec<UnspentResponse>);
impl BlockchainScriptHashListUnspentCommand {
    pub fn new(script_hash: &str) -> Self {
        BlockchainScriptHashListUnspentCommand {
            script_hash: script_hash.to_string(),
        }
    }
    pub fn call(
        &self,
        client: &Client,
    ) -> Result<BlockchainScriptHashListUnspentCommandResponse, electrum_client::Error> {
        let list_unspent_method = "blockchain.scripthash.listunspent";
        let params = vec![Param::String(self.script_hash.to_owned())];
        let list_unspent_response_result = client
            .electrum_client
            .raw_call(list_unspent_method, params)?;
        let list_unspent_command_response: BlockchainScriptHashListUnspentCommandResponse =
            serde_json::from_value(list_unspent_response_result).unwrap();
        Ok(list_unspent_command_response)
    }
}