TreasureHunt is an on-chain game where players can stake ethers to participate in a treasure hunt game. It is a grid based game where players can move in four directions(LEFT, RIGHT, UP, DOWN). The contract uses chainlink's VRF(subscription method) to generate random number.
- Players will have to pay some ether in order to participate in the game.
- A treasure is located at any random position in the grid.
- To win the game, player's position should be equal to treasure position.
- Chainlink VRF integration for generating random number.
- Treasure Movement Condition:
- If a player lands on multiple of 5 position, the treasure moves to a random adjacent position.
- If a player lands on a prime number position, the treasure moves to any random position on the grid.
- Winner receives 90 percent of the total balance as reward and remaining 10 percent is kept for next round.
- Deployer must hold the absolute minimum subscription balance for subscriptionID to fetch random numbers.
participate(): Users can join the game by paying the participation fee.makeMove(Directions direction): Player move in the specified direction.endCurrentGame(): Ends the current game if it has exceeded the time limit provided by deployer.withdrawFunds(uint256 gameIndex): Withdraw participation fee after game expiry.
NewPlayerAdded: Emitted when a player joins the game.PlayerRelocated: Emitted when a player makes a move.TreasureRelocated: Emitted when the treasure changes position.Winner: Emitted when a player wins the game.GameBegins: Emitted when a new game round begins.GameOver: Emitted when a game expires without a winner.WithdrawFunds: Emitted when a player withdraws fund.
- Ensure you have the Chainlink VRF contracts and dependencies set up.
- Set up your environment variables:
- Rename
.env.exampleto.env - Fill in the required values in the
.envfile
- Rename
- Deploy the contract with the following parameters:
- VRF CoordinatorId
- SubscriptionId
- KeyHash of sepolia network
- Request confirmation blocks
- Game duration
- Participation Fee
The project uses a .env file to manage sensitive and configuration information. Follow these steps to set up:
- Locate the
.env.examplefile in the project root. - Create a copy of this file and rename it to
.env. - Open the
.envfile and fill in the following variables:
ALCHEMY_NODE_API_KEY=your_alchemy_api_key
DEPLOYER_PVT_KEY=your_private_key
ETHERSCAN_API_KEY=your_etherscan_api_key
VRF_COORDINATOR_ID=0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B
S_SUBSCRIPTION_ID=your_subscription_id
KEY_HASH=0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae
ALCHEMY_NODE_API_KEY: Your Alchemy API key for accessing Ethereum nodesDEPLOYER_PVT_KEY: The private key of the account deploying the contractETHERSCAN_API_KEY: Your Etherscan API key for contract verificationVRF_COORDINATOR_ID: VRF CoordinatorId for sepolia test networkS_SUBSCRIPTION_ID: Subscription Id generated by chainlinkKEY_HASH: Key Hash for sepolia test network
Important:
- Never commit your
.envfile to version control. - Keep your private keys and API keys secure.
To run the test suite for TreasureHunt, follow these steps:
-
Open a terminal and navigate to the project's root directory.
-
Run the following command:
npm testThis command will execute all the tests in the test suite.
-
The test results will be displayed in the terminal, showing which tests passed and which (if any) failed.
Make sure you have all the necessary dependencies installed before running the tests. All the required packages can be installed by running npm install.
- The contract employs Chainlink VRF's Subscription method to generate random numbers, which ensures fairness and verifiability in randomness.
- Mechanisms for game expiration and the withdrawal of total value locked (TVL) are implemented to address potential edge cases.
- Robust access control measures are established for functions designated for admin use.
The TreasureHunt contract has been deployed on the Sepolia testnet. You can interact with or verify the contract at the following address:
Contract Address: 0x2376df1c0C7Ea045905FFA9a013a51FA6aE6E169
Etherscan Link: https://sepolia.etherscan.io/address/0x2376df1c0C7Ea045905FFA9a013a51FA6aE6E169#code