diff options
Diffstat (limited to 'impls')
| -rw-r--r-- | impls/monero.rs/src/lib.rs | 56 | ||||
| -rw-r--r-- | impls/monero.rs/tests/integration_tests.rs | 44 |
2 files changed, 95 insertions, 5 deletions
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<GetBalance> { + 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"); +} diff --git a/impls/monero.rs/tests/integration_tests.rs b/impls/monero.rs/tests/integration_tests.rs index 6aeeab0..ec7cae2 100644 --- a/impls/monero.rs/tests/integration_tests.rs +++ b/impls/monero.rs/tests/integration_tests.rs @@ -88,7 +88,7 @@ fn test_wallet_manager_creation() { let wallet_path = temp_dir.path().join("test_wallet"); let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); - let wallet_result = manager.create_wallet(wallet_str, "password123", "English", NetworkType::Mainnet); + let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet); assert!(wallet_result.is_ok(), "WalletManager creation seems to have failed"); teardown(&temp_dir).expect("Failed to clean up after test"); @@ -119,7 +119,7 @@ fn test_get_seed() { let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); let wallet = manager - .create_wallet(wallet_str, "password123", "English", NetworkType::Mainnet) + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) .expect("Failed to create wallet"); // Test getting seed without offset. @@ -151,7 +151,7 @@ fn test_get_address() { let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); let wallet = manager - .create_wallet(wallet_str, "password123", "English", NetworkType::Mainnet) + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) .expect("Failed to create wallet"); println!("Attempting to get address..."); let start = Instant::now(); @@ -173,7 +173,7 @@ fn test_is_deterministic() { let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); let wallet = manager - .create_wallet(wallet_str, "password123", "English", NetworkType::Mainnet) + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) .expect("Failed to create wallet"); println!("Checking if wallet is deterministic..."); let start = Instant::now(); @@ -221,7 +221,7 @@ fn test_multiple_address_generation() { let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); let wallet = manager - .create_wallet(wallet_str, "password123", "English", NetworkType::Mainnet) + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) .expect("Failed to create wallet"); for i in 0..5 { @@ -305,3 +305,37 @@ fn test_open_wallet_integration() { // Clean up. teardown(&temp_dir).expect("Failed to clean up after test"); } + +#[test] +fn test_get_balance_integration() { + println!("Running test_get_balance_integration"); + let (manager, temp_dir) = setup().expect("Failed to set up test environment"); + + // Construct the full path for the wallet within temp_dir. + let wallet_path = temp_dir.path().join("test_wallet"); + let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); + + // Create the wallet. + let wallet = manager + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) + .expect("Failed to create wallet"); + + // Fetch the balance. + println!("Fetching wallet balance..."); + let start = Instant::now(); + let balance_result = wallet.get_balance(0); // Account index 0 + println!("Fetching balance took {:?}", start.elapsed()); + + assert!(balance_result.is_ok(), "Failed to fetch balance: {:?}", balance_result.err()); + + let balance = balance_result.unwrap(); + println!("Balance: {:?}", balance); + + // Ensure the balance values make sense. + // 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. + // TODO: Test with scanning integration. + + teardown(&temp_dir).expect("Failed to clean up after test"); +} |
