From f89b9521b617d98c3f2005461ef969fef3231cd7 Mon Sep 17 00:00:00 2001 From: Phanuella Date: Mon, 1 Jun 2026 00:34:59 +0000 Subject: [PATCH] fix: allowed match token entries --- contracts/escrow/src/lib.rs | 14 +++++++-- contracts/escrow/src/tests/ttl.rs | 50 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/contracts/escrow/src/lib.rs b/contracts/escrow/src/lib.rs index 325efe4..588fba5 100644 --- a/contracts/escrow/src/lib.rs +++ b/contracts/escrow/src/lib.rs @@ -112,10 +112,18 @@ impl EscrowContract { /// Check if a token is allowed. pub fn is_token_allowed(env: Env, token: Address) -> bool { - env.storage() + let key = DataKey::AllowedToken(token.clone()); + let allowed: bool = env + .storage() .persistent() - .get(&DataKey::AllowedToken(token)) - .unwrap_or(false) + .get(&key) + .unwrap_or(false); + if allowed { + env.storage() + .persistent() + .extend_ttl(&key, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + } + allowed } /// Create a new match. Both players must call `deposit` before the game starts. diff --git a/contracts/escrow/src/tests/ttl.rs b/contracts/escrow/src/tests/ttl.rs index 9688647..10349eb 100644 --- a/contracts/escrow/src/tests/ttl.rs +++ b/contracts/escrow/src/tests/ttl.rs @@ -24,6 +24,56 @@ fn test_ttl_extended_on_create_match() { assert_eq!(ttl, crate::MATCH_TTL_LEDGERS); } +#[test] +fn test_add_allowed_token_extends_ttl() { + let (env, contract_id, _oracle, _player1, _player2, token, _admin) = setup(); + let client = EscrowContractClient::new(&env, &contract_id); + + client.add_allowed_token(&token); + + let ttl = env.as_contract(&contract_id, || { + env.storage() + .persistent() + .get_ttl(&DataKey::AllowedToken(token.clone())) + }); + assert_eq!(ttl, crate::MATCH_TTL_LEDGERS); +} + +#[test] +fn test_is_token_allowed_extends_ttl_on_read() { + let (env, contract_id, _oracle, _player1, _player2, token, _admin) = setup(); + let client = EscrowContractClient::new(&env, &contract_id); + + client.add_allowed_token(&token); + + env.ledger().set(soroban_sdk::testutils::LedgerInfo { + sequence_number: env.ledger().sequence() + 1000, + timestamp: env.ledger().timestamp() + 5000, + protocol_version: 22, + network_id: Default::default(), + base_reserve: 10, + min_temp_entry_ttl: 1, + min_persistent_entry_ttl: 1, + max_entry_ttl: crate::MATCH_TTL_LEDGERS + 2000, + }); + + let ttl_before = env.as_contract(&contract_id, || { + env.storage() + .persistent() + .get_ttl(&DataKey::AllowedToken(token.clone())) + }); + assert!(ttl_before < crate::MATCH_TTL_LEDGERS); + + assert!(client.is_token_allowed(&token)); + + let ttl_after = env.as_contract(&contract_id, || { + env.storage() + .persistent() + .get_ttl(&DataKey::AllowedToken(token.clone())) + }); + assert_eq!(ttl_after, crate::MATCH_TTL_LEDGERS); +} + #[test] fn test_ttl_extended_on_deposit() { let (env, contract_id, _oracle, player1, player2, token, _admin) = setup();