spark_sdk/wallet/handlers/
leaves.rs

1use crate::{
2    error::SparkSdkError,
3    signer::traits::SparkSigner,
4    wallet::leaf_manager::{LeafNodeStatus, PublicLeafNode},
5    SparkSdk,
6};
7
8impl<S: SparkSigner + Send + Sync> SparkSdk<S> {
9    /// Returns the count of available leaves in the wallet.
10    ///
11    /// An available leaf is one that has a status of `LeafNodeStatus::Available`, meaning it can be
12    /// used for transfers or other operations. This excludes leaves that are locked, pending, or in other states.
13    ///
14    /// # Returns
15    /// - `Ok(u32)` - The number of available leaves
16    /// - `Err(SparkSdkError)` - If there was an error accessing the leaf manager
17    ///
18    /// # Example
19    /// ```no_run
20    /// # use spark_sdk::SparkSdk;
21    /// # let sdk: SparkSdk = unimplemented!();
22    /// let available_count = sdk.get_available_leaves_count().unwrap();
23    /// println!("Number of available leaves: {}", available_count);
24    /// ```
25    pub fn get_available_leaves_count(&self) -> Result<u32, SparkSdkError> {
26        let cb =
27            Box::new(|node: &PublicLeafNode| matches!(node.status(), LeafNodeStatus::Available));
28        self.leaf_manager.get_leaf_count(Some(cb))
29    }
30
31    /// Returns the balance of the wallet in satoshis.
32    ///
33    /// This function calculates the total value of all available leaves in the wallet.
34    ///
35    /// # Returns
36    /// - `Ok(u64)` - The total balance in satoshis
37    /// - `Err(SparkSdkError)` - If there was an error accessing the leaf manager
38    ///
39    /// # Example
40    /// ```no_run
41    /// # use spark_sdk::SparkSdk;
42    /// # let sdk: SparkSdk = unimplemented!();
43    /// let balance = sdk.get_btc_balance().unwrap();
44    /// println!("Balance: {}", balance);
45    /// ```
46    pub fn get_btc_balance(&self) -> Result<u64, SparkSdkError> {
47        let cb =
48            Box::new(|node: &PublicLeafNode| matches!(node.status(), LeafNodeStatus::Available));
49        self.leaf_manager.get_btc_value(Some(cb))
50    }
51}