AVAX Lotto Learning Project
A decentralized lottery application built on Avalanche (AVAX) for educational purposes.
Overview
This project demonstrates a full-stack dApp workflow:
- Smart Contract:
Lotto.solhandles ticket purchases, pooling funds, and picking a pseudo-random winner. - Frontend: A React + Vite + Tailwind CSS application that connects to the contract using
viem. - Tooling: Hardhat (v3 Beta) for development, testing, and deployment.
Prerequisites
- Node.js (v20+)
- MetaMask (or similar wallet) installed in your browser.
Getting Started
1. Install Dependencies
npm install
cd frontend
npm install
cd ..2. Run Local Blockchain
Start a local Hardhat node to simulate the Avalanche network:
npx hardhat node
# OR if using Nix shell:
lotto-nodeKeep this terminal running.
3. Deploy Contract
In a new terminal, deploy the contract to your local node:
npx hardhat ignition deploy ignition/modules/Lotto.ts --network localhost
# OR if using Nix shell:
lotto-deployImportant: Copy the deployed contract address from the output.
4. Configure Frontend
- Open
frontend/src/App.tsx. - Update
CONTRACT_ADDRESSwith the address you just copied. - (Optional) Import the provided private keys from
npx hardhat nodeoutput into MetaMask to have funds for testing. Switch MetaMask network toLocalhost 8545.
5. Run Frontend
cd frontend
npm run dev
# OR if using Nix shell:
lotto-uiOpen the provided URL (usually http://localhost:5173) in your browser.
6. Wallet Configuration & Funds (Crucial!)
To interact with the dApp, your wallet (Core or MetaMask) must be connected to your local Hardhat node.
Add Network:
- Network Name: Localhost 8545
- RPC URL:
http://127.0.0.1:8545 - Chain ID:
31337 - Currency Symbol:
AVAX(or ETH)
Get Test Funds:
- Look at the terminal running
lotto-node(ornpx hardhat node). - It lists 20 accounts with 10,000 ETH/AVAX each.
- Copy a Private Key from that list (e.g.,
0xac09...). - In your wallet, select "Import Account" (or "Add Account" -> "Import Private Key") and paste the key.
- Note: Never use a real mainnet private key for development!
- Look at the terminal running
Project Structure
contracts/: Solidity smart contracts.frontend/: React application.test/: Contract tests usingviemandnode:test.ignition/: Deployment modules.docs/: detailed learning guides.
Features
- Enter Lottery: Pay 1 AVAX (testnet/local) to enter.
- Pick Winner: Owner can pick a winner when the duration ends.
- Yield (Concept): The contract is structured to allow future integration with yield protocols.
Deployment to Production (Avalanche Fuji Testnet / Mainnet)
1. Smart Contract Deployment
To deploy to a live network like the Avalanche Fuji Testnet:
Set Environment Variables: Use Hardhat's configuration variables to securely store your private key and RPC URL.
bashnpx hardhat vars set FUJI_PRIVATE_KEY # Enter your private key (must have AVAX for gas)Configure Network: Update
hardhat.config.tsto include the network:typescriptimport { vars } from "hardhat/config"; const FUJI_PRIVATE_KEY = vars.get("FUJI_PRIVATE_KEY"); // ... inside config object networks: { fuji: { url: "https://api.avax-test.network/ext/bc/C/rpc", accounts: [FUJI_PRIVATE_KEY], }, },Deploy:
bashnpx hardhat ignition deploy ignition/modules/Lotto.ts --network fuji
2. Frontend Deployment
Update Contract Address: Update
frontend/src/App.tsxwith the new address from the Fuji deployment.Update Chain Config: In
App.tsx, change thechainfromhardhattoavalancheFuji:typescriptimport { avalancheFuji } from 'viem/chains'; // ... use avalancheFuji in createPublicClient and createWalletClientBuild:
bashcd frontend npm run buildThis creates a
dist/folder containing static files ready to be hosted on Vercel, Netlify, or GitHub Pages.
License
MIT