-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
QuantumNexusProtocol/src/smart_contracts/voting_contract.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract VotingContract { | ||
struct Candidate { | ||
string name; | ||
uint256 voteCount; | ||
} | ||
|
||
mapping(uint256 => Candidate) public candidates; | ||
mapping(address => bool) public hasVoted; | ||
uint256 public candidatesCount; | ||
|
||
event Voted(address indexed voter, uint256 indexed candidateId); | ||
|
||
function addCandidate(string memory name) external { | ||
candidatesCount++; | ||
candidates[candidatesCount] = Candidate(name, 0); | ||
} | ||
|
||
function vote(uint256 candidateId) external { | ||
require(!hasVoted[msg.sender], "You have already voted"); | ||
require(candidateId > 0 && candidateId <= candidatesCount, "Invalid candidate ID"); | ||
|
||
hasVoted[msg.sender] = true; | ||
candidates[candidateId].voteCount++; | ||
|
||
emit Voted(msg.sender, candidateId); | ||
} | ||
} |