1use bitcoin::{Script, Txid};
6
7use crate::types::{Call, Param, ToElectrumScriptHash};
8
9#[derive(Default)]
20pub struct Batch {
21 calls: Vec<Call>,
22}
23
24impl Batch {
25 pub fn raw(&mut self, method: String, params: Vec<Param>) {
27 self.calls.push((method, params));
28 }
29
30 pub fn script_list_unspent(&mut self, script: &Script) {
32 let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
33 self.calls
34 .push((String::from("blockchain.scripthash.listunspent"), params));
35 }
36
37 pub fn script_get_history(&mut self, script: &Script) {
39 let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
40 self.calls
41 .push((String::from("blockchain.scripthash.get_history"), params));
42 }
43
44 pub fn script_get_balance(&mut self, script: &Script) {
46 let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
47 self.calls
48 .push((String::from("blockchain.scripthash.get_balance"), params));
49 }
50
51 pub fn script_subscribe(&mut self, script: &Script) {
53 let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
54 self.calls
55 .push((String::from("blockchain.scripthash.subscribe"), params));
56 }
57
58 pub fn transaction_get(&mut self, tx_hash: &Txid) {
60 let params = vec![Param::String(format!("{:x}", tx_hash))];
61 self.calls
62 .push((String::from("blockchain.transaction.get"), params));
63 }
64
65 pub fn estimate_fee(&mut self, number: usize) {
67 let params = vec![Param::Usize(number)];
68 self.calls
69 .push((String::from("blockchain.estimatefee"), params));
70 }
71
72 pub fn block_header(&mut self, height: u32) {
74 let params = vec![Param::U32(height)];
75 self.calls
76 .push((String::from("blockchain.block.header"), params));
77 }
78
79 pub fn iter(&self) -> BatchIter {
81 BatchIter {
82 batch: self,
83 index: 0,
84 }
85 }
86}
87
88impl std::iter::IntoIterator for Batch {
89 type Item = (String, Vec<Param>);
90 type IntoIter = std::vec::IntoIter<Self::Item>;
91
92 fn into_iter(self) -> Self::IntoIter {
93 self.calls.into_iter()
94 }
95}
96
97pub struct BatchIter<'a> {
98 batch: &'a Batch,
99 index: usize,
100}
101
102impl<'a> std::iter::Iterator for BatchIter<'a> {
103 type Item = &'a (String, Vec<Param>);
104
105 fn next(&mut self) -> Option<Self::Item> {
106 let val = self.batch.calls.get(self.index);
107 self.index += 1;
108 val
109 }
110}