A decentralized lottery application built with Next.js, Solidity, and Web3. Users can participate in a blockchain-based lottery by entering with 0.1 ETH, and the contract owner can pick a random winner who receives the entire pot.
- Decentralized Lottery: Built on Ethereum blockchain using smart contracts
- Web3 Integration: Connect with MetaMask wallet
- Real-time Updates: Live lottery pot and player count updates
- Winner History: View previous lottery winners
- Responsive Design: Modern UI with CSS modules
- Gas Optimization: Efficient smart contract design
- Framework: Next.js 12.2.5 with React 18
- Web3: Web3.js for blockchain interaction
- Styling: CSS Modules for component-specific styles
- State Management: React Context API for global state
- Language: Solidity ^0.8.15
- Framework: Truffle for development and deployment
- Networks: Goerli and Sepolia testnets
- Provider: Infura for blockchain connectivity
CryptoLottery/
โโโ backend/ # Smart contract development
โ โโโ contracts/
โ โ โโโ Lottery.sol # Main lottery contract
โ โโโ migrations/
โ โ โโโ 1_deploy.js # Deployment script
โ โโโ build/
โ โ โโโ contracts/
โ โ โโโ Lottery.json # Compiled contract ABI
โ โโโ truffle-config.js # Truffle configuration
โโโ components/ # React components
โ โโโ ConnectWalletBtn.js # Wallet connection button
โ โโโ Header.js # App header
โ โโโ LotteryCard.js # Lottery information card
โ โโโ Table.js # Players table
โ โโโ TableRow.js # Individual table row
โ โโโ UserCard.js # User information card
โโโ context/
โ โโโ context.js # React context for state management
โโโ pages/
โ โโโ _app.js # Next.js app wrapper
โ โโโ index.js # Main lottery page
โ โโโ api/
โ โโโ hello.js # API route
โโโ styles/ # CSS modules
โ โโโ globals.css # Global styles
โ โโโ Header.module.css # Header styles
โ โโโ Home.module.css # Home page styles
โ โโโ PotCard.module.css # Pot card styles
โ โโโ Table.module.css # Table styles
โ โโโ TableRow.module.css # Table row styles
โโโ utils/
โ โโโ constants.js # Contract constants
โ โโโ Lottery.json # Contract ABI
โ โโโ lotteryContract.js # Contract interaction utilities
โโโ package.json # Dependencies and scripts
- Node.js (v14 or higher)
- npm or yarn
- MetaMask browser extension
- Truffle (for smart contract development)
- Goerli or Sepolia testnet ETH
-
Clone the repository
git clone <repository-url> cd CryptoLottery
-
Install dependencies
npm install # or yarn install -
Set up environment variables Create a
.envfile in the backend directory with your wallet mnemonic:MNEMONIC=your_wallet_mnemonic_phrase_here INFURA_PROJECT_ID=your_infura_project_id -
Deploy smart contracts
cd backend truffle compile truffle migrate --network goerli # or sepolia
-
Update contract address After deployment, update the contract address in
utils/constants.jswith the deployed contract address. -
Start the development server
npm run dev # or yarn dev -
Open the application Navigate to http://localhost:3000 in your browser.
- Connect Wallet: Click "Connect Wallet" and approve the connection in MetaMask
- Enter Lottery: Click "Enter Lottery" and confirm the 0.1 ETH transaction
- View Status: See your entry in the players table and current lottery pot
- Wait for Winner: The contract owner will pick a winner when ready
- Connect as Owner: Use the owner wallet to connect
- Pick Winner: Click "Pick Winner" to randomly select and pay the winner
- View History: See all previous winners in the winners list
enter(): Enter the lottery with 0.1 ETH minimumgetPlayers(): Get list of current playersgetBalance(): Get current lottery potgetLotteryId(): Get current lottery round IDpickWinner(): Pick random winner (owner only)getWinners(): Get list of all winners
- Access Control: Only contract owner can pick winners
- Minimum Entry: 0.1 ETH minimum to prevent spam
- Random Selection: Uses block timestamp and owner address for randomness
- Automatic Reset: Players array resets after each winner selection
npm run dev # Start development server
npm run build # Build for production
npm run start # Start production server
npm run lint # Run ESLintcd backend
truffle compile # Compile contracts
truffle test # Run tests
truffle migrate # Deploy to local network
truffle migrate --network goerli # Deploy to GoerliThe application is configured for:
- Goerli Testnet: Primary test network
- Sepolia Testnet: Alternative test network
- Infura: Blockchain node provider
next: React frameworkreact: UI libraryweb3: Ethereum JavaScript API@truffle/hdwallet-provider: Wallet provider for Truffletruncate-eth-address: Address formatting utility
truffle: Development framework@truffle/hdwallet-provider: HD wallet providersolc: Solidity compiler
- Testnet Only: This is for testing purposes only
- Private Key Security: Never commit private keys or mnemonics
- Gas Limits: Set appropriate gas limits for transactions
- Randomness: Current randomness is not cryptographically secure
- Access Control: Only deploy with trusted owner addresses
- Build the application:
npm run build - Deploy to Vercel, Netlify, or your preferred platform
- Update contract address for production network
- Update
truffle-config.jswith production network settings - Deploy to mainnet:
truffle migrate --network mainnet - Verify contract on Etherscan
- Update frontend with production contract address
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit changes:
git commit -m 'Add feature' - Push to branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This is a demonstration project for educational purposes. The lottery system uses pseudo-random number generation and should not be used for real money without proper security audits and improvements.
For support and questions:
- Create an issue in the repository
- Check the documentation
- Review the smart contract code