My name is Jose Dennis, and I am a developer exploring the world of blockchain and smart contracts. ChiaRPSGame is my first project involving Chialisp and smart contracts, and it represents my initial steps into this exciting field.
Feel free to connect with me on Twitter: @MrDennisV
If you would like to support my journey, consider making a donation to my XCH address:
xch1a63283n8rh7yksz03774s5jaq2rw5f4je5w3f7ux7esw9y6allusdrlhg5
Alternatively, you can purchase an NFT from my collection AMLITOVERSE: AMLITOVERSE on MintGarden
Thank you for your support!
ChiaRPSGame is a project designed to practice what has been studied about Chialisp and smart contracts. This project allows playing the classic Rock, Paper, Scissors game using smart contracts on the Chia blockchain.
You can check out the demo at: chiarps.mrdennis.dev Offline
ChiaRPSGame leverages the unique state management features of the Chia blockchain, utilizing a coin set model similar to Bitcoin's UTXO model. Instead of an account balance, players have a collection of unspent coins that can be spent, making everything in the Chia blockchain represented as coins. This approach introduces unique methods for creating decentralized applications.
- GameWallet.clsp: This smart contract acts as a virtual wallet for players to initiate or join games. It handles actions like cashing out, creating a game, and sending coins to the game.
- Game.clsp: This smart contract controls the game's logic. It manages the different stages of the game, such as player joining, revealing selections, closing the game, and claiming wins.
- PublicOracle.clsp: This smart contract acts as a public ledger that tracks all the games that have been played. It records the state of each game, including when games are opened, joined, and closed. This allows for transparency and ensures that all players can verify the integrity of the game history.
- PlayerOracle.clsp: This smart contract functions similarly to the PublicOracle but focuses on individual player activities. It tracks the actions of each player, such as game participation and outcomes. This personalized history allows players to review their performance and keep a record of their game activities.
- RPSDriver: This Python class serves as the main interface for interacting with the smart contracts and managing the game logic.
- Game Initialization: Players load their virtual wallet with coins. The
GameWallet.clsp
contract handles the creation of a game, which involves placing a bet and providing a secret selection (Rock, Paper, or Scissors). - Player Actions:
- Join Player 1: The first player initiates a game by locking their bet and selection into a commitment.
- Join Player 2: A second player joins the game by placing a matching bet and making their selection.
- Game Progression: The game transitions through various states, each represented by coins that reflect the current game state.
- Revelation and Resolution: The first player reveals their selection using their private key(passphrase). The game contract (
Game.clsp
) then determines the winner or if the game is a tie, based on both players' selections. - Closure and Claims: If the first player does not reveal their selection within a specified time(24 hrs), the second player can claim the bet. The game can also be closed if no opponent joins within a set time frame(24 hrs).
-
Clone the repository:
git clone https://github.com/jjldv/ChiaRPSGame.git cd ChiaRPSGame
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Set your private key as an environment variable:
- On Linux/Mac OS:
export HEX_PRIVATE_KEY="your_private_key"
- On Windows:
set HEX_PRIVATE_KEY=your_private_key
- On PowerShell:
$env:HEX_PRIVATE_KEY="your_private_key"
- On Linux/Mac OS:
-
Run the application:
uvicorn app:app --reload --host 0.0.0.0 --port 443 --ssl-certfile path_to_cert/fullchain.pem --ssl-keyfile path_to_cert/privkey.pem