Skip to content

Commit

Permalink
Create voting_contract.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Nov 26, 2024
1 parent 03dc803 commit 4f5f40f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions QuantumNexusProtocol/src/smart_contracts/voting_contract.sol
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);
}
}

0 comments on commit 4f5f40f

Please sign in to comment.