// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Voting {
struct Proposal {
address target;
bytes data;
uint yesCount;
uint noCount;
mapping(address => bool) hasVoted; // Keep track of whether an address has voted
}
Proposal[] public proposals;
function newProposal(address _target, bytes calldata _data) external {
Proposal memory newProposalInstance = Proposal({
target: _target,
data: _data,
yesCount: 0,
noCount: 0
});
proposals.push(newProposalInstance);
}
function castVote(uint proposalId, bool supportsProposal) external {
require(proposalId < proposals.length, "Invalid proposalId");
Proposal storage proposal = proposals[proposalId];
// Check if the voter has already voted
require(!proposal.hasVoted[msg.sender], "Address has already voted");
// If the voter previously voted yes, decrement yesCount; if voted no, decrement noCount
if (proposal.hasVoted[msg.sender]) {
if (proposal.hasVoted[msg.sender] == true) {
proposal.yesCount--;
} else {
proposal.noCount--;
}
}
// Update the vote and mark the voter as having voted
if (supportsProposal) {
proposal.yesCount++;
} else {
proposal.noCount++;
}
proposal.hasVoted[msg.sender] = supportsProposal;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Voting {
struct Proposal {
address target;
bytes data;
uint yesCount;
uint noCount;
mapping(address => bool) hasVoted; // Keep track of whether an address has voted
}
Proposal[] public proposals;
function newProposal(address _target, bytes calldata _data) external {
Proposal memory newProposalInstance = Proposal({
target: _target,
data: _data,
yesCount: 0,
noCount: 0
});
proposals.push(newProposalInstance);
}
function castVote(uint proposalId, bool supportsProposal) external {
require(proposalId < proposals.length, "Invalid proposalId");
Proposal storage proposal = proposals[proposalId];
// Check if the voter has already voted
require(!proposal.hasVoted[msg.sender], "Address has already voted");
// If the voter previously voted yes, decrement yesCount; if voted no, decrement noCount
if (proposal.hasVoted[msg.sender]) {
if (proposal.hasVoted[msg.sender] == true) {
proposal.yesCount--;
} else {
proposal.noCount--;
}
}
// Update the vote and mark the voter as having voted
if (supportsProposal) {
proposal.yesCount++;
} else {
proposal.noCount++;
}
proposal.hasVoted[msg.sender] = supportsProposal;
}
}
Please help, i can solve the above error.Please help figure out how to solve the error shared above.