spark_rust/wallet/handlers/leaves.rs
1use crate::{
2 error::SparkSdkError, signer::traits::SparkSigner,
3 wallet::internal_handlers::traits::leaves::LeavesInternalHandlers, with_handler_lock, SparkSdk,
4};
5
6impl<S: SparkSigner + Send + Sync + Clone + 'static> SparkSdk<S> {
7 /// Returns the balance of the wallet in satoshis.
8 ///
9 /// This function calculates the total value of all available leaves in the wallet.
10 ///
11 /// # Returns
12 /// - `Ok(u64)` - The total balance in satoshis
13 /// - `Err(SparkSdkError)` - If there was an error accessing the leaf manager
14 ///
15 /// # Example
16 /// ```
17 /// # use spark_rust::{SparkSdk, SparkNetwork, signer::default_signer::DefaultSigner, signer::traits::SparkSigner};
18 ///
19 /// async fn example() {
20 /// let mnemonic = "abandon ability able about above absent absorb abstract absurd abuse access accident";
21 /// let network = SparkNetwork::Regtest;
22 /// let signer = DefaultSigner::from_mnemonic(mnemonic, network.clone()).await.unwrap();
23 /// let sdk = SparkSdk::new(network, signer).await.unwrap();
24 /// let balance = sdk.get_bitcoin_balance();
25 /// println!("Balance: {}", balance);
26 /// }
27 /// ```
28 #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all))]
29 pub fn get_bitcoin_balance(&self) -> u64 {
30 self.leaf_manager.get_available_bitcoin_value(None)
31 }
32
33 /// Performs a comprehensive synchronization of your wallet with the Spark network.
34 ///
35 /// This function executes multiple synchronization operations in sequence:
36 /// 1. Claims all pending Bitcoin transfers that have been sent to you
37 /// 2. Refreshes timelock nodes to handle any mature timelocked funds
38 /// 3. Synchronizes all leaves (UTXOs) with the Spark network
39 /// 4. Optimizes leaf distribution for efficient wallet operation
40 ///
41 /// This is a convenience method that ensures your wallet is fully up-to-date with
42 /// the latest state from the Spark network. It's recommended to call this periodically
43 /// or whenever you need to ensure you have the most current state.
44 ///
45 /// # Returns
46 ///
47 /// * `Ok(())` - If all synchronization operations completed successfully
48 /// * `Err(SparkSdkError)` - If there was an error during any synchronization step
49 ///
50 /// # Example
51 ///
52 /// ```
53 /// # use spark_rust::{SparkSdk, SparkNetwork, signer::default_signer::DefaultSigner, signer::traits::SparkSigner};
54 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
55 /// # let mnemonic = "abandon ability able about above absent absorb abstract absurd abuse access accident";
56 /// # let network = SparkNetwork::Regtest;
57 /// # let signer = DefaultSigner::from_mnemonic(mnemonic, network.clone()).await?;
58 /// # let sdk = SparkSdk::new(network, signer).await?;
59 ///
60 /// // Synchronize wallet with the network
61 /// sdk.sync_wallet().await?;
62 ///
63 /// // After synchronization, your wallet will have:
64 /// // - Claimed all pending transfers
65 /// // - Updated all leaves
66 /// // - Optimized the leaf structure
67 ///
68 /// // Check your updated balance
69 /// let updated_balance = sdk.get_bitcoin_balance();
70 /// println!("Updated balance: {} satoshis", updated_balance);
71 /// # Ok(())
72 /// # }
73 /// ```
74 pub async fn sync_wallet(&self) -> Result<(), SparkSdkError> {
75 with_handler_lock!(self, async {
76 // claim Bitcoin transfers
77 self.claim_transfers_internal().await?;
78
79 // TODO leaves: refresh timelock nodes
80 self.refresh_timelock_nodes(None).await?;
81
82 // sync Bitcoin leaves
83 self.sync_leaves().await?;
84
85 // optimize leaves
86 self.optimize_leaves().await?;
87
88 Ok(())
89 })
90 .await
91 }
92}