Skip to content

0xDevNinja/Treasure-Hunt

Repository files navigation

TreasureHunt

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.

Game Highlights

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

Functions

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

Events

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

Setup and Deployment

  1. Ensure you have the Chainlink VRF contracts and dependencies set up.
  2. Set up your environment variables:
    • Rename .env.example to .env
    • Fill in the required values in the .env file
  3. Deploy the contract with the following parameters:
    • VRF CoordinatorId
    • SubscriptionId
    • KeyHash of sepolia network
    • Request confirmation blocks
    • Game duration
    • Participation Fee

Environment Variables

The project uses a .env file to manage sensitive and configuration information. Follow these steps to set up:

  1. Locate the .env.example file in the project root.
  2. Create a copy of this file and rename it to .env.
  3. Open the .env file 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 nodes
  • DEPLOYER_PVT_KEY: The private key of the account deploying the contract
  • ETHERSCAN_API_KEY: Your Etherscan API key for contract verification
  • VRF_COORDINATOR_ID: VRF CoordinatorId for sepolia test network
  • S_SUBSCRIPTION_ID: Subscription Id generated by chainlink
  • KEY_HASH: Key Hash for sepolia test network

Important:

  • Never commit your .env file to version control.
  • Keep your private keys and API keys secure.

Compile Deploy and Test

To run the test suite for TreasureHunt, follow these steps:

  1. Open a terminal and navigate to the project's root directory.

  2. Run the following command:

    npm test
    

    This command will execute all the tests in the test suite.

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

Security Considerations

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

Sepolia Testnet Deployment

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


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published