summaryrefslogtreecommitdiff
path: root/impls/monero.rs/src/lib.rs
diff options
context:
space:
mode:
authorsneurlax <sneurlax@gmail.com>2024-10-16 22:13:29 -0500
committersneurlax <sneurlax@gmail.com>2024-10-16 22:13:29 -0500
commit7a90b8ffbcfd812c2fb2df83b66450522903d1cc (patch)
treeea441f22ea17b51d073f2f44c0ad80f4424489c3 /impls/monero.rs/src/lib.rs
parentff39a2876b6708dafefe84d6a04237fa13b64303 (diff)
add get_balance
Diffstat (limited to 'impls/monero.rs/src/lib.rs')
-rw-r--r--impls/monero.rs/src/lib.rs56
1 files changed, 56 insertions, 0 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");
+}