Skip to content

Bisonai/orakl-demo-flip-coin

Repository files navigation

Flip Coin

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

What is Flip Coin Game?

"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.

Development

1. Create Orakl Network Account

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.

2. Deploy Smart Contracts

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 deployment
  • ACCOUNT_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.

3. Launch Backend (optional)

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 deployed FlipCoin smart contract

Install dependencies, and launch backend.

yarn install
yarn start

4. Launch Frontend

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, 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

Docker

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

License

MIT License