From 959ba20670ca999fe750058f5a9ed6ddc8b0aae4 Mon Sep 17 00:00:00 2001 From: starkiVector-090 Date: Mon, 1 Jun 2026 16:12:46 +0000 Subject: [PATCH] feat: list allowed tokens --- contracts/escrow/src/lib.rs | 71 +++++++++++++++++++++++++++++++++++ contracts/escrow/src/types.rs | 1 + 2 files changed, 72 insertions(+) diff --git a/contracts/escrow/src/lib.rs b/contracts/escrow/src/lib.rs index c42ece9..b53c96f 100644 --- a/contracts/escrow/src/lib.rs +++ b/contracts/escrow/src/lib.rs @@ -91,6 +91,7 @@ impl EscrowContract { env.storage() .persistent() .extend_ttl(&DataKey::AllowedToken(token.clone()), MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + Self::append_allowed_token(&env, &token); env.storage().instance().set(&DataKey::AllowlistEnforced, &true); env.events().publish( @@ -112,6 +113,7 @@ impl EscrowContract { env.storage() .persistent() .remove(&DataKey::AllowedToken(token.clone())); + Self::remove_allowed_token_from_list(&env, &token); env.events().publish( (Symbol::new(&env, "admin"), symbol_short!("token_remove")), @@ -132,10 +134,79 @@ impl EscrowContract { env.storage() .persistent() .extend_ttl(&key, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + if env.storage().persistent().has(&DataKey::AllowedTokens) { + env.storage() + .persistent() + .extend_ttl(&DataKey::AllowedTokens, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + } } allowed } + /// Return the current allowlist as an ordered list. + pub fn get_allowed_tokens(env: Env) -> Result, Error> { + Ok(Self::get_allowed_token_list(&env)) + } + + fn get_allowed_token_list(env: &Env) -> soroban_sdk::Vec
{ + if let Some(allowed_tokens) = env + .storage() + .persistent() + .get(&DataKey::AllowedTokens) + { + env.storage() + .persistent() + .extend_ttl(&DataKey::AllowedTokens, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + allowed_tokens + } else { + soroban_sdk::vec![env] + } + } + + fn set_allowed_token_list(env: &Env, allowed_tokens: &soroban_sdk::Vec
) { + if allowed_tokens.is_empty() { + env.storage().persistent().remove(&DataKey::AllowedTokens); + } else { + env.storage() + .persistent() + .set(&DataKey::AllowedTokens, allowed_tokens); + env.storage() + .persistent() + .extend_ttl(&DataKey::AllowedTokens, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + } + } + + fn append_allowed_token(env: &Env, token: &Address) { + let mut allowed_tokens: soroban_sdk::Vec
= env + .storage() + .persistent() + .get(&DataKey::AllowedTokens) + .unwrap_or_else(|| soroban_sdk::vec![env]); + if !allowed_tokens.iter().any(|existing| existing == token) { + allowed_tokens.push_back(token.clone()); + Self::set_allowed_token_list(env, &allowed_tokens); + } else if env.storage().persistent().has(&DataKey::AllowedTokens) { + env.storage() + .persistent() + .extend_ttl(&DataKey::AllowedTokens, MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS); + } + } + + fn remove_allowed_token_from_list(env: &Env, token: &Address) { + let allowed_tokens = Self::get_allowed_token_list(env); + if allowed_tokens.is_empty() { + return; + } + + let mut updated = soroban_sdk::vec![env]; + for existing in allowed_tokens.iter() { + if existing != token { + updated.push_back(existing.clone()); + } + } + Self::set_allowed_token_list(env, &updated); + } + /// Create a new match. Both players must call `deposit` before the game starts. /// /// # Parameters diff --git a/contracts/escrow/src/types.rs b/contracts/escrow/src/types.rs index 5d92733..4254128 100644 --- a/contracts/escrow/src/types.rs +++ b/contracts/escrow/src/types.rs @@ -56,6 +56,7 @@ pub enum DataKey { PlayerMatches(Address), MatchTimeout, AllowedToken(Address), + AllowedTokens, AllowlistEnforced, OracleRecord(u64), }