Arena Smart Contract Exposed: Re-entrancy Vulnerability in `claim` Function Threatens Complete Token Drain
A critical security flaw has been identified in the Arena smart contract's `claim` function, exposing the protocol to a complete token drain. The vulnerability is a classic Checks-Effects-Interactions violation, where the contract performs an external token transfer to a winner *before* updating its internal state to mark the claim as completed. This sequence creates a dangerous window for re-entrancy attacks, allowing a malicious actor to repeatedly call the `claim` function and drain all tokens from the contract before the system can register that the initial claim has been processed.
The flaw resides in the `contract/arena/src/lib.rs` file. The problematic logic executes `token_client.transfer(...)` to send rewards, and only afterwards sets the `Claimed(player)` storage key to `true`. If the token being transferred supports callback mechanisms or re-entrancy, a player can exploit this gap. By re-entering the `claim` function in the middle of the initial transfer, the attacker bypasses the intended safeguard against double-claims, as the contract's state has not yet been updated to reflect the first payout.
The proposed fix is straightforward but critical: reorder the operations to follow the secure pattern. The state update (`storage(&env).set(&DataKey::Claimed(player.clone()), &true);`) must be moved to occur **before** the external `token_client.transfer(...)` call. This simple change ensures the contract's internal ledger is locked before any value leaves the system, neutralizing the re-entrancy vector. The vulnerability underscores the persistent risks in DeFi and gaming contracts where financial logic and state management must be meticulously sequenced to prevent catastrophic financial losses.