From b49b1a7f5859a2562e30fa61f59c6492a9e575ed Mon Sep 17 00:00:00 2001 From: Elliot Clement Date: Sun, 31 May 2026 23:23:24 +0000 Subject: [PATCH] refac: add bolean arithmetic --- contracts/escrow/src/lib.rs | 17 +++++++++++++++-- contracts/escrow/src/tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/contracts/escrow/src/lib.rs b/contracts/escrow/src/lib.rs index 325efe4..65e773d 100644 --- a/contracts/escrow/src/lib.rs +++ b/contracts/escrow/src/lib.rs @@ -627,6 +627,16 @@ impl EscrowContract { Ok(m.player1_deposited && m.player2_deposited) } + /// Return the number of players who have deposited for a match (0, 1, or 2). + pub fn get_depositor_count(env: Env, match_id: u64) -> Result { + let m: Match = env + .storage() + .persistent() + .get(&DataKey::Match(match_id)) + .ok_or(Error::MatchNotFound)?; + Ok(Self::depositor_count(&m) as u32) + } + /// Return the total escrowed balance for a match (0, 1x, or 2x stake). pub fn get_escrow_balance(env: Env, match_id: u64) -> Result { let m: Match = env @@ -638,11 +648,14 @@ impl EscrowContract { return Ok(0); } // Count depositors explicitly — avoids fragile bool-to-integer casting. - let depositors: i128 = if m.player1_deposited { 1 } else { 0 } - + if m.player2_deposited { 1 } else { 0 }; + let depositors: i128 = Self::depositor_count(&m); Ok(depositors * m.stake_amount) } + fn depositor_count(m: &Match) -> i128 { + if m.player1_deposited { 1 } else { 0 } + if m.player2_deposited { 1 } else { 0 } + } + /// Return all matches that are in Active state (fully funded). pub fn get_live_matches(env: Env) -> Result, Error> { let mut live_matches = soroban_sdk::vec![&env]; diff --git a/contracts/escrow/src/tests.rs b/contracts/escrow/src/tests.rs index a060b26..bb79dee 100644 --- a/contracts/escrow/src/tests.rs +++ b/contracts/escrow/src/tests.rs @@ -106,6 +106,29 @@ fn test_deposit_and_activate() { assert_eq!(client.get_escrow_balance(&id), 200); } +#[test] +fn test_get_depositor_count_tracks_player_deposits() { + let (env, contract_id, _oracle, player1, player2, token, _admin) = setup(); + let client = EscrowContractClient::new(&env, &contract_id); + + let id = client.create_match( + &player1, + &player2, + &100, + &token, + &String::from_str(&env, "depositor_count_test"), + &Platform::Lichess, + ); + + assert_eq!(client.get_depositor_count(&id), 0); + + client.deposit(&id, &player1); + assert_eq!(client.get_depositor_count(&id), 1); + + client.deposit(&id, &player2); + assert_eq!(client.get_depositor_count(&id), 2); +} + #[test] fn test_deposit_emits_activated_event() { let (env, contract_id, _oracle, player1, player2, token, _admin) = setup();