A simple election smart contract built with Solidity and tested using Foundry. This project demonstrates a basic voting flow where eligible voters can cast a single vote for one of three predefined candidates while the election is open.
- Contract:
src/SuraChain.sol - Test suite:
test/SuraChain.t.solandtest/ElectionNotOpen.t.sol - Build system: Foundry
- Election can be opened with a 24-hour deadline
- One vote per address
- Three hard-coded candidates
- Custom errors for invalid operations
- Events emitted for vote casting and election start
SuraChain exposes the following public state and actions:
NAME: Constant name of the contracttotalVotes: Total number of votes casthasVoted: Tracks whether an address has already votedcandidates: Maps candidate IDs to candidate detailselectionOpened: Flag to indicate if voting is openelectionDeadline: Timestamp when voting closes
openElection(): Opens the election and sets the deadline to 24 hours from the current block timestampvote(uint8 candidateId): Allows a voter to cast a vote for candidate IDs1,2, or3
- Voting is allowed only when the election is open
- Voting after the deadline closes the election and reverts
- Each address may vote only once
- Candidate IDs outside
1–3are rejected
- Foundry installed: https://book.getfoundry.sh/
- Linux-compatible environment
forge buildforge testThe test suite covers:
- initial state verification
- successful voting flow
- prevention of double voting
- invalid candidate handling
- voting while election is not open
- event emission checks
forge fmtsrc/SuraChain.sol— main contract implementationtest/SuraChain.t.sol— core voting behavior teststest/ElectionNotOpen.t.sol— election state validation testsfoundry.toml— Foundry project configuration
The contract uses fixed candidate addresses and a simple 1-day election window for demonstration purposes. In a production environment, candidate registration, role-based access control, and configurable election parameters should be added.