A decentralized Rock, Paper, Scissors game smart contract designed to run on Abstract Chain. This contract implements a commitment scheme to ensure fair play and prevent cheating.
- Fair Play: Uses cryptographic commitments to prevent players from seeing each other's moves before committing
- Betting System: Players can bet native tokens (ETH/ABST) on game outcomes
- Timeout Mechanism: Games automatically timeout if players don't complete their moves
- Tie Handling: Proper handling of ties with fund distribution
- Gas Efficient: Optimized for minimal gas consumption on Abstract Chain
- Game Creation: Player 1 creates a game with a bet amount
- Game Joining: Player 2 joins the game with matching bet amount
- Move Commitment: Both players commit their moves using cryptographic hashes
- Move Revelation: Players reveal their moves with the original nonce
- Winner Determination: Smart contract determines winner and distributes funds
createGame()- Create a new game with bet amountjoinGame(uint256 gameId)- Join an existing gamecommitMove(uint256 gameId, bytes32 commitment)- Commit a moverevealMove(uint256 gameId, Move move, bytes32 nonce)- Reveal a movetimeoutGame(uint256 gameId)- Timeout a game after deadline
getGame(uint256 gameId)- Get game detailsgetPlayerGames(address player)- Get all games for a playergetAvailableGames()- Get all available games to join
setMinBet(uint256 _minBet)- Set minimum bet amountsetMaxBet(uint256 _maxBet)- Set maximum bet amountsetGameTimeout(uint256 _timeout)- Set game timeout duration
- Clone the repository:
git clone https://github.com/rustjesty/abstract-chain-rps-game-smart-contract
cd abstract-chain-rps-game-smart-contract- Install dependencies:
npm install- Copy environment file and configure:
cp env.example .env- Edit
.envfile with your Abstract Chain configuration:
# Abstract Chain Configuration
ABSTRACT_RPC_URL=https://rpc.abstractchain.com
ABSTRACT_CHAIN_ID=1234
PRIVATE_KEY=your_private_key_hereUpdate the hardhat.config.js file with the correct Abstract Chain parameters:
networks: {
abstract: {
url: process.env.ABSTRACT_RPC_URL,
accounts: [process.env.PRIVATE_KEY],
chainId: process.env.ABSTRACT_CHAIN_ID,
},
abstractTestnet: {
url: process.env.ABSTRACT_TESTNET_RPC_URL,
accounts: [process.env.PRIVATE_KEY],
chainId: process.env.ABSTRACT_TESTNET_CHAIN_ID,
}
}ABSTRACT_RPC_URL: Abstract Chain RPC endpointABSTRACT_CHAIN_ID: Abstract Chain network IDPRIVATE_KEY: Your wallet private key for deploymentABSTRACT_EXPLORER_API_KEY: Explorer API key for contract verification
npm run compilenpm run testnpm run deploy:localnpm run deploy:testnetnpm run deploy:mainnetnpm run verify:testnet
npm run verify:mainnetMoves are encoded as follows:
- 0: None (invalid)
- 1: Rock
- 2: Paper
- 3: Scissors
Players commit their moves using:
commitment = keccak256(abi.encodePacked(move, nonce, playerAddress))- Rock beats Scissors
- Paper beats Rock
- Scissors beats Paper
- Same moves result in a tie
- Winner: Receives both players' bets (minus gas fees)
- Tie: Each player receives their original bet back
- Timeout: Each player receives their original bet back
- Commitment Scheme: Prevents front-running and move manipulation
- Timeout Mechanism: Prevents games from being stuck indefinitely
- Access Control: Only game participants can perform game actions
- State Validation: Ensures games follow proper state transitions
- Fund Safety: Secure fund handling with proper withdrawal mechanisms
The contract is optimized for Abstract Chain with:
- Efficient storage patterns
- Minimal external calls
- Optimized loops and data structures
- Batch operations where possible
Run the comprehensive test suite:
npm run testTests cover:
- Game creation and joining
- Move commitment and revelation
- Winner determination
- Timeout handling
- Edge cases and error conditions
Before deploying to Abstract Chain:
- Update RPC URLs in configuration
- Set correct chain IDs
- Configure explorer settings
- Test on local network
- Test on testnet
- Verify contract source code
- Set appropriate gas limits
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions about Abstract Chain integration, please refer to the Abstract Chain documentation or create an issue in this repository.
This smart contract is provided as-is for educational and development purposes. Always audit smart contracts before using them with real funds on mainnet.