A transparent, tamper-proof voting DApp using Solidity, Hardhat, and a simple HTML/JS frontend. This project demonstrates the core concepts of decentralized application development on the Ethereum blockchain.
- Smart contracts for voting logic
- Preventing double voting
- Event logging for transparency
- Owner-restricted functionalities
- Solidity: For writing the smart contract.
- Hardhat: For Ethereum development environment, testing, and deployment.
- Ethers.js: For interacting with the Ethereum blockchain from the frontend.
- HTML/JavaScript: For the frontend application.
- MetaMask: As the wallet to connect to the DApp.
.
├── contracts/
│ └── Voting.sol
├── scripts/
│ └── deploy.js
├── test/
│ └── voting-test.js
├── frontend/
│ ├── index.html
│ └── app.js
├── hardhat.config.js
└── package.json
Clone this repository and install the required dependencies.
git clone https://github.com/TusharB-07/voting.git
cd voting
npm installCompile the Voting.sol contract.
npx hardhat compileAfter compiling, a new artifacts directory will be created. You will need the ABI from artifacts/contracts/Voting.sol/Voting.json.
Run a local Hardhat blockchain node.
npx hardhat nodeThis will start a local Ethereum network and generate several accounts with test Ether. Keep this terminal window open.
In a new terminal window, deploy the contract to the local network.
npx hardhat run scripts/deploy.js --network localhostCopy the deployed contract address from the terminal output.
- Open
frontend/app.js. - Paste the deployed contract address into the
contractAddressvariable. - Copy the ABI from
artifacts/contracts/Voting.sol/Voting.jsonand paste it into thecontractABIvariable.
Serve the frontend directory using a simple web server.
npx http-server frontend- Open your browser and navigate to
http://127.0.0.1:8080. - Configure MetaMask:
- Add a new network with the following details:
- Network Name: Local Hardhat
- New RPC URL:
http://127.0.0.1:8545 - Chain ID:
31337
- Import an account into MetaMask using one of the private keys provided by the
npx hardhat nodecommand. The first account is the contract owner.
- Add a new network with the following details:
- Connect MetaMask to the DApp and start interacting with it.
- Run local chain:
npx hardhat node - Deploy contract:
npx hardhat run scripts/deploy.js --network localhost - Serve frontend:
npx http-server frontend - Admin actions:
addCandidate,registerVoter,setPhase - Voter actions:
vote(once, only in the Voting phase)
- Restrict admin actions via a more robust system like a DAO.
- Optimize gas usage.
- Add TheGraph for real-time off-chain data indexing.
- Implement commit-reveal or zk-SNARKs for private voting.
- Perform security audits before any mainnet deployment.
- Add token-weighted voting.
- Upgrade the frontend to a modern framework like React or Vue.