From 37a549f202634d02e5c951eeeba2e13beca2c912 Mon Sep 17 00:00:00 2001 From: Syed Ghufran Hassan Date: Sun, 21 Jun 2026 14:50:02 +0500 Subject: [PATCH 1/2] feat(credits): add balance verification endpoint - Add verify_balance function to enable credit availability checks without modifying state. This improves UX by allowing callers to validate sufficient credits before attempting spend operations, reducing failed transactions and providing audit visibility through balance verification events. --- contracts/profile/src/credits.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/contracts/profile/src/credits.rs b/contracts/profile/src/credits.rs index 5ce83dd..8ebfc2b 100644 --- a/contracts/profile/src/credits.rs +++ b/contracts/profile/src/credits.rs @@ -33,6 +33,29 @@ pub fn bootstrap(env: &Env, user: Address, op_id: BytesN<32>) -> Result<(), Erro Ok(()) } +pub fn verify_balance( + env: &Env, + user: Address, + amount: u32, + op_id: BytesN<32>, +) -> Result { + admin::require_events_contract(env)?; + admin::require_not_paused(env)?; + idempotency::require_unseen(env, &op_id)?; + + let profile = storage::get_profile(env, &user).ok_or(Error::ProfileNotFound)?; + let has_sufficient = profile.credits >= amount; + + evt::BalanceVerified { + user, + amount, + has_sufficient, + }.publish(env); + + idempotency::mark_seen(env, &op_id); + Ok(has_sufficient) +} + pub fn spend( env: &Env, user: Address, @@ -49,11 +72,20 @@ pub fn spend( idempotency::mark_seen(env, &op_id); return Ok(()); } + let mut profile = storage::get_profile(env, &user).ok_or(Error::ProfileNotFound)?; if profile.credits < amount { return Err(Error::InsufficientCredits); } + + // Use verify_balance logic directly + let mut profile = storage::get_profile(env, &user).ok_or(Error::ProfileNotFound)?; + if profile.credits < amount { + return Err(Error::InsufficientCredits); + } + + profile.credits -= amount; storage::set_profile(env, &user, &profile); From 9dbf96868ab6fe1c09cd988032c9d068c9ee124d Mon Sep 17 00:00:00 2001 From: Syed Ghufran Hassan Date: Sun, 21 Jun 2026 18:49:32 +0500 Subject: [PATCH 2/2] Update credits.rs Modified file --- contracts/profile/src/credits.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contracts/profile/src/credits.rs b/contracts/profile/src/credits.rs index 8ebfc2b..fcbc140 100644 --- a/contracts/profile/src/credits.rs +++ b/contracts/profile/src/credits.rs @@ -74,10 +74,7 @@ pub fn spend( } - let mut profile = storage::get_profile(env, &user).ok_or(Error::ProfileNotFound)?; - if profile.credits < amount { - return Err(Error::InsufficientCredits); - } + // Use verify_balance logic directly let mut profile = storage::get_profile(env, &user).ok_or(Error::ProfileNotFound)?;