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
2 changes: 2 additions & 0 deletions contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl EscrowContract {
env.storage().instance().set(&DataKey::Admin, &admin);
env.storage().instance().set(&DataKey::MatchCount, &0u64);
env.storage().instance().set(&DataKey::Paused, &false);
env.storage().instance().set(&DataKey::AllowlistEnforced, &false);
env.storage().instance().set(&DataKey::AllowedTokenCount, &0u64);
}

/// Pause the contract — admin only. Blocks create_match, deposit, and submit_result.
Expand Down
4 changes: 2 additions & 2 deletions contracts/escrow/src/tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ fn test_remove_allowed_token_emits_event() {
let expected_topics = vec![
&env,
Symbol::new(&env, "admin").into_val(&env),
symbol_short!("token_removed").into_val(&env),
symbol_short!("token_remove").into_val(&env),
];
let matched = events
.iter()
.find(|(_, topics, _)| *topics == expected_topics);
assert!(matched.is_some(), "token_removed event not emitted");
assert!(matched.is_some(), "token_remove event not emitted");

let (_, _, data) = matched.unwrap();
let ev_token: Address = TryFromVal::try_from_val(&env, &data).unwrap();
Expand Down
63 changes: 59 additions & 4 deletions contracts/escrow/src/tests/token_allowlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ fn test_add_allowed_token_emits_event() {
}

#[test]
fn test_removed_tokens_can_no_longer_be_used_for_new_matches() {
fn test_removed_tokens_are_rejected_when_other_allowed_tokens_remain() {
let (env, contract_id, _oracle, player1, player2, token, _admin) = setup();
let client = EscrowContractClient::new(&env, &contract_id);

let token2_id = env.register_stellar_asset_contract_v2(Address::generate(&env));
let token2_addr = token2_id.address();
let asset_client2 = StellarAssetClient::new(&env, &token2_addr);
asset_client2.mint(&player1, &1000);
asset_client2.mint(&player2, &1000);

client.add_allowed_token(&token);
client.add_allowed_token(&token2_addr);
client.remove_allowed_token(&token);

let result = client.try_create_match(
Expand All @@ -49,10 +56,58 @@ fn test_removed_tokens_can_no_longer_be_used_for_new_matches() {
&String::from_str(&env, "removed_token_game"),
&Platform::Lichess,
);
assert!(
result.is_err(),
"create_match should reject removed token"
assert!(result.is_err(), "create_match should reject removed token");

let id = client.create_match(
&player1,
&player2,
&100,
&token2_addr,
&String::from_str(&env, "remaining_token_game"),
&Platform::Lichess,
);
assert_eq!(id, 0, "remaining allowed token should still be accepted");
}

#[test]
fn test_removing_last_allowed_token_disables_allowlist_enforcement() {
let (env, contract_id, _oracle, player1, player2, token, _admin) = setup();
let client = EscrowContractClient::new(&env, &contract_id);

client.add_allowed_token(&token);
client.remove_allowed_token(&token);

assert!(!client.is_token_allowed(&token));

let unknown_token = Address::generate(&env);
let id = client.create_match(
&player1,
&player2,
&100,
&unknown_token,
&String::from_str(&env, "rollback_game"),
&Platform::Lichess,
);
assert_eq!(id, 0, "create_match should accept any token after last allowed token is removed");
}

#[test]
fn test_remove_allowed_token_requires_admin_auth() {
let (env, contract_id, _oracle, _player1, _player2, token, _admin) = setup();
let client = EscrowContractClient::new(&env, &contract_id);

let attacker = Address::generate(&env);
env.mock_auths(&[MockAuth {
address: &attacker,
invoke: &MockAuthInvoke {
contract: &contract_id,
fn_name: "remove_allowed_token",
args: (token.clone(),).into_val(&env),
sub_invokes: &[],
},
}]);

assert_eq!(client.try_remove_allowed_token(&token), Err(Ok(Error::Unauthorized)));
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions contracts/escrow/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub enum DataKey {
PlayerMatches(Address),
MatchTimeout,
AllowedToken(Address),
AllowedTokenCount,
AllowlistEnforced,
AllowedTokenCount,
OracleRecord(u64),
Expand Down
2 changes: 1 addition & 1 deletion docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ stellar contract invoke \
--token <USDC_CONTRACT_ADDRESS>
```

> **Note:** After the first `add_allowed_token` call, `AllowlistEnabled` is set to `true` on-chain and cannot be unset. Any token not explicitly added will be rejected by `create_match`.
> **Note:** After the first `add_allowed_token` call, allowlist enforcement becomes active. If the last allowed token is removed, enforcement is disabled again and `create_match` accepts any token.

---

Expand Down
Loading