Skip to content
Open
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
10 changes: 10 additions & 0 deletions contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,16 @@ impl EscrowContract {
Ok(depositors * m.stake_amount)
}

/// Return the ledger interval between match creation and terminal completion/cancellation.
pub fn get_match_duration(env: Env, match_id: u64) -> Result<Option<u32>, Error> {
let m: Match = env
.storage()
.persistent()
.get(&DataKey::Match(match_id))
.ok_or(Error::MatchNotFound)?;
Ok(m.completed_ledger.map(|completed| completed.saturating_sub(m.created_ledger)))
}

/// Return all matches that are in Active state (fully funded).
pub fn get_live_matches(env: Env) -> Result<soroban_sdk::Vec<Match>, Error> {
let mut live_matches = soroban_sdk::vec![&env];
Expand Down
47 changes: 47 additions & 0 deletions contracts/escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,53 @@ fn test_cancel_match_sets_completed_ledger() {
assert!(completed >= ledger_before && completed <= ledger_after);
}

#[test]
fn test_get_match_duration_after_cancelled_match() {
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, "duration_cancel_test"),
&Platform::Lichess,
);

client.cancel_match(&id, &player1);
let duration = client.get_match_duration(&id).unwrap();
let m = client.get_match(&id);

assert_eq!(duration, Some(m.completed_ledger.unwrap().saturating_sub(m.created_ledger)));
assert!(duration.is_some());
}

#[test]
fn test_get_match_duration_after_completion() {
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, "duration_complete_test"),
&Platform::Lichess,
);

client.deposit(&id, &player1);
client.deposit(&id, &player2);
client.submit_result(&id, &Winner::Player1, &oracle);

let duration = client.get_match_duration(&id).unwrap();
let m = client.get_match(&id);

assert_eq!(duration, Some(m.completed_ledger.unwrap().saturating_sub(m.created_ledger)));
assert!(duration.is_some());
}

#[test]
fn test_expire_match_sets_completed_ledger() {
let (env, contract_id, _oracle, player1, player2, token, _admin) = setup();
Expand Down