//SPDX-License-Identifier: MIT pragma solidity 0.7.4; import "./Forum.sol"; contract Voting { Forum public forum; constructor(Forum addr) { forum = Forum(addr); } struct Poll { uint topicID; uint numOptions; string dataHash; mapping (address => uint) votes; uint[] voteCounts; // First element will hold total count uint timestamp; // Timestamp of creation } mapping (uint => Poll) polls; event PollCreated(uint topicID); event UserVoted(address userAddress); function createPoll(uint topicID, uint numOptions, string memory dataHash) public returns (uint) { require(forum.hasUserSignedUp(msg.sender)); // Only registered users can create polls require(topicID 0 && option <= poll.numOptions); // Verify that this option exists address voter = msg.sender; uint currentVote = poll.votes[voter]; if(currentVote == option) return; if(currentVote == 0) // Voter hadn't voted before poll.voteCounts[0]++; else poll.voteCounts[currentVote]--; poll.voteCounts[option]++; poll.votes[voter] = option; emit UserVoted(voter); } }