A decentralized voting system built with Solidity and Foundry, allowing users to vote on candidates within a specified time period.
- Time-Bound Voting: Voting is only allowed within a defined start and end time
- Single Vote Per Address: Each address can vote only once to prevent double voting
- Candidate Management: Add multiple candidates before voting begins
- Vote Counting: Real-time vote tallying for each candidate
- Secure: Built with Solidity best practices and comprehensive testing
The Voting contract includes the following functionality:
constructor(uint256 _start, uint256 _end): Initialize voting with start and end timestampsaddCandidate(string memory _name): Add a candidate to the electionvote(uint256 _index): Cast a vote for a candidate by indexcandidates(uint256): View candidate information (name and vote count)hasVoted(address): Check if an address has already voted
- Foundry installed
- Solidity ^0.8.20
- Clone the repository:
git clone <repository-url>
cd solidity-voting-contract- Install dependencies:
forge installforge buildforge testRun specific test:
forge test --match-test testVoteOnce- Set up your environment variables:
export PRIVATE_KEY=your_private_key
export RPC_URL=your_rpc_url- Deploy using the script:
forge script script/Deploy.sol --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcastUse Cast to interact with the deployed contract:
# Add a candidate
cast send <contract_address> "addCandidate(string)" "Candidate Name" --rpc-url $RPC_URL --private-key $PRIVATE_KEY
# Vote for candidate (index 0)
cast send <contract_address> "vote(uint256)" 0 --rpc-url $RPC_URL --private-key $PRIVATE_KEY
# Check vote count
cast call <contract_address> "candidates(uint256)" 0 --rpc-url $RPC_URLThe project includes comprehensive tests covering:
- Single vote enforcement
- Vote count incrementing
- Voting period validation
- Double voting prevention
Run tests with verbose output:
forge test -v├── src/
│ └── Voting.sol # Main voting contract
├── test/
│ └── Voting.t.sol # Contract tests
├── script/
│ └── Deploy.sol # Deployment script
├── foundry.toml # Foundry configuration
└── README.md # This file
- Voting is time-bound to prevent indefinite voting periods
- One vote per address prevents manipulation
- Input validation for candidate indices
- Uses Solidity ^0.8.20 for enhanced security features
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.