Browse Source

Voting contract init

develop
Ezerous 4 years ago
parent
commit
e7b3ea37f6
  1. 7
      packages/concordia-contracts/contracts/Forum.sol
  2. 5
      packages/concordia-contracts/contracts/Migrations.sol
  3. 90
      packages/concordia-contracts/contracts/Voting.sol
  4. 3
      packages/concordia-contracts/migrations/2_deploy_contracts.js
  5. 2
      packages/concordia-contracts/truffle-config.js

7
packages/concordia-contracts/contracts/Forum.sol

@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.7.1;
pragma solidity 0.7.4;
contract Forum {
@ -145,6 +145,11 @@ contract Forum {
return topics[topicID].postIDs;
}
function getTopicAuthor(uint topicID) public view returns (address) {
require(topicID<numTopics); // Topic should exist
return topics[topicID].author;
}
function getPost(uint postID) public view returns (address, string memory, uint, uint) {
require(postID<numPosts);
return (

5
packages/concordia-contracts/contracts/Migrations.sol

@ -1,10 +1,11 @@
pragma solidity 0.7.1;
//SPDX-License-Identifier: MIT
pragma solidity 0.7.4;
contract Migrations {
address public owner;
uint public last_completed_migration;
constructor() public {
constructor() {
owner = msg.sender;
}

90
packages/concordia-contracts/contracts/Voting.sol

@ -0,0 +1,90 @@
//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) voters;
uint[] voteCounts; // First element will hold total count
uint timestamp;
}
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<forum.getNumberOfTopics()); // Only allow poll creation if topic exists
require (forum.getTopicAuthor(topicID) == msg.sender); // Only allow poll creation from the author of the topic
require(polls[topicID].timestamp == 0); // Only allow poll creation if it doesn't exist yet
Poll storage poll = polls[topicID];
poll.topicID = topicID;
poll.numOptions = numOptions;
poll.dataHash = dataHash;
poll.voteCounts = new uint[](numOptions+1);
poll.timestamp = block.timestamp;
emit PollCreated(topicID);
return topicID;
}
// Verify that poll exists
function isPollExistent(uint topicID) public view returns (bool) {
if (polls[topicID].timestamp != 0)
return true;
return false;
}
function hasVoted(uint topicID, address voter) public view returns (bool) {
require(isPollExistent(topicID));
if (polls[topicID].voters[voter] != 0)
return true;
return false;
}
function getPollInfo(uint topicID) public view returns (uint, string memory, uint, uint) {
require(isPollExistent(topicID));
return (
polls[topicID].numOptions,
polls[topicID].dataHash,
polls[topicID].timestamp,
polls[topicID].voteCounts[0]
);
}
// Gets vote count for a specific option (option 0 will return total count)
function getVoteCount(uint topicID, uint option) public view returns (uint) {
require(isPollExistent(topicID)); // Verify that poll exists
return (polls[topicID].voteCounts[option]);
}
function vote(uint topicID, uint option) public {
require(isPollExistent(topicID));
Poll storage poll = polls[topicID];
require(option > 0 && option <= poll.numOptions); // Verify that this option exists
address voter = msg.sender;
uint currentVote = poll.voters[voter];
if(currentVote == option)
return;
if(currentVote == 0) // Voter hadn't voted before
poll.voteCounts[0]++;
else
poll.voteCounts[currentVote]--;
poll.voteCounts[option]++;
poll.voters[voter] = option;
emit UserVoted(voter);
}
}

3
packages/concordia-contracts/migrations/2_deploy_contracts.js

@ -1,6 +1,7 @@
const Forum = artifacts.require('Forum');
const Voting = artifacts.require('Voting');
// eslint-disable-next-line func-names
module.exports = function (deployer) {
deployer.deploy(Forum);
deployer.deploy(Forum).then((forum) => deployer.deploy(Voting, forum.address));
};

2
packages/concordia-contracts/truffle-config.js

@ -8,7 +8,7 @@ module.exports = {
// to customize your Truffle configuration!
compilers: {
solc: {
version: '0.7.1',
version: '0.7.4',
},
},
contracts_build_directory: path.join(__dirname, 'build/'),

Loading…
Cancel
Save