diff --git a/contracts/escrow/src/lib.rs b/contracts/escrow/src/lib.rs index e3b4c84..c42ece9 100644 --- a/contracts/escrow/src/lib.rs +++ b/contracts/escrow/src/lib.rs @@ -404,6 +404,8 @@ impl EscrowContract { } } + Self::remove_live_match(env.clone(), match_id); + m.state = MatchState::Completed; m.completed_ledger = Some(env.ledger().sequence()); Self::remove_active_match(&env, match_id); @@ -749,11 +751,12 @@ impl EscrowContract { .get(&DataKey::MatchCount) .unwrap_or(0); - for i in 0..count { + for i in 0..ids.len() { + let match_id = *ids.get(i).unwrap(); if let Ok(m) = env .storage() .persistent() - .get::(&DataKey::Match(i)) + .get::(&DataKey::Match(match_id)) { if m.state == state { matches.push_back(m); @@ -779,11 +782,16 @@ impl EscrowContract { let mut skipped = 0u32; let mut added = 0u32; - for i in 0..count { + for i in 0..ids.len() { + if skipped < offset { + skipped = skipped.saturating_add(1); + continue; + } + let match_id = *ids.get(i).unwrap(); if let Ok(m) = env .storage() .persistent() - .get::(&DataKey::Match(i)) + .get::(&DataKey::Match(match_id)) { if m.state != state { continue; diff --git a/contracts/escrow/src/tests.rs b/contracts/escrow/src/tests.rs index 71e1eec..99d459f 100644 --- a/contracts/escrow/src/tests.rs +++ b/contracts/escrow/src/tests.rs @@ -1784,4 +1784,37 @@ fn test_get_live_matches_returns_only_fully_funded_matches() { let live_matches = client.get_live_matches(); assert_eq!(live_matches.len(), 1); assert_eq!(live_matches.get(0).unwrap().id, active_id); + assert_ne!(live_matches.get(0).unwrap().id, pending_id); +} + +#[test] +fn test_get_active_matches_excludes_pending_matches() { + let (env, contract_id, _oracle, player1, player2, token, _admin) = setup(); + let client = EscrowContractClient::new(&env, &contract_id); + + // Create pending match and active match. + let pending_id = client.create_match( + &player1, + &player2, + &100, + &token, + &String::from_str(&env, "pending_match_exclusion"), + &Platform::Lichess, + ); + + let active_id = client.create_match( + &player1, + &player2, + &100, + &token, + &String::from_str(&env, "active_match_exclusion"), + &Platform::Lichess, + ); + client.deposit(&active_id, &player1); + client.deposit(&active_id, &player2); + + let active_matches = client.get_active_matches(); + assert_eq!(active_matches.len(), 1); + assert_eq!(active_matches.get(0).unwrap().id, active_id); + assert_ne!(active_matches.get(0).unwrap().id, pending_id); } diff --git a/contracts/escrow/src/tests/lifecycle.rs b/contracts/escrow/src/tests/lifecycle.rs index 8923246..566d9a0 100644 --- a/contracts/escrow/src/tests/lifecycle.rs +++ b/contracts/escrow/src/tests/lifecycle.rs @@ -973,7 +973,7 @@ fn test_expire_match_refunds_depositor_after_timeout() { .extend_ttl_for_code(token.clone(), MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); env.as_contract(&contract_id, || { env.storage().persistent().extend_ttl( - &DataKey::ActiveMatches, + &DataKey::LiveMatches, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS, ); @@ -1304,7 +1304,7 @@ fn test_get_match_returns_cancelled_after_expire_match() { } env.as_contract(&contract_id, || { env.storage().persistent().extend_ttl( - &DataKey::ActiveMatches, + &DataKey::LiveMatches, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS, ); @@ -1323,7 +1323,7 @@ fn test_get_match_returns_cancelled_after_expire_match() { } env.as_contract(&contract_id, || { env.storage().persistent().extend_ttl( - &DataKey::ActiveMatches, + &DataKey::LiveMatches, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS, ); @@ -1535,7 +1535,7 @@ fn test_expire_match_refunds_both_players_when_both_deposited_but_still_pending( .extend_ttl_for_code(token.clone(), MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); env.as_contract(&contract_id, || { env.storage().persistent().extend_ttl( - &DataKey::ActiveMatches, + &DataKey::LiveMatches, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS, ); diff --git a/contracts/escrow/src/tests/ttl.rs b/contracts/escrow/src/tests/ttl.rs index 5583167..8083f31 100644 --- a/contracts/escrow/src/tests/ttl.rs +++ b/contracts/escrow/src/tests/ttl.rs @@ -101,7 +101,7 @@ fn test_active_matches_ttl_refreshed_on_append_and_removal() { ); let ttl_after_append = env.as_contract(&contract_id, || { - env.storage().persistent().get_ttl(&DataKey::ActiveMatches) + env.storage().persistent().get_ttl(&DataKey::LiveMatches) }); assert_eq!(ttl_after_append, crate::MATCH_TTL_LEDGERS); @@ -122,7 +122,7 @@ fn test_active_matches_ttl_refreshed_on_append_and_removal() { client.submit_result(&match1, &Winner::Player1); let ttl_after_removal = env.as_contract(&contract_id, || { - env.storage().persistent().get_ttl(&DataKey::ActiveMatches) + env.storage().persistent().get_ttl(&DataKey::LiveMatches) }); assert_eq!(ttl_after_removal, crate::MATCH_TTL_LEDGERS); }