Appearance
Smart Contracts Deployment
This section details the process of deploying the smart contracts to a blockchain network. We use Foundry for contract deployment.
Prerequisites
Before deploying the contracts, ensure you have:
- Foundry installed (installation guide)
- Node.js and pnpm installed
- Access to a blockchain network (local Anvil, testnet, or mainnet)
- Sufficient funds in your deployer wallet for gas fees
Deployment Process
1. Local Development Deployment
For local development and testing:
bash
# Start a local Anvil node
anvil
# In another terminal, deploy contracts
forge script script/Deploy.s.sol --rpc-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast2. Testnet/Mainnet Deployment
For deploying to a testnet or mainnet:
bash
# Set your private key in environment variables
export PRIVATE_KEY=your_private_key_here
# Deploy to Sepolia testnet
forge script script/Deploy.s.sol --rpc-url https://sepolia.infura.io/v3/YOUR_INFURA_KEY --private-key $PRIVATE_KEY --broadcast --verify
# Deploy to Polygon mainnet
forge script script/Deploy.s.sol --rpc-url https://polygon-rpc.com --private-key $PRIVATE_KEY --broadcast --verifyDeployment Script
The deployment script (script/Deploy.s.sol) handles the deployment of all core contracts:
- VoterRegistry: Manages voter registration and verification
- CandidateRegistry: Manages candidate information
- PartyRegistry: Manages political parties
- ElectionRegistry: Manages elections
- VotingSystem: Central hub coordinating all registries
Post-Deployment Configuration
After deployment, you'll need to:
- Set contract addresses: Update environment variables in the backend
- Register administrators: Add admin addresses to registries
- Configure relayers: Set up backend relayer for gasless transactions
- Initialize ZK groups: Set up Semaphore groups for voter privacy
Environment Variables
Set these environment variables in your backend:
env
# Blockchain Configuration
BLOCKCHAIN_VOTING_SYSTEM_ADDRESS=0x...
BLOCKCHAIN_WALLET_PRIVATE_KEY=0x...Gasless Transaction Setup
To enable gasless transactions for voters:
- Deploy relayer wallet: Create a dedicated wallet for paying gas fees
- Fund relayer wallet: Ensure sufficient funds for transaction fees
- Configure backend: Set
BLOCKCHAIN_WALLET_PRIVATE_KEYto relayer wallet key - Enable ZK verification: Set up Semaphore for voter anonymity
ZK Integration Deployment
For ZK voter privacy using Semaphore:
- Deploy Semaphore contracts: Follow Semaphore deployment guide
- Create voter groups: Set up groups for each election
- Configure backend: Integrate Semaphore proof generation
- Test ZK proofs: Verify proof generation and verification works
Verification
After deployment, verify contract functionality:
bash
# Run tests against deployed contracts
forge test --fork-url http://localhost:8545
# Check contract addresses
cast call 0xVotingSystemAddress "voterRegistryAddress()" --rpc-url http://localhost:8545
cast call 0xVotingSystemAddress "candidateRegistryAddress()" --rpc-url http://localhost:8545
cast call 0xVotingSystemAddress "partyRegistryAddress()" --rpc-url http://localhost:8545
cast call 0xVotingSystemAddress "electionRegistryAddress()" --rpc-url http://localhost:8545Troubleshooting
Common deployment issues:
- Insufficient funds: Ensure deployer wallet has enough ETH/MATIC
- Network connectivity: Verify RPC endpoint is accessible
- Contract verification: Check Etherscan/Polygonscan API keys for verification
- Nonce issues: Reset nonce if transactions fail repeatedly
Security Considerations
- Private key management: Never commit private keys to version control
- Admin access: Limit admin addresses to trusted personnel only
- Contract upgrades: Plan for upgradeability if needed
- Auditing: Consider third-party security audits for production deployments