Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, Error> {
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<i128, Error> {
let m: Match = env
Expand All @@ -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)
}

Expand Down
23 changes: 23 additions & 0 deletions contracts/escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading