glin_client/
batch.rs

1//! Batch RPC operations
2//!
3//! Utilities for performing multiple RPC calls in parallel for better performance.
4//!
5//! Note: This module provides patterns and examples for parallel operations.
6//! Applications should use their own metadata types for type-safe queries.
7
8use crate::GlinClient;
9use anyhow::Result;
10use futures::future::join_all;
11
12/// Batch RPC helper
13///
14/// Enables efficient parallel fetching of blockchain data.
15///
16/// # Example
17///
18/// ```rust,no_run
19/// use glin_client::{create_client, BatchRpc};
20///
21/// #[tokio::main]
22/// async fn main() -> anyhow::Result<()> {
23///     let client = create_client("wss://testnet.glin.ai").await?;
24///     let batch = BatchRpc::new(client);
25///
26///     // Example: Fetch storage in parallel
27///     // Applications would use their own metadata types here
28///
29///     Ok(())
30/// }
31/// ```
32pub struct BatchRpc {
33    client: GlinClient,
34}
35
36impl BatchRpc {
37    /// Create new batch RPC helper
38    pub fn new(client: GlinClient) -> Self {
39        Self { client }
40    }
41
42    /// Example: Fetch multiple storage values in parallel
43    ///
44    /// Applications should use their own metadata types for type-safe queries.
45    /// See subxt documentation for static storage queries.
46    ///
47    /// # Pattern Example
48    ///
49    /// ```rust,ignore
50    /// // With static metadata:
51    /// let queries = vec![
52    ///     polkadot::storage().system().account(&alice),
53    ///     polkadot::storage().system().account(&bob),
54    /// ];
55    ///
56    /// let futures = queries.into_iter().map(|query| {
57    ///     let client = self.client.clone();
58    ///     async move {
59    ///         client.storage().at_latest().await?.fetch(&query).await
60    ///     }
61    /// });
62    ///
63    /// let results = futures::future::join_all(futures).await;
64    /// ```
65    pub async fn fetch_storage_parallel<T>(
66        &self,
67        keys: Vec<Vec<u8>>,
68    ) -> Result<Vec<Option<Vec<u8>>>> {
69        // Example pattern for parallel storage queries
70        // Applications should replace this with their own typed queries
71
72        let futures = keys.into_iter().map(|_key| {
73            let _client = self.client.clone();
74            async move {
75                // Placeholder: Applications implement with their metadata types
76                Ok::<Option<Vec<u8>>, anyhow::Error>(None)
77            }
78        });
79
80        let results: Vec<_> = join_all(futures).await;
81        results.into_iter().collect()
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    #[test]
88    fn test_batch_creation() {
89        // Tested in integration tests with real client
90    }
91}