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 transaction_get_merkle(&mut self, tx_hash_and_height: &(Txid, usize)) {
67 let (tx_hash, height) = tx_hash_and_height;
68 let params = vec![
69 Param::String(format!("{:x}", tx_hash)),
70 Param::Usize(*height),
71 ];
72 self.calls
73 .push((String::from("blockchain.transaction.get_merkle"), params));
74 }
75
76 pub fn estimate_fee(&mut self, number: usize) {
78 let params = vec![Param::Usize(number)];
79 self.calls
80 .push((String::from("blockchain.estimatefee"), params));
81 }
82
83 pub fn block_header(&mut self, height: u32) {
85 let params = vec![Param::U32(height)];
86 self.calls
87 .push((String::from("blockchain.block.header"), params));
88 }
89
90 pub fn iter(&self) -> BatchIter {
92 BatchIter {
93 batch: self,
94 index: 0,
95 }
96 }
97}
98
99impl std::iter::IntoIterator for Batch {
100 type Item = (String, Vec<Param>);
101 type IntoIter = std::vec::IntoIter<Self::Item>;
102
103 fn into_iter(self) -> Self::IntoIter {
104 self.calls.into_iter()
105 }
106}
107
108pub struct BatchIter<'a> {
109 batch: &'a Batch,
110 index: usize,
111}
112
113impl<'a> std::iter::Iterator for BatchIter<'a> {
114 type Item = &'a (String, Vec<Param>);
115
116 fn next(&mut self) -> Option<Self::Item> {
117 let val = self.batch.calls.get(self.index);
118 self.index += 1;
119 val
120 }
121}