1

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.

0

1 Answer 1

1
// 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 storage newProposalInstance = proposals.push();

        newProposalInstance.target = _target;
        newProposalInstance.data = _data;
        newProposalInstance.yesCount = 0;
        newProposalInstance.noCount = 0;
    }

    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;
    }
}

Tweaking the syntax and the instantiation order slightly should fix that error in your case.

1
  • 1
    Notice that the solution creates a storage pointer for the nee proposal in newProposal(). This is important. You can't create a mapping in memory including inside a struct. Feb 10 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.