# Blockchain-Based-Voting-System
A secure, transparent voting web application that integrates a React frontend, Node/Express backend, and Ethereum smart contracts. This README explains how to set up, run, test, and deploy the project.
Repository links
- Root package: package.json (see
scriptssuch asscripts.start,scripts.dev,scripts.install-all) - Server entrypoint: server/index.js
- Server helpers / docs: server/ganache-setup.md
- Smart contract config: contracts/truffle-config.js
- Deployment guide: DEPLOYMENT_GUIDE.md
- Compiled contract artifact: server/VotingSystem.json
- Client app: client (React app)
Contents
- Overview
- Prerequisites
- Setup (install & env)
- Local development (server + client)
- Smart contracts (Ganache, Truffle, deployment)
- MetaMask configuration
- Building & Production
- Deployment options & links
- Troubleshooting
Overview
- Frontend: React + Chakra UI (client)
- Backend: Node.js + Express + SQLite (or production PostgreSQL) (server/index.js)
- Smart contracts: Truffle-based contracts in contracts
- Authentication: JWT, bcrypt
- OTP email verification via Gmail (nodemailer)
Prerequisites
- Node.js v14+ (recommend v16 or v18)
- npm (or yarn)
- Truffle (for contract deployment):
npm install -g truffle - Ganache CLI or Ganache GUI (for local blockchain): see server/ganache-setup.md
- MetaMask browser extension
Quick setup (local)
-
Clone the repo and open it: git clone cd block-vote
-
Install dependencies
- Install root dependencies and client/server: npm run install-all
- Or manually: npm install cd client && npm install cd ../server && npm install
-
Environment variables
- Create a
.envinserver/(see below). Example: PORT=5000 NODE_ENV=development EMAIL_USER=your-gmail@gmail.com EMAIL_PASS=your-app-specific-password JWT_SECRET=your-secret-key-min-32-chars ETHEREUM_NODE_URL=http://127.0.0.1:7545 CONTRACT_ADDRESS=<deployed_contract_address> ADMIN_ETHEREUM_ADDRESS=<ganache_admin_address> CHAIN_ID=1337 - Frontend environment (for local dev you can use
client/.env): REACT_APP_API_URL=http://localhost:5000 REACT_APP_NETWORK_ID=1337 REACT_APP_NETWORK_NAME=Local Ganache REACT_APP_RPC_URL=http://127.0.0.1:7545
Files referenced in this repo:
- server main: server/index.js
- contract config: contracts/truffle-config.js
- ganache guide: server/ganache-setup.md
- full deployment instructions: DEPLOYMENT_GUIDE.md
- Create a
Local development (run app)
-
Start Ganache (see below) or ensure
ETHEREUM_NODE_URLpoints to your node. -
Start both server and client concurrently (recommended): npm run dev
- This runs the server (nodemon) and client (React dev server). See
scripts.dev.
- This runs the server (nodemon) and client (React dev server). See
-
Alternatively run individually:
cd server npm start
cd client npm start
-
Open browser:
- Frontend: http://localhost:3000
- API: http://localhost:5000 (default; matches
PORTin.env)
Smart contracts (Ganache & Truffle)
-
Quick Ganache setup:
- CLI: ganache-cli -p 7545 -i 1337 --accounts 10 --defaultBalanceEther 100
- Or use Ganache GUI and set RPC server to
http://127.0.0.1:7545, Network ID1337. - See full instructions and verification commands in server/ganache-setup.md.
-
Deploy contracts locally: cd contracts truffle migrate --reset --network development
- The Truffle network config lives in contracts/truffle-config.js.
- After deployment, copy the deployed contract address into
server/.envasCONTRACT_ADDRESS. Also updateADMIN_ETHEREUM_ADDRESS.
-
Artifacts:
- Compiled contract JSON for server usage: server/VotingSystem.json
MetaMask setup
- Install MetaMask browser extension.
- Create or import an account.
- Add a custom RPC network for Ganache:
- RPC URL: http://127.0.0.1:7545
- Chain ID: 1337
- Network name: Ganache Local
- Import an account private key from Ganache (the private key printed by Ganache) into MetaMask for testing.
- Ensure the wallet address matches
ADMIN_ETHEREUM_ADDRESSinserver/.envwhere needed.
Building for production
-
Build frontend: cd client npm run build
- The built files will be in
client/build/.
- The built files will be in
-
Serve static frontend from server:
- The repo's DEPLOYMENT_GUIDE.md includes an example to update server/index.js to serve
client/buildwhenNODE_ENV === 'production'.
- The repo's DEPLOYMENT_GUIDE.md includes an example to update server/index.js to serve
-
Run server (production): NODE_ENV=production PORT=8080 node server/index.js
- Or use process managers (pm2) or containerize (see below).
Deployment options
- Short list (full steps in DEPLOYMENT_GUIDE.md):
- Frontend: Vercel or Netlify (deploy
client/) - Backend: Railway, Render, or Heroku (set root to
server/for Railway) - All-in-one: Railway using a monorepo Dockerfile (see DEPLOYMENT_GUIDE.md)
- Database: SQLite is included. For production use PostgreSQL — the guide gives sample
server/config/database.jsadjustment.
- Frontend: Vercel or Netlify (deploy
Sample .env (server/.env.production) PORT=8080 NODE_ENV=production DATABASE_URL=postgres://username:password@host:5432/database EMAIL_USER=your-gmail@gmail.com EMAIL_PASS=your-app-specific-password JWT_SECRET=your-super-secret-jwt-key-min-32-chars CORS_ORIGIN=https://your-frontend-url.vercel.app
Useful scripts (from package.json)
- npm start — run server (
node server/index.js) - npm run client — start frontend dev server
- npm run server — start backend with nodemon
- npm run dev — run client & server concurrently
- npm run install-all — install root, client, and server deps
- npm run build — builds frontend:
cd client && npm run build
Troubleshooting & tips
- CORS errors: confirm
CORS_ORIGINin server env. - DB connection: ensure
DATABASE_URLis valid for PostgreSQL; SQLite file included for local testing. - MetaMask network mismatch: set chain id / RPC as in Ganache or testnet RPC.
- Email (OTP) not sending: verify Gmail app password and
EMAIL_USER/EMAIL_PASS. - Contract issues: re-run
truffle migrate --resetand update contract address inserver/.env.
Where to read more in this repo
- Project deployment walkthrough: DEPLOYMENT_GUIDE.md
- Ganache local blockchain: server/ganache-setup.md
- Contract network settings: contracts/truffle-config.js
- Server startup: server/index.js Snapshots