electrum/
batch.rs

1//! Batch utilities
2//!
3//! This module contains definitions and helper functions used when making batch calls.
4
5use crate::types::{Call, Param, ToElectrumScriptHash};
6use bp::{ScriptPubkey, Txid};
7
8/// Helper structure that caches all the requests before they are actually sent to the server.
9///
10/// Calls on this function are stored and run when [`batch_call`](../client/struct.Client.html#method.batch_call)
11/// is run on a [`Client`](../client/struct.Client.html).
12///
13/// This structure can be used to make multiple *different* calls in one single run. For batch
14/// calls of the same type, there are shorthands methods defined on the
15/// [`Client`](../client/struct.Client.html), like
16/// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the
17/// server for the balance of multiple scripts with a single request.
18#[derive(Default)]
19pub struct Batch {
20    calls: Vec<Call>,
21}
22
23impl Batch {
24    /// Add a raw request to the batch queue
25    pub fn raw(&mut self, method: String, params: Vec<Param>) {
26        self.calls.push((method, params));
27    }
28
29    /// Add one `blockchain.scripthash.listunspent` request to the batch queue
30    pub fn script_list_unspent(&mut self, script: &ScriptPubkey) {
31        let params = vec![Param::String(script.to_electrum_scripthash().as_hex())];
32        self.calls
33            .push((String::from("blockchain.scripthash.listunspent"), params));
34    }
35
36    /// Add one `blockchain.scripthash.get_history` request to the batch queue
37    pub fn script_get_history(&mut self, script: &ScriptPubkey) {
38        let params = vec![Param::String(script.to_electrum_scripthash().as_hex())];
39        self.calls
40            .push((String::from("blockchain.scripthash.get_history"), params));
41    }
42
43    /// Add one `blockchain.scripthash.get_balance` request to the batch queue
44    pub fn script_get_balance(&mut self, script: &ScriptPubkey) {
45        let params = vec![Param::String(script.to_electrum_scripthash().as_hex())];
46        self.calls
47            .push((String::from("blockchain.scripthash.get_balance"), params));
48    }
49
50    /// Add one `blockchain.scripthash.listunspent` request to the batch queue
51    pub fn script_subscribe(&mut self, script: &ScriptPubkey) {
52        let params = vec![Param::String(script.to_electrum_scripthash().as_hex())];
53        self.calls
54            .push((String::from("blockchain.scripthash.subscribe"), params));
55    }
56
57    /// Add one `blockchain.transaction.get` request to the batch queue
58    pub fn transaction_get(&mut self, tx_hash: &Txid) {
59        let params = vec![Param::String(format!("{:x}", tx_hash))];
60        self.calls
61            .push((String::from("blockchain.transaction.get"), params));
62    }
63
64    /// Add one `blockchain.estimatefee` request to the batch queue
65    pub fn estimate_fee(&mut self, number: usize) {
66        let params = vec![Param::Usize(number)];
67        self.calls
68            .push((String::from("blockchain.estimatefee"), params));
69    }
70
71    /// Add one `blockchain.block.get_header` request to the batch queue
72    pub fn block_header(&mut self, height: u32) {
73        let params = vec![Param::U32(height)];
74        self.calls
75            .push((String::from("blockchain.block.header"), params));
76    }
77
78    /// Returns an iterator on the batch
79    pub fn iter(&self) -> BatchIter {
80        BatchIter {
81            batch: self,
82            index: 0,
83        }
84    }
85}
86
87impl IntoIterator for Batch {
88    type Item = (String, Vec<Param>);
89    type IntoIter = std::vec::IntoIter<Self::Item>;
90
91    fn into_iter(self) -> Self::IntoIter {
92        self.calls.into_iter()
93    }
94}
95
96pub struct BatchIter<'a> {
97    batch: &'a Batch,
98    index: usize,
99}
100
101impl<'a> Iterator for BatchIter<'a> {
102    type Item = &'a (String, Vec<Param>);
103
104    fn next(&mut self) -> Option<Self::Item> {
105        let val = self.batch.calls.get(self.index);
106        self.index += 1;
107        val
108    }
109}