This project is a decentralized lottery smart contract built on Ethereum using Solidity and Hardhat. Players can enter the lottery by sending ETH, and a winner is randomly selected to receive the entire prize pool.
- Players enter by sending a fixed entry fee (e.g., 0.01 ETH)
- Random winner selection using block.prevrandao (PoS-compatible)
- Only the contract owner can trigger the lottery draw
- Fully tested using Hardhat & Chai
- Deployable on the Sepolia testnet
-
Clone the repository:
git clone https://github.com/DevFreAkeD/lottery-smart-contract.git cd lottery-smart-contract
-
Install dependencies:
npm install
-
Set up Hardhat:
npx hardhat
Select "Create a basic sample project" if prompted.
Create contracts/Lottery.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract Lottery {
address public owner;
address[] public players;
uint public entryFee;
constructor(uint _entryFee) {
owner = msg.sender;
entryFee = _entryFee;
}
function enter() public payable {
require(msg.value == entryFee, "Incorrect entry fee");
players.push(msg.sender);
}
function pickWinner() public onlyOwner {
require(players.length > 0, "No players joined");
uint winnerIndex = random() % players.length;
address winner = players[winnerIndex];
payable(winner).transfer(address(this).balance);
delete players;
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, players.length)));
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this");
_;
}
}
Run:
npx hardhat compile
You should see "Compilation finished successfully".
Create scripts/deploy.js
:
const hre = require("hardhat");
async function main() {
const entryFee = hre.ethers.parseEther("0.01"); // 0.01 ETH entry fee
const Lottery = await hre.ethers.getContractFactory("Lottery");
const lottery = await Lottery.deploy(entryFee);
await lottery.waitForDeployment();
console.log(`Lottery deployed to: ${lottery.target}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
npx hardhat run scripts/deploy.js --network hardhat
- Get Sepolia test ETH from:
- Set up
hardhat.config.js
:
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
networks: {
sepolia: {
url: "https://eth-sepolia.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY",
accounts: ["YOUR_PRIVATE_KEY"]
}
},
solidity: "0.8.19",
};
- Deploy:
npx hardhat run scripts/deploy.js --network sepolia
To ensure the contract works as expected, run:
npx hardhat test
Create test/Lottery.js
:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Lottery", function () {
let Lottery, lottery, owner, addr1, addr2;
beforeEach(async function () {
[owner, addr1, addr2] = await ethers.getSigners();
Lottery = await ethers.getContractFactory("Lottery");
lottery = await Lottery.deploy(ethers.parseEther("0.01"));
});
it("should allow players to enter", async function () {
await lottery.connect(addr1).enter({ value: ethers.parseEther("0.01") });
expect(await lottery.players(0)).to.equal(addr1.address);
});
it("should only allow the owner to pick a winner", async function () {
await expect(lottery.connect(addr1).pickWinner()).to.be.revertedWith("Only owner can call this");
});
});
💬 Let's discuss blockchain & Web3! Connect with me on LinkedIn or Twitter.