1use crate::types::{Call, Param, ToElectrumScriptHash};
6use bp::{ScriptPubkey, Txid};
7
8#[derive(Default)]
19pub struct Batch {
20 calls: Vec<Call>,
21}
22
23impl Batch {
24 pub fn raw(&mut self, method: String, params: Vec<Param>) {
26 self.calls.push((method, params));
27 }
28
29 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 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 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 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 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 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 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 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}