fireblocks_sdk/
paged_client.rs

1use {
2    crate::{Client, Epoch},
3    chrono::{TimeZone, Utc},
4    std::sync::Arc,
5};
6
7mod paged_transaction;
8mod paged_vault;
9pub use {paged_transaction::TransactionStream, paged_vault::VaultStream};
10
11#[derive(Clone)]
12pub struct PagedClient {
13    pub client: Arc<Client>,
14}
15
16impl PagedClient {
17    pub const fn new(client: Arc<Client>) -> Self {
18        Self { client }
19    }
20
21    /// Stream the vault accounts based on batch size
22    ///
23    /// ```
24    /// use {
25    ///     fireblocks_sdk::{Client, PagedClient},
26    ///     futures::TryStreamExt,
27    ///     std::sync::Arc,
28    /// };
29    ///
30    /// async fn vault_accounts(c: Client) -> anyhow::Result<()> {
31    ///     let pc = PagedClient::new(Arc::new(c));
32    ///     let mut vault_stream = pc.vaults(100);
33    ///     while let Ok(Some(result)) = vault_stream.try_next().await {
34    ///         tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
35    ///     }
36    ///     Ok(())
37    /// }
38    /// ```
39    /// see [`Client::vaults`]
40    pub fn vaults(&self, batch_size: u16) -> VaultStream {
41        VaultStream::new(self.client.clone(), batch_size)
42    }
43
44    /// Stream all the transactions from source vault account id and after some
45    /// date
46    ///
47    /// Default date is 2022-04-06 if None provided
48    ///
49    /// ```
50    /// use {
51    ///     fireblocks_sdk::{Client, PagedClient},
52    ///     futures::TryStreamExt,
53    ///     std::sync::Arc,
54    /// };
55    ///
56    /// async fn transactions_paged(c: Client) -> anyhow::Result<()> {
57    ///     let pc = PagedClient::new(Arc::new(c));
58    ///     let mut ts = pc.transactions_from_source(0, 100, None);
59    ///     while let Ok(Some(result)) = ts.try_next().await {
60    ///         tracing::info!("transactions {}", result.len());
61    ///     }
62    ///     Ok(())
63    /// }
64    /// ```
65    ///
66    /// see
67    /// * [`Client::transactions`]
68    pub fn transactions_from_source(
69        &self,
70        vault_id: i32,
71        batch_size: u16,
72        after: Option<Epoch>,
73    ) -> TransactionStream {
74        #[allow(clippy::unwrap_used, clippy::or_fun_call)]
75        let after = after.unwrap_or(Utc.with_ymd_and_hms(2022, 4, 6, 0, 1, 1).unwrap());
76        TransactionStream::from_source(self.client.clone(), batch_size, vault_id, after)
77    }
78
79    ///  Stream all the transactions from destination vault account id
80    ///  See [`self.transactions_from_source`]
81    pub fn transactions_from_destination(
82        &self,
83        vault_id: i32,
84        batch_size: u16,
85        after: Option<Epoch>,
86    ) -> TransactionStream {
87        #[allow(clippy::unwrap_used, clippy::or_fun_call)]
88        let after = after.unwrap_or(Utc.with_ymd_and_hms(2022, 4, 6, 0, 1, 1).unwrap());
89        TransactionStream::from_dest(self.client.clone(), batch_size, vault_id, after)
90    }
91}