From b01c802872e3153cebb0c063cfcdac181ee3b8ce Mon Sep 17 00:00:00 2001 From: JamesVictor-O Date: Tue, 24 Mar 2026 12:34:25 +0100 Subject: [PATCH] feat: expose health factor and position query entrypoints. Add read-only contract methods for user health factor and user position backed by analytics calculations, and add tests to verify query correctness against existing report data. Made-with: Cursor --- stellar-lend/contracts/hello-world/src/lib.rs | 12 +++++++++ .../hello-world/src/tests/views_test.rs | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/stellar-lend/contracts/hello-world/src/lib.rs b/stellar-lend/contracts/hello-world/src/lib.rs index bcb49965..50ef880a 100644 --- a/stellar-lend/contracts/hello-world/src/lib.rs +++ b/stellar-lend/contracts/hello-world/src/lib.rs @@ -42,8 +42,10 @@ mod bridge; // mod tests; use crate::deposit::DepositDataKey; +use crate::deposit::Position; use crate::risk_management::RiskManagementError; use crate::interest_rate::InterestRateError; +use crate::analytics::AnalyticsError; // ─── Admin helper ───────────────────────────────────────────────────────────── @@ -227,6 +229,16 @@ impl HelloContract { pub fn get_fee_config(env: Env) -> treasury::TreasuryFeeConfig { treasury::get_fee_config(&env) } + + /// Read-only user health factor query (collateral/debt in basis points). + pub fn get_health_factor(env: Env, user: Address) -> Result { + analytics::calculate_health_factor(&env, &user) + } + + /// Read-only user position query. + pub fn get_user_position(env: Env, user: Address) -> Result { + analytics::get_user_position_summary(&env, &user) + } } #[cfg(test)] diff --git a/stellar-lend/contracts/hello-world/src/tests/views_test.rs b/stellar-lend/contracts/hello-world/src/tests/views_test.rs index 974e94af..c0cc6c0b 100644 --- a/stellar-lend/contracts/hello-world/src/tests/views_test.rs +++ b/stellar-lend/contracts/hello-world/src/tests/views_test.rs @@ -159,6 +159,33 @@ fn test_health_factor_below_threshold() { assert_eq!(report.metrics.health_factor, 9000); } +#[test] +fn test_get_health_factor_query_matches_report() { + let env = create_test_env(); + let (contract_id, _admin, client) = setup_contract_with_admin(&env); + let user = Address::generate(&env); + + client.deposit_collateral(&user, &None, &100); + set_user_position(&env, &contract_id, &user, 15000, 10000, 0); + + let report = client.get_user_report(&user); + let health = client.get_health_factor(&user).unwrap(); + assert_eq!(health, report.metrics.health_factor); +} + +#[test] +fn test_get_user_position_query_returns_position() { + let env = create_test_env(); + let (contract_id, _admin, client) = setup_contract_with_admin(&env); + let user = Address::generate(&env); + + set_user_position(&env, &contract_id, &user, 4000, 1000, 0); + + let position = client.get_user_position(&user).unwrap(); + assert_eq!(position.collateral, 4000); + assert_eq!(position.debt, 1000); +} + #[test] fn test_health_factor_risk_level_reflected() { let env = create_test_env();