diff --git a/contracts/escrow/src/lib.rs b/contracts/escrow/src/lib.rs index 53da896..1ee5b5d 100644 --- a/contracts/escrow/src/lib.rs +++ b/contracts/escrow/src/lib.rs @@ -641,6 +641,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 @@ -652,8 +662,7 @@ 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) } diff --git a/contracts/escrow/src/tests.rs b/contracts/escrow/src/tests.rs index bfbc0d0..71e1eec 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();