-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtoken.sol
61 lines (51 loc) · 2.05 KB
/
token.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Token.sol new token solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting {
// Chairperson of the voting
address public chairperson;
// A struct to hold voter information
struct Voter {
bool hasVoted; // True if the person has already voted
uint vote; // Index of the voted proposal
uint weight; // Weight of the vote
}
// A struct to hold proposal information
struct Proposal {
string name; // Name of the proposal
uint voteCount; // Number of accumulated votes
}
// Mapping to store voter details
mapping(address => Voter) public voters;
// Array to store proposals
Proposal[] public proposals;
// Function to give the right to vote to a voter
function giveRightToVote(address voter) public {
require(msg.sender == chairperson, "Only the chairperson can give the right to vote.");
require(!voters[voter].hasVoted, "The voter has already voted.");
require(voters[voter].weight == 0, "The voter already has the right to vote.");
voters[voter].weight = 1;
}
// Function to vote for a proposal
function vote(uint proposal) public {
Voter storage sender = voters[msg.sender];
require(sender.weight > 0, "You have no right to vote.");
require(!sender.hasVoted, "You have already voted.");
sender.hasVoted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.weight;
}// Function to get the winning proposal
function winningProposal() public view returns (uint winningProposalIndex) {
uint winningVoteCount = 0;
for (uint i = 0; i < proposals.length; i++) {
if (proposals[i].voteCount > winningVoteCount) {
winningVoteCount = proposals[i].voteCount;
winningProposalIndex = i;
}
}
}
// Function to get the winner's name
function winnerName() public view returns (string memory winnerName_) {
winnerName_ = proposals[winningProposal()].name;
}
}