Skip to content

emilamaj/VoodooNFT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NFT Test Project - Voodoo Blockchain Engineer Test

This project implements a simple game NFT project, where users can mint a random NFT. The minting process is split into 3 phases:

  • Commit phase: Users commit to mint a NFT by paying the price before knowing the NFT they will receive.
  • Reveal phase: Admin reveals a secret number that will be used to find out which NFT each users will receive. This phase starts when the reveal block is reached. (set beforehand by the admin). Once started, users cannot commit anymore.
  • Mint phase: Users can mint the NFT that they have committed to.

Fairness is ensured by the fact that the secret number generated by the admin cannot be changed after the contracts have been setup, as a hash of that number is published before any user can commit. (commit-reveal mechanism). Once the sale is over, the secret number revealed by the admin is checked against the on-chain hash.

A web page is provided to facilitate the interaction with the contracts for the users. Scripts are provided to ease the testing, deployment and verification of the contracts. Admins must perform the following steps to setup the game:

  • Deploy both contracts (GameNFT.sol and GameMint.sol)
  • Set the Mint contract (GameMint.sol) as an authorized minter on the GameNFT contract.
  • Setup the mintContract by providing the price, the reveal block and the secret number. Scripts are also provided for the admin to setup the game, and to reveal the secret number.

Dev environment setup

This project uses the following tools/frameworks:

  • Foundry Which is a modern, Rust-written Soldity smart contract development framework. It provides a CLI to compile, test, deploy and verify smart contracts. Everything from tests to deployment procedures are written in solidity, which eases a lot of the frictions.
  • ReactJS for the user interface

First, extract the project .zip into a new folder. You will have to run the following commands to setup the project:

forge init --force
forge install openzeppelin/openzeppelin-contracts

Contracts

How to run tests

To run the unit test procedures created for the smart contracts, run the following command:

forge test -vv

This command automatically runs all the test files *.t.sol in the tests folder.

How to deploy

The contracts are ready to be deployed on the Polygon Mumbai testnet. Before deployment, make sure to correctly setup the .env file at the root of the project. Here is an example .env file:

# Environment variables for the tools. These are throwaway keys.
MUMBAI_RPC_URL=https://polygon-mumbai.g.alchemy.com/v2/xxxxxx
PUBLIC_KEY=0xB048xxxxx # Not currently used, address derived from the PK
PRIVATE_KEY=0xc11dd9dbyyyyyyyy
ETHERSCAN_KEY_MUMBAI=TNFCQ3Bzzzzzzzzzzz

# NFT Deploy parameters
BASE_URI=http://localhost:3000/metadata/
NFT_ID_COUNT=4

[...] # Used later

To deploy the contracts, run the following command:

Testnet:

forge script script/Deploy.s.sol:DeployContract --rpc-url mumbai --broadcast -vv --verify

This command automatically verifies the contracts on polygonscan, if a correct API key is provided.

How to setup the game

Once the contracts are deployed, the admin must setup the project by providing the following parameters:

  • price: The price of a NFT in wei.
  • reveal block: The block height at which the reveal phase starts.
  • secret hash: Hash of the secret number that will be used to determine the NFTs that users will receive. A Forge script is provided to run the setup. To configure the script, open the .env file at the root of the project and set the following parameters:
NFT_PRICE_MATIC_HUNDREDTH=1
BLOCK_REVEAL_SECONDS=120
ADMIN_SECRET_UINT256=0xcafe

To execute the provided script, run the following command:

forge script script/Setup.s.sol:SetupMint --rpc-url mumbai --broadcast -vv

How to reveal the secret number

Once the reveal block is reached, the admin can reveal the secret number. The funds cannot be withdrawn before the secret number is revealed. The script Reaveal.s.sol allows the admin to perform this very easily. To reveal the secret number, run the following command:

forge script script/Reveal.s.sol:RevealSecret --rpc-url mumbai --broadcast -vv

UI Frontend

This project provides a simple User Interface to interact with the contracts. The front page displays general info about the NFT project, as well as the current state of the game.

The frontend was only tested on Chrome.

Once the contracts are deployed, you should update the frontend parameters in params.json to match the deployed contracts.

{
    "deployed": false ==> true,
    "nftContractAddress": "0xBadAddress" ==> "0xGoodAddress",
    "mintContractAddress": "0xBadAddress" ==> "0xGoodAddress",
    "rpcUrl": "http://localhost:8545" ==> "https://polygon-mumbai.g.alchemy.com/v2/..."
}

(Note: Deploy.s.sol should update this file automatically, right after the deployment of the scripts)

Depending on the state of the Game, users can interact with the contracts in different ways.

Users can freely explore the NFTs and their metadata. The metadata is stored at /metadata/<token_id>.json and references the image at /metadata/<token_id>.png Users can also view the NFTs owned by a given address.

Running the frontend

To run the React project, run the following commands inside the game-ui/ folder:

npm install
npm start

About

ERC-1155 NFT Example - Voodoo video game NFT

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published