From 7a90b8ffbcfd812c2fb2df83b66450522903d1cc Mon Sep 17 00:00:00 2001 From: sneurlax Date: Wed, 16 Oct 2024 22:13:29 -0500 Subject: add get_balance --- impls/monero.rs/src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'impls/monero.rs/src/lib.rs') diff --git a/impls/monero.rs/src/lib.rs b/impls/monero.rs/src/lib.rs index 53ee8b0..ad1ce1a 100644 --- a/impls/monero.rs/src/lib.rs +++ b/impls/monero.rs/src/lib.rs @@ -376,6 +376,42 @@ impl Wallet { WalletError::WalletErrorCode(status, error_msg) } } + + /// Retrieves the balance and unlocked balance for the given account index. + /// + /// # Example + /// + /// ``` + /// use monero_c_rust::{WalletManager, NetworkType, WalletResult}; + /// use tempfile::TempDir; + /// + /// let temp_dir = TempDir::new().expect("Failed to create temporary directory"); + /// let wallet_path = temp_dir.path().join("wallet_name"); + /// let wallet_str = wallet_path.to_str().unwrap(); + /// + /// let manager = WalletManager::new().unwrap(); + /// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap(); + /// + /// let balance = wallet.get_balance(0); + /// assert!(balance.is_ok(), "Failed to get balance: {:?}", balance.err()); + /// + /// // Clean up wallet files. + /// std::fs::remove_file(wallet_str).expect("Failed to delete test wallet"); + /// std::fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys"); + /// ``` + pub fn get_balance(&self, account_index: u32) -> WalletResult { + unsafe { + let balance = bindings::MONERO_Wallet_balance(self.ptr.as_ptr(), account_index); + let unlocked_balance = bindings::MONERO_Wallet_unlockedBalance(self.ptr.as_ptr(), account_index); + Ok(GetBalance { balance, unlocked_balance }) + } + } +} + +#[derive(Debug)] +pub struct GetBalance { + pub balance: u64, + pub unlocked_balance: u64, } impl Drop for Wallet { @@ -617,3 +653,23 @@ fn test_open_wallet() { teardown(&temp_dir).expect("Failed to clean up after test"); } + +#[test] +fn test_get_balance() { + let (manager, temp_dir) = setup().expect("Failed to set up test environment"); + + let wallet_path = temp_dir.path().join("wallet_name"); + let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); + + let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap(); + + let balance_result = wallet.get_balance(0); + assert!(balance_result.is_ok(), "Failed to get balance: {:?}", balance_result.err()); + + let _balance = balance_result.unwrap(); + // assert!(_balance.balance >= 0, "Balance should be non-negative"); + // assert!(_balance.unlocked_balance >= 0, "Unlocked balance should be non-negative"); + // These assertions are meaningless with the constraints of the type. + + teardown(&temp_dir).expect("Failed to clean up after test"); +} -- cgit v1.2.3