pragma solidity ^0.8.24;
import {Storage, Schema} from "bundle/voting-system/storages/Storage.sol";
contract VoteV2 {
function vote(uint256 proposalId) public {
Schema.Proposal storage $proposal = Storage.Proposals().getProposal(proposalId);
if (!$proposal.exists()) revert VotingSystemErrors.ProposalNotFound();
if ($proposal.hasVoted(msg.sender)) revert VotingSystemErrors.AlreadyVoted();
$proposal.setVote(msg.sender, true);
emit VoteCast(proposalId, msg.sender);
}
}
import {MCTest} from "@mc-devkit/Flattened.sol";
import {VotingSystemErrors} from "bundle/voting-system/interfaces/VotingSystemErrors.sol";
import {VotingSystemEvents} from "bundle/voting-system/interfaces/VotingSystemEvents.sol";
contract VoteV2Test is MCTest {
function setUp() public {
address _voteV2 = address(new VoteV2());
_use(Vote.vote.selector, _voteV2);
}
function test_vote_success() public {
uint256 proposalId = 1;
Storage.Proposals().createProposal(proposalId, Schema.Proposal({exists: true, ...}));
vm.expectEmit();
emit VoteCast(proposalId, address(this));
Vote(target).vote(proposalId);
assertTrue(Storage.Proposals().getProposal(proposalId).hasVoted(address(this)));
}
function test_vote_alreadyVoted() public {
uint256 proposalId = 1;
Storage.Proposals().createProposal(proposalId, Schema.Proposal({exists: true, ...}));
Storage.Proposals().setVote(proposalId, address(this), true);
vm.expectRevert(VotingSystemErrors.AlreadyVoted.selector);
Vote(target).vote(proposalId);
}
}