# Lottery DApp
A decentralized Lottery DApp built using Ethereum smart contracts and a web-based frontend. Users can participate in the lottery by sending Ether and have a chance to win the entire balance when the manager picks a winner.
- Allows users to enter the lottery by sending a specified amount of Ether.
- The manager can pick a winner randomly from the list of participants.
- The DApp displays the number of players and the list of participants.
- Solidity: For writing the smart contract.
- Ethers.js: For interacting with the Ethereum blockchain.
- MetaMask: For connecting to the user's Ethereum wallet.
- HTML/CSS/JavaScript: For the frontend interface.
- Hardhat: For Ethereum development and testing.
- MetaMask browser extension installed.
- Node.js and npm installed.
- An Ethereum development environment like Hardhat.
-
Clone the Repository
git clone https://github.com/your-username/lottery-dapp.git cd lottery-dapp
-
Install Dependencies Make sure you are in the project directory and install the necessary npm packages.
npm install
-
Set Up Hardhat If you haven't already set up a Hardhat environment, create a basic Hardhat configuration.
npx hardhat
Follow the prompts to create a new Hardhat project.
-
Create and Configure the Smart Contract
- Create a new file
contracts/Lottery.sol
with the following content:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract Lottery { address public manager; address payable[] public players; constructor() { manager = msg.sender; } function participate() public payable { require(msg.value >= 0.01 ether, "Minimum 0.01 ether required to participate"); players.push(payable(msg.sender)); } function getNumberOfPlayers() public view returns (uint256) { return players.length; } function pickWinner() public restricted { require(players.length > 0, "No players have participated"); uint256 winnerIndex = random() % players.length; address payable winner = players[winnerIndex]; winner.transfer(address(this).balance); players = new address payable ; // Reset players after winner is picked } function getPlayers() public view returns (address payable[] memory) { return players; } function getBalance() public view returns (uint256) { return address(this).balance; } modifier restricted() { require(msg.sender == manager, "Only the manager can call this function"); _; } function random() private view returns (uint256) { return uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length))); } }
- Create a new file
-
Compile the Contract
npx hardhat compile
-
Deploy the Contract Create a new deployment script
scripts/deploy.js
:async function main() { const Lottery = await ethers.getContractFactory("Lottery"); const lottery = await Lottery.deploy(); await lottery.deployed(); console.log("Lottery contract deployed to:", lottery.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
Then run the deployment script:
npx hardhat run scripts/deploy.js --network localhost
Note: Make sure you have a local Ethereum network running using:
npx hardhat node
-
Update the Contract Address
- In
index.html
, update thecontractAddress
variable with the deployed contract address from the deployment step.
- In
-
Install MetaMask
- Install MetaMask and connect it to the local Ethereum network (usually
http://localhost:8545
).
- Install MetaMask and connect it to the local Ethereum network (usually
-
Run the Frontend
- Open
index.html
in your web browser.
- Open
-
Enter the Lottery
- Click the "Enter Lottery" button. Make sure you have at least 0.01 Ether in your MetaMask account.
-
Get Players
- Click the "Get Players" button to view the list of participants.
-
Pick Winner
- Add a button for the "Pick Winner" functionality (available only to the manager). Update the frontend JavaScript to include the function to pick a winner.
- MetaMask Connection Issues: Ensure MetaMask is configured to connect to the correct network.
- Revert Errors: Make sure all contract conditions are met (e.g., minimum Ether for participation, caller is the manager).
- Clone the repository:
git clone https://github.com/your-username/lottery-dapp.git
- Install dependencies:
npm install
- Compile the contract:
npx hardhat compile
- Start a local Ethereum network:
npx hardhat node
- Deploy the contract:
npx hardhat run scripts/deploy.js --network localhost
MIT License
This updated `README.md` covers the complete process of setting up and running the Lottery DApp from scratch. It includes commands for each step, providing new users with a clear guide to start the project. Make sure to replace `"your-username"` in the GitHub link with your actual GitHub username if applicable.