This repository contains a simple flip coin game utilizing Orakl Network Verifiable Randomness Function (VRF). VRF is deployed on Klaytn mainnet (Cypress) and testnet (Baobab), and this repository is compatible with both.
![image](https://private-user-images.githubusercontent.com/2312761/320178147-3ff7a81d-5ca3-4e28-a1d2-876fe092042d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNzUzNTYsIm5iZiI6MTcyMjE3NTA1NiwicGF0aCI6Ii8yMzEyNzYxLzMyMDE3ODE0Ny0zZmY3YTgxZC01Y2EzLTRlMjgtYTFkMi04NzZmZTA5MjA0MmQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcyOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MjhUMTM1NzM2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWZkYmNjZWFjYWFhYzUzMGExYWM4MjZiNjk0Mjc4MDk2NjgyMmJlYWI5MDJhZWYyZTdlMTNmZmViOTY0OGUyZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.NyR6PVRxC50h9XkGNDOGtvJovDxtUJaAoBZNwrzYIzE)
"Flip Coin" is a betting game implemented as a Solidity smart contract. Users can bet any amount of $KLAY on the outcome of a random coin flip, with a 50% chance for heads and a 50% chance for tails. Randomness is generated using the Verifiable Randomness Function (VRF) provided by Orakl Network. If the bet is correct, the user is rewarded with twice the amount bet, otherwise, the smart contract retains the user's bet. After the user ends the game, all $KLAY can be claimed at once.
FlipCoin.sol requires Orakl Network Permanent Account.
You can create one through https://orakl.network/account.
Once you have successfully created an account, you will be prompted to "Add Consumer" (which will be possible after the FlipCoin
smart contract is deployed) and to "Deposit $KLAY" into your account.
The $KLAY in your account will be used as payment for VRF requests.
If you do not have $KLAY in your account, you won't be able to request VRF, and the Flip Coin game will not function.
$KLAY tokens can be requested through Baobab faucet.
Navigate to the contracts
directory.
cd contracts
Install dependencies.
yarn install
Create an .env
file and specify the environment variables below.
PRIV_KEY=
ACCOUNT_ID=
PRIV_KEY
- private key that will be utilized for smart contract deploymentACCOUNT_ID
- Orakl Network account ID (can be found at https://orakl.network/account)
Deploy smart contracts on Baobab network by executing the command below.
yarn deploy baobab
After successfull execution you should be able to see output similar to the following.
$ hardhat run scripts/deploy.ts --network baobab
Creating Typechain artifacts in directory typechain for target ethers-v5
Successfully generated Typechain artifacts!
Deployer 0xa37AcA2eaf7dcc199820Dc17689a17839B7510e9
FlipCoin 0x0458E0244E23B4663B4a28671EC4bfA3BbD3628F
Finally, you need to add the address of your deployed FlipCoin
contract as a consumer to your Orakl Network account, and deposit $KLAY tokens into the FlipCoin
contract to make it possible to win.
Backend is used for event data collection of players' bets. Collected bet information are displayed in frontend leaderboard.
Navigate to the backend
directory.
cd backend
Create an .env
file and specify the parameters below.
RPC_URL=
FLIPCOIN_ADDRESS=
RPC_URL
- JSON-RPC url that is used to communicate with klaytn blockchain (Cypress JSON-RPC, Baobab JSON-RPC)FLIPCOIN_ADDRESS
- address of deployedFlipCoin
smart contract
Install dependencies, and launch backend.
yarn install
yarn start
Navigate to the frontend
directory.
cd frontend
Install dependencies.
yarn install
Create an .env
file and specify the parameters below.
NEXT_PUBLIC_EXPLORER=
NEXT_PUBLIC_RPC_URL=
NEXT_PUBLIC_FLIPCOIN_ADDRESS=
NEXT_PUBLIC_EXPLORER
- url of klaytn blockchain explorer (Cypress block explorer, Baobab block explorer)NEXT_PUBLIC_RPC_URL
- JSON-RPC url that is used to communicate with klaytn blockchain (Cypress JSON-RPC, Baobab JSON-RPC)NEXT_PUBLIC_FLIPCOIN_ADDRESS
- address of deployedFlipCoin
smart contract
Next, you can start the website in a development mode.
yarn dev
Or you can build it first, and then launch in a production mode.
yarn build
yarn start
Ensure Docker service is installed and running on your local machine.
brew install docker
brew install docker-compose
Create a volume for storing backend data.
docker volume create leaderboard
Build images (frontend + backend).
docker-compose -f docker-compose.yml build
Launch containers (frontend + backend).
docker-compose -f docker-compose.yml up