forked from bitcoindevkit/rust-electrum-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.rs
More file actions
124 lines (106 loc) · 4.42 KB
/
batch.rs
File metadata and controls
124 lines (106 loc) · 4.42 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Batch utilities
//!
//! This module contains definitions and helper functions used when making batch calls.
use bitcoin::{Script, Txid};
use crate::types::{Call, EstimationMode, Param, ToElectrumScriptHash};
/// Helper structure that caches all the requests before they are actually sent to the server.
///
/// Calls on this function are stored and run when [`batch_call`](../client/struct.Client.html#method.batch_call)
/// is run on a [`Client`](../client/struct.Client.html).
///
/// This structure can be used to make multiple *different* calls in one single run. For batch
/// calls of the same type, there are shorthands methods defined on the
/// [`Client`](../client/struct.Client.html), like
/// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the
/// server for the balance of multiple scripts with a single request.
#[derive(Default)]
pub struct Batch {
calls: Vec<Call>,
}
impl Batch {
/// Add a raw request to the batch queue
pub fn raw(&mut self, method: String, params: Vec<Param>) {
self.calls.push((method, params));
}
/// Add one `blockchain.scripthash.listunspent` request to the batch queue
pub fn script_list_unspent(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.listunspent"), params));
}
/// Add one `blockchain.scripthash.get_history` request to the batch queue
pub fn script_get_history(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.get_history"), params));
}
/// Add one `blockchain.scripthash.get_balance` request to the batch queue
pub fn script_get_balance(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.get_balance"), params));
}
/// Add one `blockchain.scripthash.listunspent` request to the batch queue
pub fn script_subscribe(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.subscribe"), params));
}
/// Add one `blockchain.transaction.get` request to the batch queue
pub fn transaction_get(&mut self, tx_hash: &Txid) {
let params = vec![Param::String(format!("{:x}", tx_hash))];
self.calls
.push((String::from("blockchain.transaction.get"), params));
}
/// Add one `blockchain.transaction.get_merkle` request to the batch queue
pub fn transaction_get_merkle(&mut self, tx_hash_and_height: &(Txid, usize)) {
let (tx_hash, height) = tx_hash_and_height;
let params = vec![
Param::String(format!("{:x}", tx_hash)),
Param::Usize(*height),
];
self.calls
.push((String::from("blockchain.transaction.get_merkle"), params));
}
/// Add one `blockchain.estimatefee` request to the batch queue
pub fn estimate_fee(&mut self, number: usize, mode: Option<EstimationMode>) {
let mut params = vec![Param::Usize(number)];
if let Some(mode) = mode {
params.push(Param::String(mode.to_string()));
}
self.calls
.push((String::from("blockchain.estimatefee"), params));
}
/// Add one `blockchain.block.get_header` request to the batch queue
pub fn block_header(&mut self, height: u32) {
let params = vec![Param::U32(height)];
self.calls
.push((String::from("blockchain.block.header"), params));
}
/// Returns an iterator on the batch
pub fn iter(&self) -> BatchIter<'_> {
BatchIter {
batch: self,
index: 0,
}
}
}
impl std::iter::IntoIterator for Batch {
type Item = (String, Vec<Param>);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.calls.into_iter()
}
}
pub struct BatchIter<'a> {
batch: &'a Batch,
index: usize,
}
impl<'a> std::iter::Iterator for BatchIter<'a> {
type Item = &'a (String, Vec<Param>);
fn next(&mut self) -> Option<Self::Item> {
let val = self.batch.calls.get(self.index);
self.index += 1;
val
}
}