Skip to content

chdineshbabu/CryptoLottery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฐ CryptoLottery DApp

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.

๐ŸŒŸ Features

  • 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

๐Ÿ—๏ธ Architecture

Frontend (Next.js)

  • 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

Backend (Smart Contracts)

  • Language: Solidity ^0.8.15
  • Framework: Truffle for development and deployment
  • Networks: Goerli and Sepolia testnets
  • Provider: Infura for blockchain connectivity

๐Ÿ“ Project Structure

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

๐Ÿš€ Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn
  • MetaMask browser extension
  • Truffle (for smart contract development)
  • Goerli or Sepolia testnet ETH

Installation

  1. Clone the repository

    git clone <repository-url>
    cd CryptoLottery
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Set up environment variables Create a .env file in the backend directory with your wallet mnemonic:

    MNEMONIC=your_wallet_mnemonic_phrase_here
    INFURA_PROJECT_ID=your_infura_project_id
    
  4. Deploy smart contracts

    cd backend
    truffle compile
    truffle migrate --network goerli  # or sepolia
  5. Update contract address After deployment, update the contract address in utils/constants.js with the deployed contract address.

  6. Start the development server

    npm run dev
    # or
    yarn dev
  7. Open the application Navigate to http://localhost:3000 in your browser.

๐ŸŽฎ How to Use

For Players

  1. Connect Wallet: Click "Connect Wallet" and approve the connection in MetaMask
  2. Enter Lottery: Click "Enter Lottery" and confirm the 0.1 ETH transaction
  3. View Status: See your entry in the players table and current lottery pot
  4. Wait for Winner: The contract owner will pick a winner when ready

For Contract Owner

  1. Connect as Owner: Use the owner wallet to connect
  2. Pick Winner: Click "Pick Winner" to randomly select and pay the winner
  3. View History: See all previous winners in the winners list

๐Ÿ”ง Smart Contract Details

Lottery Contract Functions

  • enter(): Enter the lottery with 0.1 ETH minimum
  • getPlayers(): Get list of current players
  • getBalance(): Get current lottery pot
  • getLotteryId(): Get current lottery round ID
  • pickWinner(): Pick random winner (owner only)
  • getWinners(): Get list of all winners

Security Features

  • 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

๐Ÿ› ๏ธ Development

Available Scripts

npm run dev      # Start development server
npm run build    # Build for production
npm run start    # Start production server
npm run lint     # Run ESLint

Smart Contract Development

cd backend
truffle compile          # Compile contracts
truffle test            # Run tests
truffle migrate         # Deploy to local network
truffle migrate --network goerli  # Deploy to Goerli

๐ŸŒ Network Configuration

The application is configured for:

  • Goerli Testnet: Primary test network
  • Sepolia Testnet: Alternative test network
  • Infura: Blockchain node provider

๐Ÿ“ฆ Dependencies

Frontend

  • next: React framework
  • react: UI library
  • web3: Ethereum JavaScript API
  • @truffle/hdwallet-provider: Wallet provider for Truffle
  • truncate-eth-address: Address formatting utility

Backend

  • truffle: Development framework
  • @truffle/hdwallet-provider: HD wallet provider
  • solc: Solidity compiler

๐Ÿ”’ Security Considerations

  • 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

๐Ÿš€ Deployment

Frontend Deployment

  1. Build the application: npm run build
  2. Deploy to Vercel, Netlify, or your preferred platform
  3. Update contract address for production network

Smart Contract Deployment

  1. Update truffle-config.js with production network settings
  2. Deploy to mainnet: truffle migrate --network mainnet
  3. Verify contract on Etherscan
  4. Update frontend with production contract address

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Commit changes: git commit -m 'Add feature'
  4. Push to branch: git push origin feature-name
  5. Submit a pull request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

โš ๏ธ Disclaimer

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.

๐Ÿ†˜ Support

For support and questions:

  • Create an issue in the repository
  • Check the documentation
  • Review the smart contract code

๐Ÿ”— Links

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages