This repository contains smart contracts designed to handle payments and rewards for onchain games. The contracts provide a secure and flexible way to manage entry fees and rewards in both ETH and USDC.
The contracts serve as the backend logic for onchain games, handling:
- Entry fee collection (ETH or USDC)
- Reward distribution
- Contract funding and management
- Balance tracking
The base contract that provides core functionality for game payments and rewards:
- Entry fee collection in ETH or USDC
- Reward recording and claiming
- Owner-only functions for contract management
- Balance tracking for both ETH and USDC
An enhanced version of GameAirdrop that adds signature-based reward verification:
- All features from GameAirdrop
- Signature-based reward recording for enhanced security
- Protection against replay attacks
- Deadline-based signature validation
- Configurable entry fees for both ETH and USDC
- Owner can update fees independently
- Default entry fees:
- ETH: 0.0001 ETH
- USDC: 0.10 USDC
- Configurable reward amounts for both ETH and USDC
- Default rewards:
- ETH: 0.0001 ETH
- USDC: 1 USDC
- Secure reward claiming mechanism
- Owner-only functions for critical operations
- Signature verification for reward recording (GameAirdropSigned)
- Protection against replay attacks
- Safe token transfers using OpenZeppelin's SafeERC20
- Deploy the contract (either GameAirdrop or GameAirdropSigned)
- Fund the contract with ETH and/or USDC
- Players can start games by paying the entry fee
- Record rewards for successful players
- Players can claim their rewards
Built with:
- Solidity ^0.8.19
- OpenZeppelin Contracts
- Foundry for testing and deployment
MIT
- Clone the repository:
git clone <your-repo-url>
cd game-contracts- Install dependencies:
forge installCreate a .env file with the following variables:
BASE_RPC_URL=<your-base-rpc-url>
BASE_SEPOLIA_RPC_URL=<your-base-sepolia-rpc-url>
BASESCAN_API_KEY=<your-basescan-api-key>
PRIVATE_KEY=<your-private-key>
To deploy to Base Sepolia testnet:
source .env
forge script script/Deploy.s.sol:DeployScript --rpc-url $BASE_SEPOLIA_RPC_URL --broadcast --verify -vvvvTo deploy to Base mainnet:
source .env
forge script script/Deploy.s.sol:DeployScript --rpc-url $BASE_RPC_URL --broadcast --verify -vvvv
forge script script/Deploy.s.sol:DeployScript --rpc-url $BASE_RPC_URL --broadcast --verify -vvvvRun the test suite:
forge testRun tests with gas reporting:
forge test --gas-report-
Play a game by calling
playGame:- Pay with ETH: Send ETH along with the call
- Pay with USDC: Approve the contract first, then call without ETH
-
Claim rewards using
claimReward:- Specify whether claiming ETH or USDC rewards
- Rewards are sent directly to the player's wallet
- Withdraw accumulated funds using
ownerWithdraw - Monitor game activity through emitted events
- ReentrancyGuard for all external functions
- Pull-based reward claiming
- SafeERC20 for token transfers
- Ownable access control
Contributions are welcome! Please feel free to submit a Pull Request.