pub struct Batch { /* private fields */ }Expand description
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
is run on a Client.
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, like
batch_script_get_balance to ask the
server for the balance of multiple scripts with a single request.
Implementations§
Source§impl Batch
impl Batch
Sourcepub fn raw(&mut self, method: String, params: Vec<Param>)
pub fn raw(&mut self, method: String, params: Vec<Param>)
Add a raw request to the batch queue
Examples found in repository?
examples/batch_headers.rs (lines 33-36)
7fn main() {
8 let server = std::env::var("ELECTRUM_SERVER")
9 .unwrap_or_else(|_| "wss://rostrum.riften.net:443".to_string());
10
11 println!("Connecting to {}...", server);
12 let client = match Client::new(&server) {
13 Ok(c) => c,
14 Err(e) => {
15 eprintln!("Connection failed: {:?}", e);
16 return;
17 }
18 };
19
20 println!("Connected! Testing ping...");
21 if let Err(e) = client.ping() {
22 eprintln!("Ping failed: {:?}", e);
23 return;
24 }
25 println!("Ping successful!");
26
27 // Create a batch request for multiple block headers
28 let mut batch = Batch::default();
29
30 // Request headers for blocks 0, 1, 2, 3, 4
31 let heights = [0, 1, 2, 3, 4];
32 for height in &heights {
33 batch.raw(
34 "blockchain.block.header".to_string(),
35 vec![Param::Usize(*height)],
36 );
37 }
38
39 println!("\nBatch requesting {} headers...", heights.len());
40
41 match client.batch_call(&batch) {
42 Ok(results) => {
43 println!("Got {} results:", results.len());
44 for (i, (height, result)) in heights.iter().zip(results.iter()).enumerate() {
45 let header_hex = result.as_str().unwrap_or("<not a string>");
46 // Show first 32 chars of header hex
47 let preview = if header_hex.len() > 64 {
48 format!("{}...", &header_hex[..64])
49 } else {
50 header_hex.to_string()
51 };
52 println!(" [{}] Block {}: {}", i, height, preview);
53 }
54 }
55 Err(e) => {
56 eprintln!("Batch call failed: {:?}", e);
57 }
58 }
59}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Batch
impl RefUnwindSafe for Batch
impl Send for Batch
impl Sync for Batch
impl Unpin for Batch
impl UnsafeUnpin for Batch
impl UnwindSafe for Batch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more