I tried modify the castVote function to allow voters to change their vote on a particular proposal.I also tried to figure out a new way to track which addresses have already voted on which proposal. But i keep running this this error :
TypeError: Struct containing a (nested) mapping cannot be constructed.
--> src/Voting.sol:19:47:
|
19 | Proposal memory newProposalInstance = Proposal({
| ^ (Relevant source part starts here and spans across multiple lines).
// 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 figure out how to solve the error shared above.