DEV Community

Michael Kim
Michael Kim

Posted on

Decentralized Lottery

Dettery - decentralized lottery dApp (Ethereum testnet Sepolia).


I just launched dettery and testing the app.

Here is the markdown…(https://github.com/advexon/Dettery)

DETTERY - Decentralized Lottery Platform
License: MIT Solidity Next.js TypeScript Ethereum

A provably fair, transparent, and decentralized lottery system built on Ethereum Sepolia testnet with automatic payouts and secure randomness.
🌟 Features

πŸ”’ Security & Transparency

βœ… Smart Contract Verified - All contracts are verified and auditable
βœ… Block Hash Randomness - Free, secure randomness using blockchain data
βœ… No Central Authority - Fully decentralized operation
βœ… Automatic Payouts - Winner gets 80%, admin gets 20%
βœ… Public Blockchain - All transactions are transparent and verifiable
🎯 User Experience

βœ… Modern UI/UX - Beautiful, responsive design with Tailwind CSS
βœ… Real-time Updates - No page refresh needed for lottery updates
βœ… Participant Visibility - See all participants and their entries
βœ… Multiple Entry Support - Users can enter multiple times
βœ… Wallet Integration - MetaMask and Web3 wallet support
πŸ› οΈ Technical Features

βœ… TypeScript - Full type safety across the application
βœ… Wagmi & Viem - Modern Ethereum development tools
βœ… Hardhat - Professional smart contract development
βœ… Sepolia Testnet - Deployed and tested on Ethereum testnet
βœ… Auto-refresh - Real-time data updates after transactions
πŸš€ Quick Start

Prerequisites

Node.js 18+
npm or yarn
MetaMask or Web3 wallet
Sepolia ETH (get from faucets)
Installation

Clone the repository

git clone https://github.com/yourusername/dettery.git
cd dettery
Install dependencies

Install frontend dependencies

npm install

Install smart contract dependencies

cd contracts
npm install
cd ..
Environment Setup

Copy environment template

cp contracts/.env.example contracts/.env

Edit contracts/.env with your configuration

SEPOLIA_RPC_URL=https://ethereum-sepolia.publicnode.com
PRIVATE_KEY=your_private_key_here
ETHERSCAN_API_KEY=your_etherscan_api_key
Deploy Smart Contracts

cd contracts
npx hardhat compile
npx hardhat run scripts/deploy.ts --network sepolia
Update Frontend Configuration

// Update src/lib/config.ts with your deployed contract address
export const FACTORY_ADDRESS = 'your_deployed_contract_address';
Start the Application

npm run dev
Open in Browser

http://localhost:3000
πŸ“– How It Works

  1. Create Lottery Pool

Set ticket price and maximum players
Deploy a new lottery contract
Lottery becomes available for participation

  1. Participate

Connect your Web3 wallet
Pay the ticket price to enter
Multiple entries allowed per user

  1. Winner Selection

When lottery reaches maximum players
System waits 2 blocks for fair randomness
Block hash randomness selects winner automatically

  1. Payouts

Winner receives 80% of total pool
Admin receives 20% fee
All transactions are automatic and transparent
πŸ—οΈ Architecture

Smart Contracts

contracts/
β”œβ”€β”€ Lottery.sol # Individual lottery logic
β”œβ”€β”€ LotteryFactory.sol # Factory for creating lotteries
└── scripts/
└── deploy.ts # Deployment script
Frontend

src/
β”œβ”€β”€ app/ # Next.js App Router
β”œβ”€β”€ components/ # React components
β”‚ β”œβ”€β”€ ConnectWallet.tsx
β”‚ β”œβ”€β”€ CreateLottery.tsx
β”‚ β”œβ”€β”€ LotteryCard.tsx
β”‚ └── LotteryList.tsx
└── lib/
└── config.ts # Contract addresses and ABIs
πŸ”§ Configuration

Environment Variables

Create contracts/.env:

SEPOLIA_RPC_URL=https://ethereum-sepolia.publicnode.com
PRIVATE_KEY=your_private_key_here
ETHERSCAN_API_KEY=your_etherscan_api_key
Contract Addresses

Update src/lib/config.ts:

export const FACTORY_ADDRESS = '0xDE22C7fF7Ac8C7645AfB673a4Ea7087705FE94Ce';
πŸ§ͺ Testing

Smart Contract Tests

cd contracts
npx hardhat test
Frontend Testing

npm run test
Manual Testing

Connect MetaMask to Sepolia testnet
Get Sepolia ETH from faucets
Create a lottery pool
Participate with multiple wallets
Test winner selection and payouts
πŸ“Š Smart Contract Details

Lottery Contract

contract Lottery {
// Core state variables
uint256 public immutable i_ticketPrice;
uint256 public immutable i_maxPlayers;
address payable public immutable i_admin;

// Lottery state
enum LotteryState { OPEN, CALCULATING_WINNER, CLOSED }
LotteryState private s_lotteryState;

// Participants and winner
address payable[] public s_players;
address public s_winner;

// Randomness
uint256 public s_commitBlock;
uint256 public s_revealBlock;
Enter fullscreen mode Exit fullscreen mode

}
Key Functions

enter() - Join the lottery by paying ticket price
pickWinner() - Select winner using block hash randomness
getPlayers() - Get list of all participants
getLotteryState() - Get current lottery state
πŸ”’ Security Features

Randomness

Uses blockhash(), block.timestamp, and block.prevrandao
Commits to randomness block, reveals after 2 blocks
Prevents manipulation and ensures fairness
Access Control

Only lottery participants can trigger winner selection
Admin cannot manipulate results
All functions are public and auditable
Transparency

All lottery data is public
Contract source code is verified
All transactions are on-chain
🌐 Deployment

Sepolia Testnet (Current)

Factory Contract: 0xDE22C7fF7Ac8C7645AfB673a4Ea7087705FE94Ce
Network: Ethereum Sepolia
RPC: https://ethereum-sepolia.publicnode.com
Mainnet Deployment

Update environment variables
Deploy to mainnet
Verify contracts on Etherscan
Update frontend configuration
🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Setup

Fork the repository
Create a feature branch
Make your changes
Add tests if applicable
Submit a pull request
πŸ“ License

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

πŸ†˜ Support

Issues: GitHub Issues
Discussions: GitHub Discussions
Documentation: Wiki
πŸ™ Acknowledgments

Hardhat - Smart contract development framework
Next.js - React framework
Wagmi - React hooks for Ethereum
Viem - TypeScript interface for Ethereum
Tailwind CSS - CSS framework
πŸ“ˆ Roadmap

Mainnet deployment
Mobile app (React Native)
Additional randomness providers
Lottery categories and themes
Governance token
Multi-chain support

Top comments (0)